package RPS::Import::DJMR2;

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_CODE);

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

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

my %fieldMap = (
    2 => {
        country     => 3,
        date        => 6,
        isrc        => 7,
        upc         => 8,
        svc_prod_id => 9,
        label       => 10,
        artist      => 11,
        track       => 12,
        units       => 14,
        revenue     => 16,
    },
);

#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 );

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

            foreach my $line (@$sheet) {
                my $country     = $line->[ $offsets->{country} ];
                my $currency    = "USD";
                my $product     = RPS::File::Sale::TYPE_TRACK;
                my $format      = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $svc_prod_id = $line->[ $offsets->{svc_prod_id} ];
                my $artist      = $line->[ $offsets->{artist} ];
                my $track       = $line->[ $offsets->{track} ];
                my $units       = $line->[ $offsets->{units} ];
                my $revenue     = $line->[ $offsets->{revenue} ];
                my $label       = $line->[ $offsets->{label} ];
                my $isrc        = $line->[ $offsets->{isrc} ];
                my $upc         = $line->[ $offsets->{upc} ];
                my $date        = $line->[ $offsets->{date} ];

                my $price = ( $revenue / $units ) if ( $units > 0 );

                my $countryCode = $Common::Consts::COUNTRY_CODE{ lc $country };

                # Remove timestamp if present, convert to ISO format
                if ( $date =~ m/ (\d\d|\d):\d\d$/ ) {
                    $date =~ s/ (\d\d|\d):\d\d$//;

                    if ( $date =~ m/(\d\d|\d)\/(\d\d|\d)\/(\d\d\d\d)/ ) {
                        my ( $month, $day, $year ) = ( $1, $2, $3 );
                        $date = sprintf( "%4d-%02d-%02d", $year, $month, $day );

                        #print STDERR "line $linenum, date = '$date'\n";
                    }
                }

                ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $date, type => "iso" );

                if ( $units && $artist && $country && $price && $track ne "Track Title" ) {

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

                    unless ( $countryCode && $Common::Consts::CODE_TO_COUNTRY{$countryCode} ) {
                        print STDERR "Unknown country $country at linenum: $linenum\n";
                        $self->errstr("Unknown country $country at linenum: $linenum\n");
                        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->trackName($track);
                    $sale->artistName($artist);
                    $sale->formatType($format);
                    $sale->labelName($label);
                    $sale->serviceProductID($svc_prod_id);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->countryCode($countryCode);
                    $sale->currencyCode($currency);
                    $sale->isrc($isrc);
                    $sale->upc($upc);
                    $sale->lineNum($linenum);

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

#else { print STDERR "skip l:" . $linenum . " - a:".$artist." i:".$isrc." c:".$country." u:".$units." p:".$price." t:".$track." a:".$album."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
    return $linenum;
}

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