package RPS::Import::Zingy2;

use strict;


use Date::Calc qw(Days_in_Month);
use Spreadsheet::ParseExcel;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_date normalize_isrc);
use Common::Consts;

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

use constant DATE_ROW => 1;
use constant DATE_COL => 1;

my %field_map = (
    1 => {
        country  => 0,
        format   => 1,
        vendorid => 2,
        track    => 3,
        artist   => 4,
        price    => 11,
        units    => 13,
    },
    2 => {
        country  => 0,
        format   => 1,
        vendorid => 2,
        isrc     => 3,
        track    => 4,
        artist   => 5,
        price    => 14,
        units    => 10,
    },
);


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


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

	my $lines = $args{lines};

	return undef unless ($lines);

	my ($dateBegin, $dateEnd) = $self->_getDates($lines->[DATE_ROW]->[DATE_COL]);

	unless ($dateBegin && $dateEnd)
	{
        $self->errstr("couldn't get dates");
        print STDERR "date cell: ".$lines->[DATE_ROW]->[DATE_COL]."\n";
		return 0;
	}

    my $out_of_header = 0;  ## flag
	my $linenum = 1;
	foreach my $line (@$lines)
	{
		if($out_of_header == 0)
		{

            if ($line->[0] eq "Territory" && $line->[1] eq "Kind" && 
                $line->[2] eq "ItemID" && $line->[3] eq "ItemName") {
                ## subversion 1, ItemName in col. 3
                $out_of_header = 1;
            } elsif ($line->[0] eq "Territory" && $line->[1] eq "Kind" && 
                     $line->[2] eq "ItemID" && $line->[4] eq "ItemName") {
                ## subversion 2, ItemName in col. 4
                $out_of_header = 2;
            }
            next;
		}

        my $offsets = $field_map{$out_of_header};

        my $format;
        if ($line->[$offsets->{format}] eq "xt") {
            $format = RPS::File::Sale::FORMAT_RINGTONE;
        } elsif ($line->[$offsets->{format}] eq "wt") {
            $format = RPS::File::Sale::FORMAT_RINGTONE;
        }
        ## don't know if other formats might be supported in the future... 
        ## given the format, it seems possible

		my $country		= $line->[$offsets->{country}];
		my $vendorid	= $line->[$offsets->{vendorid}];
        my $isrc        = normalize_isrc($line->[$offsets->{isrc}]) if exists($offsets->{isrc});
		my $artist		= $line->[$offsets->{artist}];
		my $track		= $line->[$offsets->{track}];
		my $units		= $line->[$offsets->{units}];
		my $total_price	= $line->[$offsets->{price}];
		my $price       = $total_price / $units if($units > 0);

		if ($units && $price && $artist && $track && $format)
		{
			my $sale = RPS::Import::SaleRec->new();

			$sale->productType(RPS::File::Sale::TYPE_TRACK);
			$sale->formatType($format);
			$sale->serviceProductID($vendorid);
			$sale->countryCode($country);
			$sale->dateBegin($dateBegin);
			$sale->dateEnd($dateEnd);
			$sale->isrc($isrc);
			$sale->artistName($artist);
			$sale->trackName($track);
			$sale->units($units);
			$sale->price($price);
			$sale->lineNum($linenum);

            ##$args{dumper}($sale);  ## for cmd-line testing
			$self->_insertSale(sale => $sale);
		}
		$linenum++;
	}

	return $linenum;
}

#
# Private methods
#

sub _getDates
{
    my $self  = shift;
    my $date  = shift;
	my ($beginDate, $endDate);

    $date =~ m/^\s*Q([1234])\s(\d{4})\s*$/;
    my $qtr  = $1;
    my $year = $2;

    if ($qtr == 1) {
        $beginDate = $year."-01-01";
        $endDate = $year."-03-".Days_in_Month($year, 3);
    } elsif ($qtr == 2) {
        $beginDate = $year."-04-01";
        $endDate = $year."-06-".Days_in_Month($year, 6);
    } elsif ($qtr == 3) {
        $beginDate = $year."-07-01";
        $endDate = $year."-09-".Days_in_Month($year, 9);
    } elsif ($qtr == 4) {
        $beginDate = $year."-10-01";
        $endDate = $year."-12-".Days_in_Month($year, 12);
    }

	return ($beginDate, $endDate);
}


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