package RPS::Import::FNAC2;

use strict;

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

use lib '/app/tools/common/lib';
use Common::Country;

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

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

my %fieldMap = (
        2 => {
            country     => 0,
            date        => 1,
            product     => 7,
            catalog     => 9,
            upc         => 10,
            album       => 11,
            aartist     => 12,
            track       => 13,
            tartist     => 14,
            label       => 15,
            isrc        => 16,
            units       => 22,
            price       => 23,
            format      => 25,
        },
        3 => {
            country     => 2,
            date        => 3,
            format      => 5,
            product     => 6,
            isrc        => 7,
            tartist     => 9,
            track       => 10,
            label       => 11,
            units       => 12,
            price       => 14,
        },
        5 => {
            country     => 0,
            date        => 1,
            format      => 3,
            product     => 4,
            isrc        => 5,
            upc         => 7,
            catalog     => 8,
            album       => 10,
            aartist     => 11,
            track       => 12,
            tartist     => 13,
            label       => 14,
            currency    => 16,
            units       => 22,
            price       => 23,
        },
        6 => {
            country     => 0,
            date        => 1,
            format      => 3,
            product     => 4,
            isrc        => 5,
            upc         => 7,
            catalog     => 9,
            album       => 11,
            aartist     => 12,
            track       => 13,
            tartist     => 14,
            label       => 15,
            currency    => 17,
            units       => 23,
            price       => 24,
        },
);

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

my %format_map = (
    ''          => RPS::File::Sale::FORMAT_DOWNLOAD,
    'MP3-256'   => RPS::File::Sale::FORMAT_DOWNLOAD,
    'MP3-320'   => RPS::File::Sale::FORMAT_DOWNLOAD,
    STREAM      => RPS::File::Sale::FORMAT_STREAM,
);


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 $product             = $product_map{uc($line->[$offsets->{product}])};
                my $format              = $format_map{uc($line->[$offsets->{format}])};
                my $catalog             = $line->[$offsets->{catalog}];
                my $aartist             = $line->[$offsets->{aartist}];
                my $tartist             = $line->[$offsets->{tartist}];
                my $track               = $line->[$offsets->{track}];
                my $album               = $line->[$offsets->{album}];
                my $units               = $line->[$offsets->{units}];
                my $price               = $line->[$offsets->{price}];
                my $label               = $line->[$offsets->{label}];
                my $isrc                = $line->[$offsets->{isrc}];
                my $upc                 = $line->[$offsets->{upc}];
                $price                  /= $units if($units != 0);

                if ($units && $country ne "Country" && $price && ($track || $album)) {
                    ($dateBegin,$dateEnd) =
                    $self->
                    _getDates(date_begin=>$line->[$offsets->{date}],
                    type=>'french_text');

                    if(!($dateBegin && $dateEnd) && ($version == 3)){
                        ($dateBegin,$dateEnd) =
                        $self->
                        _getDates(date_begin=>$line->[$offsets->{date}],
                        type=>'numerical_year_first');
                    }
                    elsif(!($dateBegin && $dateEnd) && ($version == 5)){
                        ($dateBegin,$dateEnd) =
                        $self->
                        _getDates(date_begin=>$line->[$offsets->{date}], type=>'eur');
                    }
                    elsif(!($dateBegin && $dateEnd) && ($version == 6)){
                        my $f_date = $line->[$offsets->{date}];
						$f_date =~ /(\d+)\D(\d+)\D(\d+)/;
						($dateBegin,$dateEnd) =
                        $self->
                        _getDates(date_begin => sprintf( "%04d-%02d-%02d", $3, $2, $1 ));
                    }

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

                    my $currency = "EUR";

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

                    my $mediaType = RPS::DB::Item::MediaType::kMediaTypeAudio;

                    if($format =~ /video/i)
                    {
                        $mediaType = RPS::DB::Item::MediaType::kMediaTypeVideo;
                    }


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

                    $sale->mediaType($mediaType) ;
                    $sale->productType($product);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->trackName($track) if($track ne "");
                    $sale->artistName($tartist) if($product eq RPS::File::Sale::TYPE_TRACK);
                    $sale->artistName($aartist) if($product eq RPS::File::Sale::TYPE_ALBUM);
                    $sale->formatType($format);
                    $sale->albumName($album) if(defined $offsets->{album});
                    $sale->labelName($label);
                    $sale->serviceProductID($catalog) if(defined $offsets->{catalog});
                    $sale->units($units);
                    $sale->price($price) if($version != 3);
                    $sale->upc($upc) if(defined $offsets->{upc});
                    $sale->isrc($isrc);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $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;
}

#
# Private methods
#


#sub _getDates
#{
#    my ($syear,$eyear,$smonth,$emonth,$passed_date,$dateBegin,$dateEnd);
#    $passed_date = shift;
#    $passed_date =~ s/\'//;
    #print STDERR "|>".$passed_date."<|\n";
#    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));
#    }
#    elsif($passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/) {
#            return ($1."-".sprintf("%02d",$2)."-01",$1."-".sprintf("%02d",$2)."-".Days_in_Month($1,$2));
#    }
#    elsif($passed_date =~ m/(\d{4})(\d{2})(\d{2})/) {
#        return ($1."-".$2."-01",$1."-".$2."-".Days_in_Month($1,$2));
#    }
#    elsif($passed_date =~ m/(\w+) (\d{4})/ && $passed_date !~ /(janv|fevr|mars|avr|mai|juin|juil|aout)/) {
#        return ($2."-".sprintf("%02d",Decode_Month($1))."-01",$2."-".sprintf("%02d",Decode_Month($1))."-".Days_in_Month($2,Decode_Month($1)));
#    }
#    elsif($passed_date =~ /(\w{4}) (\d{4})/ ||
#    $passed_date =~ /f.(vr)\. (\d{4})/ ||
#    $passed_date =~ /d.(c)\. (\d{4})/ ||
#    $passed_date =~ /(\D{4})\. (\d{4})/ ||
#    $passed_date =~ m/(\w{3})\. (\d{4})/
#    ) {
        #print STDERR "Hit date $1\n";
#        my %month_map = (
#            janv    => 1,
#            fevr    => 2,
#            'févr'  => 2,
#            'févr'  => 2,
#            'vr'    => 2,
#            mars    => 3,
#            avr     => 4,
#            mai     => 5,
#            juin    => 6,
#            juil    => 7,
#            aout    => 8,
#            'août'  => 8,
#            sept    => 9,
#            'oct'   => 10,
#            nov     => 11,
#            dec     => 12,
#            'déc'   => 12,
#            'c'     => 12,
#        );

        #print STDERR "Got month: ".$month_map{$1}."\n";

#        return ($2."-".sprintf("%02d",$month_map{$1})."-01",$2."-".sprintf("%02d",$month_map{$1})."-".Days_in_Month($2,$month_map{$1}));
#    }

#    return undef;
#}

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