package RPS::Import::DestraJB;

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/data_classes/lib';

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

my %format_type = (
    D   => RPS::File::Sale::FORMAT_DOWNLOAD,
);

my %product_type = (
    T               => RPS::File::Sale::TYPE_TRACK,
    B               => RPS::File::Sale::TYPE_ALBUM,
);

my %fieldMap = (
    1 => {
        vendor      => 1,
        upc         => 3,
        isrc        => 4,
        artist      => 5,
        title       => 6,
        album       => 7,
        units       => 8,
        date        => 9,
        price       => 17,
        currency    => 18,
        country     => 19,
    },
);

my %deviations = (
    1 => {
        downloadsa  => 31,
        downloadsb  => 33,
        streamsa    => 32,
        streamsb    => 34,
    },
);

#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} }) {
        my $linenum = 1;
        if($sheet_names->[$sheet_count] !~ /summary/i) {

            foreach my $line (@$sheet) {
                my $artist              = $line->[$offsets->{artist}];
                my $title               = $line->[$offsets->{title}];
                my $country             = $line->[$offsets->{country}] if(defined $offsets->{country});
                my $format              = $format_type{$line->[$offsets->{format}]} if(defined $offsets->{format}); 
                my $prodtype            = RPS::File::Sale::TYPE_TRACK;
                my $units               = $line->[$offsets->{units}] if(defined $offsets->{units});
                my $price               = $line->[$offsets->{price}];
                my $currency            = uc $line->[$offsets->{currency}] if(defined $offsets->{currency});
                my $isrc                = $line->[$offsets->{isrc}];
                my $upc                 = $line->[$offsets->{upc}];
                my $album               = $line->[$offsets->{album}] if (defined $offsets->{album});
                my $album_artist        = $line->[$offsets->{album_artist}];
                my $label               = $line->[$offsets->{label}];
                $artist                 = $album_artist if($artist eq "" && $album_artist ne "");
                $prodtype               = RPS::File::Sale::TYPE_ALBUM if($isrc eq "" && $upc ne "");

                if ($price && ($title && $title !~ /title/i || $album && $album !~ /album title/i) && $line->[$offsets->{date}] ne "") {
                    my ($dateBegin, $dateEnd) = $self->_getDates($line->[$offsets->{date}]);

                    unless (defined $dateBegin && defined $dateEnd) {
                        print STDERR "Couldn't get dates from: ". $line->[$offsets->{date}] . "\n";
                        $self->errstr("couldn't get dates from: ".$fileName."-".$sheet_names->[$sheet_count]);
                        return undef;
                    }
                    my $columns = $deviations{$version};
                    for my $keys(keys %$columns)  {
                        #print STDERR "l:".$linenum . " k:" . $keys . " c:" . $columns->{$keys} . " u:" . $line->[$columns->{$keys}] . "\n";
                        if($line->[$columns->{$keys}] != 0) {
                            $format = RPS::File::Sale::FORMAT_DOWNLOAD if($keys =~ /download/i);
                            $format = RPS::File::Sale::FORMAT_STREAM if($keys =~ /stream/i);

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

                            $sale->productType($prodtype);
                            $sale->dateBegin($dateBegin);
                            $sale->dateEnd($dateEnd);
                            $sale->isrc($isrc) if($isrc ne "");
                            $sale->upc($upc) if($upc ne "");
                            $sale->artistName($artist);
                            $sale->labelName($label) if($label ne "");
                            $sale->trackName($title) if($title ne "");
                            $sale->albumName($album) if($album ne "");
                            $sale->formatType($format);
                            $sale->units( $line->[$columns->{$keys}]);
                            $sale->price($price);
                            $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:".$title." f:".$format."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
	return $linenum;
}

#
# Private methods
#


sub _getDates
{
    my ($month,$monthName,$year);
    my $self = shift;
	my $passed_date = shift;
    #print STDERR "|->".$passed_date."<-|\n";
    if($passed_date =~ m/2(\d{3})/ && $passed_date !~ m/(\d{4})\D(\d+)\D(\d)/ && $passed_date !~ m/(\d+)\D(\d+)\D(\d{4})/) {
        $year = "2".$1;
        if($passed_date =~ m/(\d{4})(\d\d)(\d\d)/) {
            $year = $1;
            $month = $2;
        }
        elsif($passed_date =~ m/(\d{4})\.(\d+)/) {
            $year = $1;
            $month = $2;
        }
    }
    elsif($passed_date =~ m/(\d{4})\.(\d+)/) {
        $year = $1;
        $month = $2;
    }
    elsif($passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/) {
        $year = $1;
        $month = $2;
    }
    elsif($passed_date =~ m/(\d{4})\s(\w+)/) {
        $year = $1;
        $monthName = $2;
    }
    elsif($passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/) {
        $year = $3;
        $month = $1;
    }
    elsif($passed_date =~ m/(\d{4})(\d\d)(\d\d)/) {
        $year = $1;
        $month = $2;
    }
    if($month eq "") {
        $monthName = "jan" if($passed_date =~ /jan/i);
        $monthName = "feb" if($passed_date =~ /feb/i);
        $monthName = "mar" if($passed_date =~ /mar/i);
        $monthName = "apr" if($passed_date =~ /apr/i);
        $monthName = "may" if($passed_date =~ /may/i);
        $monthName = "jun" if($passed_date =~ /jun/i);
        $monthName = "jul" if($passed_date =~ /jul/i);
        $monthName = "aug" if($passed_date =~ /aug/i);
        $monthName = "sep" if($passed_date =~ /sep/i);
        $monthName = "oct" if($passed_date =~ /oct/i);
        $monthName = "nov" if($passed_date =~ /nov/i);
        $monthName = "dec" if($passed_date =~ /dec/i);
        $month = Decode_Month($monthName) if($month eq "" && $monthName ne "");
    }
    #print STDERR "|->".$passed_date."<>".$monthName."<>".$month."<>".$year."<-|\n";
    my $dateBegin = sprintf("%04d-%02d-%02d", $year, $month, 1);
    my $dateEnd   = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month));
    return ($dateBegin, $dateEnd);
    return undef;
}

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