package RPS::Import::247;

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 Client::Service;

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

my %fieldMap = (
    1 => {
        catalog   => 1,
        date      => 2,
        service   => 4,
        label     => 6,
        artist    => 7,
        title     => 8,
        code      => 9,
        code_type => 10,
        product   => 11,
        units     => 14,
        price     => 16,
        currency  => 17,
        country   => 22,
    },
    2 => {
        catalog   => 1,
        date      => 2,
        service   => 4,
        label     => 8,
        artist    => 9,
        title     => 10,
        code      => 11,
        code_type => 12,
        product   => 14,
        units     => 17,
        price     => 19,
        currency  => 20,
        country   => 25,
    },
    3 => {
        catalog   => 1,
        date      => 2,
        service   => 4,
        label     => 8,
        artist    => 9,
        title     => 10,
        code      => 11,
        code_type => 12,
        upc       => 13,
        product   => 15,
        units     => 18,
        price     => 20,
        currency  => 21,
        country   => 26,
    },
    4 => {
        catalog   => 1,
        date      => 2,
        service   => 4,
        label     => 6,
        artist    => 7,
        title     => 8,
        code      => 9,
        code_type => 10,
        upc       => 11,
        product   => 12,
        units     => 15,
        price     => 17,
        currency  => 18,
        country   => 23,
    },
);

my %product_map = (
    "ALBUM" => RPS::File::Sale::TYPE_ALBUM,
    "TRACK" => RPS::File::Sale::TYPE_TRACK,
    "MAXI"  => RPS::File::Sale::TYPE_ALBUM,
);

my %error_hash;

#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 ( $total_unit_column, $total_price_column, $last_artist );

    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     = $line->[ $offsets->{currency} ];
                my $f_product    = $line->[ $offsets->{product} ];
                my $format       = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $artist       = $line->[ $offsets->{artist} ];
                my $title        = $line->[ $offsets->{title} ];
                my $price        = $line->[ $offsets->{price} ];
                my $units        = $line->[ $offsets->{units} ];
                my $catalog      = $line->[ $offsets->{catalog} ];
                my $label        = $line->[ $offsets->{label} ];
                my $product_code = $line->[ $offsets->{code} ];
                my $code_type    = $line->[ $offsets->{code_type} ];
                my $date         = $line->[ $offsets->{date} ];
                my $upc          = $line->[ $offsets->{upc} ] if ( defined $offsets->{upc} );
                my $f_service    = $line->[ $offsets->{service} ];
                $price /= $units if ( $units != 0 );

                my $product = $product_map{ uc $f_product };

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

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

                    my $service = $self->getServiceID( lc($f_service) ) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $f_service };

                    unless ( $service || $error_hash{$f_service} ) {
                        $error_hash{$f_service} = 1;
                        print STDERR "Couldn't get service from: $f_service at line number $linenum\n";
                        $self->errstr("Couldn't get service from: $f_service at line number $linenum");

                        #return undef;
                    }

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

                    unless ($product) {
                        print STDERR "Unknown product type $f_product on linenum $linenum\n";
                        self->errstr("Unknown product type $f_product on 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->artistName($artist);
                    $sale->trackName($title) if ( $product eq RPS::File::Sale::TYPE_TRACK );
                    $sale->albumName($title) if ( $product eq RPS::File::Sale::TYPE_ALBUM );
                    $sale->serviceProductID($catalog);
                    $sale->labelName($label);
                    $sale->isrc($product_code) if ( $code_type =~ /isrc/i );
                    $sale->serviceID($service);

                    if ($upc) {
                        $sale->upc($upc);
                    } elsif ( ( $code_type =~ /upc/i ) || ( $code_type =~ /ean/i ) ) {
                        $sale->upc($product_code);
                    }

                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

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

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

#
# Private methods
#

#sub _getDates
#{
#    my ($smonth,$syear,$emonth,$eyear,$passed_date,$dateBegin,$dateEnd);
#	$passed_date = shift;
#    if($passed_date =~ m/\_(\d\d)\_q(\d)/) {
#        $syear = 2000 + $1;
#        $eyear = 2000 + $1;
#        if($2 == 1) {
#            $smonth = "01";
#            $emonth = "03";
#        }
#        elsif($2 == 2) {
#            $smonth = "04";
#            $emonth = "06";
#        }
#        elsif($2 == 3) {
#            $smonth = "07";
#            $emonth = "09";
#        }
#        elsif($2 == 4) {
#            $smonth = "10";
#            $emonth = "12";
#        }

#    }
#    if($smonth && $syear && $emonth && $eyear) {
#        return ($syear."-".$smonth."-01",$eyear."-".$emonth."-".Days_in_Month($eyear,$emonth));
#    }
#    if ($passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/)
#    {
#      return ($3."-".sprintf("%02d",$1)."-01",$3."-".sprintf("%02d",$1)."-".Days_in_Month($3,$1));
#    }
#    if ($passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/)
#    {
#      return ($1."-".sprintf("%02d",$2)."-01",$1."-".sprintf("%02d",$2)."-".Days_in_Month($1,$2));
#    }

#    return undef;
#}

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