package RPS::Import::NineSquaredMnth;

use strict;


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

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

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

my %field_map = (
    3 => {
        startdate  => 9,
        enddate    => 15,
        vendorid   => 0,
        artist     => 12,
        title      => 18,
        units      => 22,
        totalprice => 25,
    },
    7 => {
        startdate  => 9,
        enddate    => 16,
        vendorid   => 0,
        artist     => 23,
        title      => 24,
        units      => 26,
        totalprice => 29,
    },
    8 => {
        startdate  => 11,
        enddate    => 18,
        vendorid   => 2,
        artist     => 25,
        title      => 26,
        units      => 28,
        totalprice => 31,
    },
    9 => {
        startdate  => 10,
        enddate    => 17,
        vendorid   => 1,
        artist     => 25,
        title      => 26,
        units      => 28,
        totalprice => 31,
    },
);

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 $version = $args{file}->VersionNum();
    #my $version = $args{version}; # for cmd-line testing

    my $offsets = $field_map{$version};

	my $dateBegin;
	my $dateEnd;
	my $header_matched = 0;

	my $linenum = 1;
	foreach my $line (@$lines)
	{
		$header_matched = matchHeader($line, $offsets) if(!$header_matched);

		if ($linenum == 2)
		{
			# these values are in a custom format, so we have to convert them.
			# use normalize to verify
			# Dates are usually 7-1-05  to 8-1-05,
			# but we want the end date to be 7-31-05, so decrement it.
			my $start_date_raw = Spreadsheet::ParseExcel::Utility::ExcelFmt("yyyy-mm-dd", $line->[$offsets->{'startdate'}]);
			my $end_date_raw   = Spreadsheet::ParseExcel::Utility::ExcelFmt("yyyy-mm-dd", $line->[$offsets->{'enddate'}]-1);

			if($start_date_raw =~ /^\d\d\d\d-\d\d-\d\d$/ && $end_date_raw =~ /^\d\d\d\d-\d\d-\d\d$/)
			{
				$dateBegin = $start_date_raw;
				$dateEnd = $end_date_raw;
			}
		}
		elsif ($dateBegin && $dateEnd && $header_matched)
		{
			my $vendorID	= $line->[$offsets->{'vendorid'}];
			my $artist		= $line->[$offsets->{'artist'}];
			my $title		= $line->[$offsets->{'title'}];
			my $track		= $title;
			my $units		= $line->[$offsets->{'units'}];
			my $total_price	= $line->[$offsets->{'totalprice'}];
			my $price = $total_price / $units if($units > 0);

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

				$sale->productType(RPS::File::Sale::TYPE_TRACK);
				$sale->formatType(RPS::File::Sale::FORMAT_RINGTONE);
				$sale->dateBegin($dateBegin);
				$sale->dateEnd($dateEnd);
				$sale->serviceProductID($vendorID);
				# $sale->clientProductID($clientID);
				# $sale->upc($upc);
				# $sale->isrc($isrc);
				$sale->artistName($artist);
				# $sale->albumName($album);
				$sale->trackName($track);
				# $sale->labelName($label);
				$sale->units($units);
				$sale->price($price);
				$sale->lineNum($linenum);
				$sale->countryCode('US');

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

	return $linenum;
}

#
# Private methods
#

sub matchHeader
{
	my $line = shift;
    my $offsets = shift;

	# verify the column headings are as we expect them
	my $artist	= $line->[$offsets->{'artist'}];
	my $track	= $line->[$offsets->{'title'}];
	my $units	= $line->[$offsets->{'units'}];
	my $price	= $line->[$offsets->{'totalprice'}];

	if($artist =~ /author/i &&
	   $track  =~ /product/i &&
	   $units  =~ /units/i && # not a typo
	   $price  =~ /payment|royalty/i)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


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