package RPS::Import::DanceTunes::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;

    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 $label = $line->[ $offsets->{label} ];

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

        	my $period = $line->[ $offsets->{period} ];
			$period =~ /^(\d{4})(\d{2})/;
			my $startDate = sprintf( "%04d-%02d-%02d", $1, $2, 1 );

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

            my $artist               = $line->[ $offsets->{artist} ];
            my $title                = $line->[ $offsets->{title} ];
            my $units                = $line->[ $offsets->{units} ];
            my $isrc                 = Common::Util::normalize_isrc($line->[ $offsets->{isrc} ]) if( $offsets && $offsets->{isrc} );
            my $upc                  = $line->[ $offsets->{upc} ];
            my $retailPrice          = $line->[ $offsets->{retail_price} ] if( $offsets->{retail_price} );
            my $f_country            = $line->[ $offsets->{country} ];
			my $currencyCode         = $self->_currency();
            my $units                = $line->[ $offsets->{units} ];
            my $type                 = lc( $line->[ $offsets->{type} ] );
            my $f_netPrice           = $line->[ $offsets->{price} ];
            my $catalogID            = $line->[ $offsets->{catalog_id} ];

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

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

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

    			my $countryObj = Common::Country->Get( $f_country ) ||
			        die "Unknown country '$f_country' (line: $linenum)";
			    my $country = $countryObj->alpha2();

				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($title);
                $sale->artistName($artist);
                $sale->labelName($label);
                $sale->upc($upc);
                $sale->isrc($isrc);
                $sale->units($units);
                $sale->retailPrice($retailPrice);
                $sale->price($unitPrice);
                $sale->countryCode($country);
                $sale->currencyCode($currencyCode);
                $sale->clientProductID($catalogID);
                $sale->lineNum($linenum);

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

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

    return $linenum;
}

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