package RPS::Import::XpressBeats;

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/rps/lib';
use RPS::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

my %fieldMap = (
    1 => {
        date_start_row => 8,
        date_end_row   => 9,
        date_col       => 5,
        isrc           => 0,
        catalog        => 1,
        track_no       => 2,
        artist         => 3,
        track          => 4,
        price          => 5,
        units          => 6,
    },
);

#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      = "GB";
                my $currency     = "GBP";
                my $product      = RPS::File::Sale::TYPE_TRACK;
                my $artist       = $line->[ $offsets->{artist} ];
                my $catalog      = $line->[ $offsets->{catalog} ] if ( defined $offsets->{catalog} );
                my $isrc         = $line->[ $offsets->{isrc} ];
                my $track        = $line->[ $offsets->{track} ];
                my $track_number = $line->[ $offsets->{track_no} ];
                my $format       = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $units        = $line->[ $offsets->{units} ];
                my $price        = $line->[ $offsets->{price} ];
                $isrc =~ s/\-//g;
                $price /= $units if ( $units != 0 );
                $dateBegin = _getDates( $line->[ $offsets->{date_col} ] ) if ( $linenum == $offsets->{date_start_row} );
                $dateEnd   = _getDates( $line->[ $offsets->{date_col} ] ) if ( $linenum == $offsets->{date_end_row} );

                if ( $units && $country && $price && $track && $track ne "TITLE" ) {

                    unless ( defined $dateBegin && defined $dateEnd ) {
                        print STDERR "Couldn't get dates from: " . $sheet->[ $offsets->{date_start_row} ]->[ $offsets->{date_col} ] . "\n";
                        $self->errstr( "couldn't get dates from: " . $sheet->[ $offsets->{date_end_row} ]->[ $offsets->{date_col} ] );
                        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->isrc($isrc);
                    $sale->serviceProductID($catalog) if ( defined $offsets->{catalog} );
                    $sale->artistName($artist);
                    $sale->trackName($track);
                    $sale->trackNum($track_number);
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->conversionRate(0) if ( $currency ne "USD" );
                    $sale->lineNum($linenum);

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

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

#
# Private methods
#

sub _getDates {
    my ( $year, $month, $day, $monthName, $qtr, $passed_date, $dateBegin, $dateEnd );
    $passed_date = shift;
    $passed_date =~ s/\s.*$//;
    print STDERR "|>" . $passed_date . "<|\n";
    if ( $passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/ ) {
        $year  = $1;
        $month = $2;
        $day   = $3;
    }
    if ( $year && $month && $day ) {
        return ( $year . "-" . sprintf( "%02d", $month ) . "-" . sprintf( "%02d", $day ) );
    }

    return undef;
}

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