package RPS::Import::5thFinger;

use strict;

use Date::Calc qw(Days_in_Month Add_Delta_Days Decode_Month);

use lib '/app/tools/common/lib';
use Common::Consts qw(%COUNTRY_TO_CODE);

use lib '/app/tools/data_classes/lib';

use lib '/app/tools/rps/lib';
use RPS::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

my %fieldMap = (
    1 => {
        date_row => 5,
        date_col => 2,
        title    => 1,
        artist   => 2,
        label    => 5,
        units    => 6,
        price    => 7,
    },
);

#public methods

sub _importLines {
    my $self = shift;
    my %args = @_;

    my $fileObj     = $args{file};
    my $version     = $args{file}->VersionNum();
    my $fileName    = $args{file}->OrigFileName();
    my $serviceID   = $args{file}->ServiceID;
    my $sheet_count = 0;

    return undef unless ( defined $args{sheets} && defined $version );

    my $offsets        = $fieldMap{$version};
    my $sheet_names    = $args{sheet_names};
    my $header_found   = 0;
    my $linenum        = 1;
    my $price_per_unit = 0;
    my ( $dateBegin, $dateEnd );
    foreach my $sheet ( @{ $args{sheets} } ) {
        my $linenum = 1;
        if ( $sheet_names->[$sheet_count] !~ /summary/i ) {
            my $total_units = 0;
            foreach my $line (@$sheet) {
                my $artist = $line->[ $offsets->{artist} ];
                my $title  = $line->[ $offsets->{title} ];
                my $units  = $line->[ $offsets->{units} ];
                my $price  = $line->[ $offsets->{price} ];
                if ( $artist && $title && $units && $title !~ /track name/i ) {
                    $total_units += $units;
                }
                if ( $units =~ /CSR Share/ ) {
                    $price =~ s/\,//;
                    $price =~ s/\$//;
                    $price_per_unit = $price / $total_units if ( $total_units > 0 );
                }
            }

            foreach my $line (@$sheet) {
                if ( $linenum == $offsets->{date_row} ) {
                    ( $dateBegin, $dateEnd ) = _getDates( $line->[ $offsets->{date_col} ] );
                }
                my $artist   = $line->[ $offsets->{artist} ];
                my $title    = $line->[ $offsets->{title} ];
                my $country  = "AU";
                my $format   = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $prodtype = RPS::File::Sale::TYPE_TRACK;
                my $units    = $line->[ $offsets->{units} ];
                my $price    = $line->[ $offsets->{price} ];
                my $currency = "AUD";
                my $label    = $line->[ $offsets->{label} ];
                my $free     = 0;
                $free = 1 if ( $price == 0 );

                if ( $artist && $units && $title && $title !~ /track name/i && $format ) {

                    unless ( defined $dateBegin && defined $dateEnd ) {
                        print STDERR "Couldn't get dates from: " . $fileName . "\n";
                        $self->errstr( "couldn't get dates from: " . $fileName );
                        return undef;
                    }
                    unless ($country) {
                        print STDERR "Couldn't get country from : " . $line->[ $offsets->{country} ] . "\n";
                        $self->errstr( "Couldn't get country from : " . $line->[ $offsets->{country} ] );
                        return undef;
                    }

                    my $sale = RPS::Import::SaleRec->new();

                    $sale->productType($prodtype);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->artistName($artist);
                    $sale->labelName($label) if ( $label ne "" );
                    $sale->trackName($title) if ( $title ne "" );
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price_per_unit);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

                    #$args{dumper}($sale); ## for cmd-line testing
                    $self->_insertSale( sale => $sale );
                }

#else { print STDERR "skip sh: ".$sheet_names->[$sheet_count] . ": l: " . $linenum . " - u:".$units." p:".$price." a:".$artist." t:".$title." f:".$format."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my $passed_date = shift;
    my ( $year, $month, $day ) = Add_Delta_Days( 1900, 1, 1, $passed_date - 2 );
    my $dateBegin = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
    my $dateEnd = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
    return ( $dateBegin, $dateEnd );
    return undef;
}

###
1;    # Play nicely.
###
