package RPS::Import::WDA2;

use strict;


use Date::Calc qw(Days_in_Month Add_Delta_Days);

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

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 =
(
	2 =>
	{
        label_col         => 2,
		date_col          => 5,
		client_product_id => 6,
		track_title       => 7,
		units             => 12,
		net_revenue       => 14,
	},
	3 =>
	{
        label_col         => 1,
		date_col          => 3,
		client_product_id => 4,
		track_title       => 7,
		units             => 13,
		net_revenue       => 15,
	},
	4 =>
	{
        label_col         => 2,
		date_col          => 5,
		track_title       => 6,
		units             => 10,
		isrc              => 8,
		net_revenue       => 12,
	},
);

#
# public methods
#

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

    my $size_sheets = @sheets;

    my $version = $args{file}->VersionNum();
    my $offsets = $fieldMap{$version};


    return undef unless $lines;

    my $format   = RPS::File::Sale::FORMAT_RINGTONE;
    my $prodtype = RPS::File::Sale::TYPE_TRACK;

    my $linenum = 1;
    foreach my $sheet (@sheets){
    $linenum = 1;

     foreach my $line(@$sheet)
    {
        my $label = $line->[$offsets->{label_col}];
        my $date  = $line->[$offsets->{date_col}];
        my $client_product_id = $line->[$offsets->{client_product_id}] if( $offsets->{client_product_id} );
        my $isrc = $line->[$offsets->{isrc}] if( $offsets->{isrc} );
        my $track = $line->[$offsets->{track_title}];
        my $units = $line->[$offsets->{units}];
        my $revenue = $line->[$offsets->{net_revenue}];
        my $currency = 'USD' if( $version == 4 );

        my $price;

        my ($dateBegin, $dateEnd) = $self->_getDates($date);

        if($units > 0){
                $price = ($revenue / $units);
        }
        elsif($units < 0){
                $price = abs($revenue / $units);
        }




		if ($units && $label && $track && $price)
        {
            unless (defined $dateBegin && defined $dateEnd) {
                    $self->errstr("couldn't get dates from $date at line num $linenum");
                    return undef;
                }



#            my $convRate = ($currency ne 'USD') ? 0 : 1;



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

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            #$sale->artistName($artist);
            #$sale->albumName($album);
            $sale->isrc($isrc);
            $sale->trackName($track);
            $sale->units($units);
            $sale->price($price);
            $sale->labelName($label);
            $sale->clientProductID($client_product_id);
            $sale->lineNum($linenum);
            $sale->countryCode('US');
            $sale->currencyCode($currency);

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


    }

    return $linenum;
}


#
# Private methods
#

sub _getDates
{
	my $self = shift;
	my $passed_date = shift;

	my $year;
	my $month;
	my $day;

	if($passed_date =~ /(\d{4})-(\d{2})-(\d{2})/){

	     ($year, $month, $day) = ($1, $2, $3);
	}
	elsif($passed_date =~ /^(\d+)$/){

	       ($year, $month, $day) = Add_Delta_Days(1900, 1, 1, $1 - 2);

	}
	else{
	       return undef;
	}

        my $dateBegin = sprintf("%04d-%02d-%02d", $year, $month, 1);
        my $dateEnd   = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month));
        return ($dateBegin, $dateEnd);

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