package RPS::Import::Wippit;

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 => {
            label   => 4,
            artist  => 6,
            album   => 7,
            track   => 8,
            catalog => 9,
            price   => 11,
        },
);

#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] !~ /summary/i) {

            foreach my $line (@$sheet) {
                my $country             = "US";
                my $currency            = "USD";
                my $product             = RPS::File::Sale::TYPE_TRACK;
                my $format              = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $catalog             = $line->[$offsets->{catalog}];
                my $artist              = $line->[$offsets->{artist}];
                my $track               = $line->[$offsets->{track}];
                my $album               = $line->[$offsets->{album}];
                my $units               = 1;
                my $price               = $line->[$offsets->{price}];
                my $label               = $line->[$offsets->{label}];

                if ($units && $artist && $country && $price && $track ne "Track Title") {

                    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->trackName($track);
                    $sale->artistName($artist);
                    $sale->formatType($format);
                    $sale->albumName($album);
                    $sale->labelName($label);
                    $sale->serviceProductID($catalog);
                    $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 . " - 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;
    #print STDERR "|>".$passed_date."<|\n";
    if($passed_date =~ m/Big Fish Media (\w{3})(\d{2}) to (\w{3})(\d{2}) Subs/) {
            $syear = $2+2000;
            $eyear = $4+2000;
            $smonth = Decode_Month($1);
            $emonth = Decode_Month($3);
            return ($syear."-".sprintf("%02d",$smonth)."-01",$eyear."-".sprintf("%02d",$emonth)."-".Days_in_Month($eyear,$emonth));
    }

    return undef;
}

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