package RPS::Import::TuneTribe;

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 => {
            date        => 0,
            country     => 2,
            artist      => 3,
            title       => 4,
            catalog     => 5,
            upc         => 6,
            isrc        => 7,
            price       => 8,
        },
        2 => {
            date        => 0,
            upc         => 2,
            isrc        => 3,
            units       => 5,
            price       => 6,
            product     => 8,
            artist      => 10,
            title       => 11,
            label       => 12,
            country     => 15,
        },
        4 => {
            date        => 0,
            country     => 3,
            artist      => 4,
            title       => 5,
            catalog     => 6,
            upc         => 7,
            isrc        => 8,
            price       => 9,
        },     
        5 => {
            date        => 0,
            country     => 3,
            artist      => 4,
            title       => 5,
            catalog     => 6,
            upc         => 7,
            isrc        => 8,
            price       => 14,
        },             
);

#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             = $COUNTRY_CODE{lc($line->[$offsets->{country}])};
                my $currency            = "GBP";
                my $product             = RPS::File::Sale::TYPE_TRACK;
                my $artist              = $line->[$offsets->{artist}];
                my $catalog             = $line->[$offsets->{catalog}] if(defined $offsets->{catalog});
                my $upc                 = $line->[$offsets->{upc}];
                my $isrc                = $line->[$offsets->{isrc}];
                my $track               = $line->[$offsets->{title}];
                my $album               = $line->[$offsets->{title}];
                my $format              = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $units               = 1; 
                my $price               = $line->[$offsets->{price}];
                my $date                = $line->[$offsets->{date}];
                $isrc                   =~ s/\-//g;

                if ($units && $country && $price && $track && $track ne "Product name") {
                
                    if($date =~ /(\d{2})(\d{2})(\d{4})/){
                        $date = "$1/$2/$3";
                    }
                    
                    my ($dateBegin,$dateEnd)   = _getDates($date) if(defined $offsets->{date});
                    
                    $product                = RPS::File::Sale::TYPE_ALBUM if($isrc eq "" && $upc ne "");
                    
                    

                    unless (defined $dateBegin && defined $dateEnd) {
                        print STDERR "Couldn't get dates from: ". $fileName . "\n";
                        $self->errstr("couldn't get dates from: ".$fileName);
                        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->upc($upc);
                    $sale->isrc($isrc);
                    $sale->serviceProductID($catalog) if(defined $offsets->{catalog});
                    $sale->artistName($artist);
                    $sale->trackName($track) if($isrc ne "");
                    $sale->albumName($album) if($upc ne "" && $isrc eq "");
                    $sale->formatType($format);
                    $sale->units($units);
                    $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 . " - c:".$country." u:".$units." p:".$price." c:".$catalog." t:".$track." f:".$format."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
	return $linenum;
}

#
# Private methods
#


sub _getDates
{
    my ($year,$month,$monthName,$qtr,$passed_date,$dateBegin,$dateEnd);
	$passed_date = shift;
    $passed_date =~ s/\s.*$//;
    if($passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/) {
            $year = $1;
            $month = $2;
    }
    elsif($passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/) {
        $year = $3;
        $month = $2;
    }
    if($year && $month) {
        return ($year."-".$month."-01",$year."-".$month."-".Days_in_Month($year,$month));
    }

    return undef;
}

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