package RPS::Import::Jamvana::Base2010;

use strict;

use Math::BigFloat;
use Date::Calc qw(Days_in_Month Decode_Month);
use Data::Dumper;

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

use lib '/app/tools/data_classes/lib';
use Client::Service;

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 $fileName = $args{file}->OrigFileName();
    my $serviceID = $args{file}->ServiceID();
	my $productTypeMap = $self->_productTypeMap();
    my $linenum;

    my $offsets = $self->_fieldMap();

    return undef unless ($lines);

	my $sheetnum = 0;

    foreach my $sheet(@{ $args{sheets} }) {
		my $f_sheetnum = $sheetnum + 1;
        $linenum = 1;

        my $headerFound = undef;

		foreach my $line (@$sheet) {
            my $product_id = $line->[ $offsets->{product_id} ];

			unless( $headerFound ) {
				$headerFound = 1
				    if( $product_id eq $self->_productIDColumnHeader() );
				$linenum++;
				next;
			}

			my ($dateBegin,$dateEnd) = $self->_getDates( $line->[ $offsets->{period} ] );

            my $label                = $line->[ $offsets->{label} ];
            my $artist               = $line->[ $offsets->{artist} ];
            my $track                = $line->[ $offsets->{track} ];
            my $album                = $line->[ $offsets->{album} ];
            my $units                = $line->[ $offsets->{units} ];
            my $netPrice             = $line->[ $offsets->{price} ];
            my $catalogID            = $line->[ $offsets->{catalog_id} ];

			my $currencyCode = $self->_currency();
			my $country      = $self->_country();

			my ($productType, $format, $mediaType);

            if ( $version == 2 ) {
               my $songVersion = $line->[ $offsets->{version} ];
               $track .= " - $songVersion";
            }

			my $mapping = $productTypeMap;
			if ($mapping)
            {
                $productType = $mapping->{productType};
                $format = $mapping->{formatType};
                $mediaType = $mapping->{mediaType};
            } else {
				die "Unknown product type";
			}

            if( $artist && ( $track || $album ) )
            {
				my $unitPrice = $netPrice / $units;

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

                $sale->mediaType($mediaType);
                $sale->productType($productType);
                $sale->formatType($format);
                $sale->clientProductID($catalogID);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->labelName($label);
                $sale->trackName($track);
                $sale->albumName($album);
                $sale->artistName($artist);
                $sale->units($units);
                $sale->price($unitPrice);
                $sale->countryCode($country);
                $sale->currencyCode($currencyCode);
                $sale->lineNum($linenum);

                $self->_insertSale(sale => $sale);
            }

            $linenum++;
        }
        $sheetnum++;
    }

    return $linenum;
}

sub _getDates {
	my $self = shift;
	my $period = shift || return;

	$period =~ /(\d{4})-Q([1234])/i;

	my ($year, $quarter) = ($1, $2);

	die "Could not get period from '$period'" unless( $year && $quarter );

	my $endMonth = $quarter * 3;

	my $startDate = sprintf( "%04d-%02d-%02d", $year, $endMonth - 2, 1 );
	my $endDate   = sprintf( "%04d-%02d-%02d", $year, $endMonth, Days_in_Month($year, $endMonth) );

	return $self->SUPER::_getDates( date_begin => $startDate, date_end => $endDate );
}

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