package RPS::Import::7Digital2;

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

use lib '/app/tools/rps/lib';

use RPS::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

my %fieldMap = (           
        7 => {
            date    => 0,
            artist  => 1,
            album   => 2,            
            track   => 4,
            isrc    => 5,
            upc     => 6,
            label   => 7,
            currency=> 9,
            price   => 12,
        },                   
);

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

    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 $track               = $line->[$offsets->{track}];
                my $album               = $line->[$offsets->{album}];
                my $artist              = $line->[$offsets->{artist}];
                my $format              = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $product             = RPS::File::Sale::TYPE_TRACK;
                my $units               = 1;
                my $price               = $line->[$offsets->{price}];
                my $label               = $line->[$offsets->{label}];
                my $isrc                = $line->[$offsets->{isrc}];
                my $upc                 = $line->[$offsets->{upc}];
                $product                = RPS::File::Sale::TYPE_ALBUM if($track eq "");
                my $f_date              = $line->[$offsets->{date}];
                
                if ($units && $artist && $price && ($track && $track ne "trackname" 
                || $album && $album ne "productname") && 
                $album ne "Product Name") {
                    
                    my ($dateBegin,$dateEnd)= 
                    $self->_getDates(date_begin=>$f_date, 
                    type=>"numerical_year_first");

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

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

                    $sale->mediaType(1);
                    $sale->productType($product);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->labelName($label);
                    $sale->trackName($track) if($track ne "");
                    $sale->albumName($album);
                    $sale->artistName($artist);
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->isrc($isrc);
                    $sale->upc($upc);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

                    $self->_insertSale(sale => $sale);
                } 
                #else { print STDERR "skip sh: ".$sheet_names->[$sheet_count] . ": l: " . $linenum . " - u:".$units." p:".$price." a:".$artist." t:".$track." f:".$format."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
	return $linenum;
}

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