package RPS::Import::ICJ::Base2010;

use strict;

use Math::BigFloat;
use Date::Calc qw(Days_in_Month Decode_Month Add_Delta_DHMS);
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 _countryCode { 'JP' }
sub _currencyCode { 'JPY' }

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;

    return undef unless ($lines);

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

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

        my $headerFound = undef;

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

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

        	my $startDate = $self->_getDate( $line->[ $offsets->{period} ] );

            my $artist               = $line->[ $offsets->{artist} ];
            my $units                = $line->[ $offsets->{units} ];
            my $countryCode          = $self->_countryCode();
			my $currencyCode         = $self->_currencyCode();
            my $units                = $line->[ $offsets->{units} ];
            my $type                 = lc( $line->[ $offsets->{format} ] );
            my $catalogID            = $line->[ $offsets->{product_id} ];
            my $f_netPrice           = $line->[ $offsets->{price} ];

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

			my $mapping = $productTypeMap->{$type};
			if ($mapping)
            {
                $productType = $mapping->{productType};
                $format = $mapping->{formatType};
                $mediaType = $mapping->{mediaType};
			}

            if( $artist && $track )
            {
				my ($dateBegin,$dateEnd) = $self->_getDates( date_begin => $startDate );

                unless( $productType ) {
				    die "Unknown product type '$type' (line: $linenum)";
				}

				my $netPrice = $f_netPrice;
				$netPrice =~ s/,/./;
				my $unitPrice = $netPrice / $units;

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

                $sale->mediaType($mediaType);
                $sale->productType($productType);
                $sale->formatType($format);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->trackName($track);
                $sale->artistName($artist);
                $sale->units($units);
                $sale->price($unitPrice);
                $sale->countryCode($countryCode);
                $sale->currencyCode($currencyCode);
                $sale->serviceProductID($catalogID);
                $sale->lineNum($linenum);

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

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

    return $linenum;
}

sub _getDate {
	my $self = shift;
	my $period = shift;

	# Mac files are returning the days since epoch 1904-01-01.
	if( $period && $period =~ /^\d+$/ ) {
	    my ($year,$month,$day, $hour,$min,$sec) = Date::Calc::Add_Delta_DHMS(1904,1,1, 0,0,0, $period,0,0,0);
		return sprintf( "%04d-%02d-%02d", $year, $month, $day );
	}

	return $period;
}

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