package RPS::Import::Starzik;

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 => {
            country     => 0,
            date        => 1,
            free        => 4,
            product     => 7,
            upc         => 10,
            album       => 11,
            aartist     => 12,
            track       => 13,
            tartist     => 14,
            label       => 15,
            isrc        => 16,
            units       => 22,
            price       => 23,
        },
);

my %product_map = (
    Album   => RPS::File::Sale::TYPE_ALBUM,
    Track   => RPS::File::Sale::TYPE_TRACK,
);

#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) = _getDates($fileName);

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

            foreach my $line (@$sheet) {
                my ($dateBegin,$dateEnd)= _getDates($line->[$offsets->{date}]);
                my $country             = $line->[$offsets->{country}];
                my $currency            = "EUR";
                my $product             = $product_map{$line->[$offsets->{product}]};
                my $format              = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $upc                 = $line->[$offsets->{upc}];
                my $album               = $line->[$offsets->{album}];
                my $aartist             = $line->[$offsets->{aartist}];
                my $isrc                = $line->[$offsets->{isrc}];
                my $tartist             = $line->[$offsets->{tartist}];
                my $track               = $line->[$offsets->{track}];
                my $units               = $line->[$offsets->{units}];
                my $price               = $line->[$offsets->{price}];
                my $label               = $line->[$offsets->{label}];
                my $free                = ($line->[$offsets->{free}] eq "Y") ? 1 : 0;

                if ($units && ($tartist || $aartist) && $country && ($price || $free) && $track ne "Track name") {

                    my $artist          = ($product eq RPS::File::Sale::TYPE_ALBUM) ? $aartist : $tartist;
                    $price              /= $units if($units != 0);
                    $isrc               =~ s/\-//g;
                    $upc                =~ s/\-//g;

                    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 $sale = RPS::Import::SaleRec->new();

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

                    $self->_insertSale(sale => $sale);
                } 
                #else { print STDERR "skip l:" . $linenum . " - aa:".$aartist." t:".$tartist." i:".$isrc." c:".$country." u:".$units." p:".$price." t:".$track." a:".$album."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
	return $linenum;
}

#
# Private methods
#


sub _getDates
{
    my ($month,$monthName,$year,$passed_date,$dateBegin,$dateEnd);
	$passed_date = shift;
    #print STDERR "|>".$passed_date."<|\n";
    if($passed_date =~ m/(\w{3})\-(\d\d)/) {
        $monthName = $1;
        $year = 2000 + $2;
        $month = Decode_Month($monthName);
    }
    if($month && $year) {
        return ($year."-".sprintf("%02d",$month)."-01",$year."-".sprintf("%02d",$month)."-".Days_in_Month($year,$month));
    }

    return undef;
}

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