package RPS::Import::BigFish;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Consts;

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 %field_map = (
    1 => {
        prodtype    => 0,
        isrc        => 1,
        label       => 2,
        artist      => 3,
        album       => 4,
        track       => 5,
        ptest       => 7,
        mtest       => 8,
        units       => 9,
        total_sale  => 11,
    },
);

#public methods

sub _importLines
{
	my $self = shift;
	my %args = @_;
    my $fileObj = $args{file};
    my $version = $args{file}->VersionNum();
    my $fileName = $fileObj->OrigFileName;
    #my $version = $args{version}; # for cmd-line testing
    #my $fileName = $args{OrigFileName};
    my $offsets = $field_map{$version};

    my $lines = $args{lines};
    my ($dateBegin, $dateEnd) = $self->_getDates($lines->[$offsets->{date_row}]->[$offsets->{date_col}]);

    unless ($dateBegin && $dateEnd) {
        $self->errstr("couldn't get dates from ".$lines->[$offsets->{date_row}]->[$offsets->{date_col}]);
        return undef;
    }


    my $linenum = 1;
    my $last_track = "";
    foreach my $line (@$lines) {
        my ($prodtype,$format,$artist,$label,$album,$track,$upc,$isrc,$units,$price);
        ($dateBegin, $dateEnd) = $self->_getDates($line->[1]) if($linenum == 1);

        if($line->[$offsets->{prodtype}] =~ /^track$/i) {
            $prodtype   = RPS::File::Sale::TYPE_TRACK;
            $isrc       = $line->[$offsets->{isrc}];
        }
        elsif($line->[$offsets->{prodtype}] =~ /^album$/i) {
            $prodtype   = RPS::File::Sale::TYPE_ALBUM;
            $upc        = $line->[$offsets->{isrc}];
        }
        $format         = RPS::File::Sale::FORMAT_DOWNLOAD;
        $label          = $line->[$offsets->{label}];
        $artist         = $line->[$offsets->{artist}];
        $album          = $line->[$offsets->{album}];
        $track          = $line->[$offsets->{track}];
        $units	        = $line->[$offsets->{units}];
        $price          = $line->[$offsets->{total_sale}] if exists $offsets->{total_sale};
        $price          = ($units != 0) ? ($price / $units) : 0;

        if ($units && $artist && $price && $label !~ /^label$/i) {
             unless ($dateBegin && $dateEnd) {
                    $self->errstr("couldn't get dates from ".$lines->[1]->[1]);
                    return undef;
            }
            if($line->[$offsets->{ptest}]>0 || $line->[$offsets->{mtest}]>0) {
                $self->errstr("Unexpected Pricing encountered");
                print STDERR "Unexpected pricing encountered: " . $line->[$offsets->{ptest}].",".$line->[$offsets->{mtest}]."\n";
                return undef;
            }
            my $sale = RPS::Import::SaleRec->new();
 
            $sale->countryCode('US');
            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->labelName($label);
            $sale->artistName($artist);
            $sale->trackName($track) if ($track ne "");
            $sale->albumName($album);
            $sale->isrc($isrc) if ($isrc ne "");
            $sale->upc($upc) if ($upc ne "");
            $sale->units($units);
            $sale->lineNum($linenum);
            $sale->price($price);
            #$args{dumper}($sale);  ## for cmd-line testing
            $self->_insertSale(sale => $sale);
        }
        #else { print STDERR "Skipping line: " . $linenum . " a: ".$artist." t:".$track." a:".$album." u:".$units."\n";}
        $linenum++;
    }
	return $linenum;
}

#
# Private methods
#


sub _getDates
{
	my $self = shift;
	my $date = shift;

    my ($beginDate, $endDate);

    $date =~ s/^.*\(//;
    $date =~ s/\).*$//;
    my @date_parts = split(" - ",$date);
    my ($smonth,$sday,$syear) = split(" ",$date_parts[0]);
    my ($emonth,$eday,$eyear) = split(" ",$date_parts[1]);
    $beginDate = sprintf("%04d-%02d-%02d", $syear, Decode_Month($smonth), $sday);
    $endDate   = sprintf("%04d-%02d-%02d", $eyear, Decode_Month($emonth), $eyear);
    return ($beginDate, $endDate);
    return undef;
}

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