package RPS::Import::DanceRecords;

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 %fieldMap = (
        1 => {
            date        => 0,
            units       => 3,
            price       => 4,
            deduction   => 5,
            isrc        => 6,
            artist      => 7,
            track       => 8,
            label       => 9,
            country      => 10,
            upc         => 11,
            product     => 12,
        },
);

my %product_map = (
    track       => RPS::File::Sale::TYPE_TRACK,
    album       => RPS::File::Sale::TYPE_ALBUM,
);

#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             = $line->[$offsets->{country}];
                my $currency            = "USD";
                my $track               = $line->[$offsets->{track}];
                my $artist              = $line->[$offsets->{artist}];
                my $format              = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $product             = $product_map{lc ($line->[$offsets->{product}])};
                my $units               = $line->[$offsets->{units}];
                my $price               = $line->[$offsets->{price}];
                my $deduction           = $line->[$offsets->{deduction}];
                my $label               = $line->[$offsets->{label}];
                my $isrc                = $line->[$offsets->{isrc}];
                my $upc                 = $line->[$offsets->{upc}];
                my ($dateBegin,$dateEnd)= _getDates($line->[$offsets->{date}]);
                $price                  -= $deduction;

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

                    unless (defined $dateBegin && defined $dateEnd) {
                        print STDERR "Couldn't get dates from: ". $line->[$offsets->{date}] . "\n";
                        $self->errstr("couldn't get dates from: ".$line->[$offsets->{date}]);
                        return undef;
                    }

                    unless ($product) {
                        print STDERR "Unknown product:".$product." l:".$linenum."\n";
                        $self->errstr("Unknown product: ".$product." l:".$linenum);
                    }

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

                    $sale->mediaType(2) if($format =~ /video/i);
                    $sale->productType($product);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->labelName($label);
                    $sale->trackName($track) if($product eq RPS::File::Sale::TYPE_TRACK);
                    $sale->albumName($track) if($product eq RPS::File::Sale::TYPE_ALBUM);
                    $sale->artistName($artist);
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->isrc($isrc) if($product eq RPS::File::Sale::TYPE_TRACK);
                    $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;
}

#
# Private methods
#


sub _getDates
{
    my ($smonth,$syear,$sday,$emonth,$eyear,$eday);
	my $passed_date = shift;
    if($passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/) {
        $syear = $1;
        $smonth = $2;
    }
    elsif($passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/) {
        $syear = $3;
        $smonth = $1;
    }
    if($syear && $smonth) {
        return ($syear."-".$smonth."-01",$syear."-".$smonth."-".Days_in_Month($syear,$smonth));
    }
    return undef;
}

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