package RPS::Import::TraxSource;

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 => {
        label     => 0,
        upc       => 1,
        isrc      => 2,
        catalog   => 3,
        artist    => 4,
        album     => 5,
        track_num => 6,
        track     => 7,
        units     => 13,
        price     => 14,
    },
    2 => {
        label     => 0,
        upc       => 1,
        isrc      => 2,
        catalog   => 3,
        artist    => 4,
        album     => 5,
        track_num => 6,
        track     => 7,
        units     => 14,
        price     => 15,
    },
    3 => {
        label     => 0,
        date      => 1,
        upc       => 2,
        isrc      => 3,
        catalog   => 4,
        artist    => 5,
        album     => 6,
        track_num => 7,
        track     => 8,
        units     => 15,
        price     => 16,
    },
    4 => {
        label     => 0,
        date      => 1,
        upc       => 2,
        isrc      => 3,
        catalog   => 4,
        artist    => 5,
        album     => 6,
        track_num => 7,
        track     => 8,
        country   => 10,
        units     => 16,
        price     => 18,
    },
);

#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 ( $dateBegin, $dateEnd );
    ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $fileName, type => "numerical" )
      if ( !defined $offsets->{date} );

    foreach my $sheet ( @{ $args{sheets} } ) {
        $linenum = 1;
        if ( $sheet_names->[$sheet_count] !~ /summary/i ) {

            foreach my $line (@$sheet) {
                my $country;

                $country =~ s/\s+//g;

                if ( defined $offsets->{country} ) {
                    $country = $line->[ $offsets->{country} ];

                    if ( $country eq "" ) {
                        $country = "US";
                    }
                } else {
                    $country = "US";
                }

                my $currency = "USD";
                my $product  = RPS::File::Sale::TYPE_TRACK;
                my $artist   = $line->[ $offsets->{artist} ];
                my $catalog  = $line->[ $offsets->{catalog} ];
                my $track    = $line->[ $offsets->{track} ];
                my $album    = $line->[ $offsets->{album} ];
                my $upc      = $line->[ $offsets->{upc} ];
                my $isrc     = $line->[ $offsets->{isrc} ];
                my $trackNum = $line->[ $offsets->{track_num} ];
                my $label    = $line->[ $offsets->{label} ];
                my $format   = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $units    = $line->[ $offsets->{units} ];
                my $price    = $line->[ $offsets->{price} ];
                $price /= $units if ( $units != 0 );

                if ( $trackNum && !$track && !$isrc ) {
                    print STDERR "Line $linenum: Track sale without track title or isrc\n";
                    $self->errstr("Line $linenum: Track sale without track title or isrc");
                    return undef;
                }

                if ( $units && $price && ( ( $track && $track ne "Track" ) || ( $isrc && $isrc ne "ISRC" ) ) ) {

                    ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $line->[ $offsets->{date} ], type => "text" )
                      if ( defined $offsets->{date} );

                    $product = RPS::File::Sale::TYPE_ALBUM if ( $trackNum == 0 );

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

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

                    $sale->mediaType(2) if ( $format =~ /video/i );
                    $sale->productType($product);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->upc($upc);
                    $sale->isrc($isrc);
                    $sale->serviceProductID($catalog);
                    $sale->artistName($artist);
                    $sale->trackName($track)   if ( $trackNum != 0 );
                    $sale->trackNum($trackNum) if ( $trackNum != 0 );
                    $sale->albumName($album);
                    $sale->labelName($label);
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

                    $self->_insertSale( sale => $sale );
                }

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

#
# Private methods
#

#sub _getDates
#{
#    my ($year,$month,$monthName,$qtr,$passed_date,$dateBegin,$dateEnd);
#	$passed_date = shift;
#    if($passed_date =~ m/_(\d+)\-(\d{4})\.xls/) {
#        $month = $1;
#        $year = $2;
#    }
#    elsif($passed_date =~ m/(\w{3})\-(\d{4})/) {
#        $monthName = $1;
#        $year = $2;
#        $month = Decode_Month($monthName);
#    }
#    if($year && $month) {
#        return ($year."-".$month."-01",$year."-".$month."-".Days_in_Month($year,$month));
#    }

#    return undef;
#}

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