package RPS::Import::MediaDo;

use strict;

use Date::Calc qw(Days_in_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 base 'RPS::Import::Importer';

use Client::Service;

my %field_map = (
    1 => {
        service_product_id  => 0,
        isrc                => 1,
        artist              => 2,
        track               => 3,
        price               => 8,
        currency            => 11,
        format              => 13,
        country             => 14,
        units               => 17,
        label               => 18,
    },
);

my %formats = (
    'tld'   => RPS::File::Sale::FORMAT_TETHERED,
);

my %countries = (
    'japan' => 'JP',
);

my %currencies = (
    'jp'    => 'JPY',
);

#
# public methods
#

sub _importLines
{
    my $self = shift;
    my %args = @_;
    my $lines = $args{lines};
    return undef unless ($lines);
    #my $version = $args{version}; # cmd-line testing
    #my $fileName = $args{OrigFileName}; # cmd-line testing
    my $version = $args{file}->VersionNum();
    my $fileName = $args{file}->OrigFileName();
    my ($dateBegin,$dateEnd) = _get_dates($fileName);
    unless ($dateBegin && $dateEnd) {
        $self->errstr("couldn't get dates");
        print STDERR "Couldn't get dates from file name: $fileName\n";
        return undef;
    }

    my $linenum = 1;
    foreach my $line (@$lines)
    {
        my $artist          = $line->[$field_map{$version}{artist}];
        my $track           = $line->[$field_map{$version}{track}];
        my $isrc            = $line->[$field_map{$version}{isrc}];
        my $units           = $line->[$field_map{$version}{units}];
        my $price           = $line->[$field_map{$version}{price}];
        my $country         = $countries{lc $line->[$field_map{$version}{country}]};
        my $currency        = $currencies{lc $country};
        my $format          = $formats{lc $line->[$field_map{$version}{format}]};
        my $label           = $line->[$field_map{$version}{label}];
        $price = ($units != 0) ? ($price / $units) : 0;
        
        if(($artist ne "" && $units ne "" && $price ne "" && $track ne "" && $track !~ /title/i)) {
            my $sale = RPS::Import::SaleRec->new();
            $sale->productType(RPS::File::Sale::TYPE_TRACK);
            $sale->trackName($track);
            $sale->isrc($isrc) if($isrc ne "");
            $sale->labelName($label) if ($label =~ m/[a-zA-Z]/);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->artistName($artist);
            $sale->price($price);
            $sale->units($units);
            $sale->currencyCode($currency);
            $sale->countryCode($country);
            $sale->lineNum($linenum);
            #$args{dumper}($sale); ## for cmd-line testing
            $self->_insertSale(sale => $sale);
        }
        #else { print STDERR "skip line number: ". $linenum . " : " . join(',', @$line) . "\n"; }
        $linenum++;
    }
    return $linenum;
}

#
# Private methods
#

sub _get_dates
{
    my $passed_date = shift;
    my ($smonth,$emonth,$year,$dateBegin,$dateEnd);
    if($passed_date =~ m/(\d{4})-Q(\d)/) {
        $year = $1;
        my $qtr = $2;
        if ($qtr == 1) {
            $dateBegin = sprintf("%04d-%02d-%02d", $year, 1, 1);
            $dateEnd   = sprintf("%04d-%02d-%02d", $year, 3, Days_in_Month($year, 3));
        } elsif ($qtr == 2) {
            $dateBegin = sprintf("%04d-%02d-%02d", $year, 4, 1);
            $dateEnd   = sprintf("%04d-%02d-%02d", $year, 6, Days_in_Month($year, 6));
        } elsif ($qtr == 3) {
            $dateBegin = sprintf("%04d-%02d-%02d", $year, 7, 1);
            $dateEnd   = sprintf("%04d-%02d-%02d", $year, 9, Days_in_Month($year, 9));
        } else {
            $dateBegin = sprintf("%04d-%02d-%02d", $year, 10, 1);
            $dateEnd   = sprintf("%04d-%02d-%02d", $year, 12, Days_in_Month($year, 12));
        }
    }
    elsif($passed_date =~ m/(\d\d)\.(\d)-(\d)/) {
        $year = $1+2000;
        $smonth = $2;
        $emonth = $3;
        $dateBegin = sprintf("%04d-%02d-%02d", $year, $smonth, 1);
        $dateEnd   = sprintf("%04d-%02d-%02d", $year, $emonth, Days_in_Month($year, $emonth));
    }
    else {
        return undef;
    }
    return ($dateBegin,$dateEnd);
}

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