package RPS::Import::EUK;

use strict;


use Date::Calc qw(Days_in_Month Add_Delta_Days);

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

use lib '/app/tools/data_classes/lib';

# map version num to the field order
my %fieldMap =
(
	1 =>
	{
		'date_begin' => 3,
		'date_end'   => 4,
		'isrc'       => 12,
		'artist'     => 13,
		'title'      => 14,
		'units'      => 15,
		'price'      => 18,
		'currency'   => 19,
		'country'    => 22,
	},
	2 =>
	{
		'date_begin' => 3,
		'date_end'   => 4,
		'isrc'       => 8,
		'artist'     => 9,
		'title'      => 10,
		'units'      => 11,
		'price'      => 14,
		'currency'   => 15,
		'country'    => 18,
	},
	3 =>
	{
		'date_begin' => 3,
		'date_end'   => 4,
		'isrc'       => 8,
		'artist'     => 9,
		'title'      => 10,
		'units'      => 11,
		'price'      => 14,
		'currency'   => 15,
		'country'    => 18,
	},
	4 =>
	{
		'date_begin' => 2,
		'date_end'   => 3,
		'isrc'       => 11,
		'artist'     => 12,
		'title'      => 13,
		'label'      => 14,
		'tracks'     => 16,
		'units'      => 17,
		'price'      => 18,
		'currency'   => 22,
		'country'    => 25,
	},
	5 =>
	{
		'date_begin' => 2,
		'date_end'   => 3,
		'isrc'       => 12,
		'artist'     => 13,
		'title'      => 14,
		'label'      => 15,
		'tracks'     => 17,
		'units'      => 18,
		'price'      => 22,
		'currency'   => 23,
		'country'    => 26,
	},
	6 =>
	{
		'date_begin' => 2,
		'date_end'   => 3,
		'isrc'       => 12,
		'artist'     => 13,
		'title'      => 14,
		'label'      => 15,
		'tracks'     => 17,
		'units'      => 18,
		'price'      => 22,
		'currency'   => 23,
		'country'    => 25,
	},
);

use lib '/app/tools/rps/lib';
use RPS::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

#public methods

sub _importLines
{
	my $self = shift;
	my %args = @_;

	my $lines = $args{lines};
	my $fileObj = $args{file};
	my $version = $fileObj->VersionNum;
    ##my $version = $args{version}; # for cmd-line testing

    return undef unless (defined $lines && defined $version);

	my $offsets = $fieldMap{$version};

    shift @$lines; ## skip header
    my $linenum = 2;
    foreach my $line (@$lines) {

        my $artist   = $line->[$offsets->{artist}];
        my $prodcode = $line->[$offsets->{isrc}];
        my $title    = $line->[$offsets->{title}];
        my $country  = $line->[$offsets->{country}];
        my $currency = $line->[$offsets->{currency}];

        my $label    = $line->[$offsets->{label}] if ($version == 4);

        next if ($country =~ m/^$/); # blank lines at eof

        if ($country !~ m/^[A-Z][A-Z]$/) {
            $self->errstr("invalid country code: $country");
            return undef;
        }
        if ($currency !~ m/^[A-Z][A-Z][A-Z]$/) {
            $self->errstr("invalid currency code: $currency");
            return undef;
        }

        ## just use the end date... begin dates are sometimes funky; it's always 1 month
	    my ($dateBegin, $dateEnd) = $self->_getDates($line->[$offsets->{date_end}]);
        unless (defined $dateBegin && defined $dateEnd) {
            $self->errstr("couldn't get dates from ".$line->[$offsets->{date_end}]);
            return undef;
        }

        my $format   = RPS::File::Sale::FORMAT_DOWNLOAD;
        my $prodtype;
        if ($prodcode =~ m/^D_/ || ($line->[$offsets->{tracks}] > 1 && $version >= 4)) {
            $prodtype = RPS::File::Sale::TYPE_ALBUM;
        } else {
            $prodtype = RPS::File::Sale::TYPE_TRACK;
        }

        my ($units)	= $line->[$offsets->{units}] =~ m/^(-?\d+)$/;
        my ($price)	= $line->[$offsets->{price}] =~ m/^(\d*\.?\d*)$/;

        if ($version == 4) {
            $price = ($units != 0)?($price/$units):0;
        }

        if ($units && $price && $title && $country)
        {
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->upc($prodcode)  if ($prodtype eq RPS::File::Sale::TYPE_ALBUM);
            $sale->isrc($prodcode) if ($prodtype eq RPS::File::Sale::TYPE_TRACK);
            $sale->artistName($artist);
            $sale->albumName($title) if ($prodtype eq RPS::File::Sale::TYPE_ALBUM);
            $sale->trackName($title) if ($prodtype eq RPS::File::Sale::TYPE_TRACK);
            $sale->labelName($label) if ($version >= 4);
            $sale->formatType($format);
            $sale->units($units);
            $sale->price($price);
            $sale->countryCode($country);
            $sale->currencyCode($currency);
            $sale->lineNum($linenum);

            ##$args{dumper}($sale); ## for cmd-line testing
            $self->_insertSale(sale => $sale);
        } 
        #else { print STDERR "$linenum: u: $units , p: $price , t: $title , c: $country\n"; }
        $linenum++;
    }

	return $linenum;
}

#
# Private methods
#


sub _getDates
{
	my $self = shift;
	my $date = shift;  # end date of period

    my ($dateBegin, $dateEnd);
    if ($date =~ m/^(\d{4})-(\d\d)-(\d\d)$/) {
        $dateBegin = sprintf("%04d-%02d-%02d", $1, $2, 1);
        $dateEnd   = sprintf("%04d-%02d-%02d", $1, $2, Days_in_Month($1, $2));
        return ($dateBegin, $dateEnd);
    } elsif ($date =~ m/^(\d\d)(\d\d?)(\d{4})$/) {
        $dateBegin = sprintf("%04d-%02d-%02d", $3, $2, 1);
        $dateEnd   = sprintf("%04d-%02d-%02d", $3, $2, Days_in_Month($3, $2));
        return ($dateBegin, $dateEnd);
    } elsif ($date =~ m/^(\d\d?)\/(\d\d?)\/(\d{4})$/) {
        my $day   = $1;
        my $month = $2;
        my $year  = $3;
        if ($day < 7) {
            if ($month == 1) {
                $year -= 1;
                $month = 12;
            } else {
                $month -= 1;
            }
        }
        $dateBegin = sprintf("%04d-%02d-%02d", $year, $month, 1);
        $dateEnd   = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month));
        return ($dateBegin, $dateEnd);
    }

    return undef;
}

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