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

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

        my $headerFound = undef;
		my $recordType;

    	my ($dateBegin,$dateEnd) = $self->_getDates( date_begin => $self->_startDate( $sheet ),
    		                                         date_end   => $self->_endDate( $sheet ) );

		foreach my $line (@$sheet) {
            my $offsets = $self->_fieldMap( $recordType ) if( $recordType );
            my $isrc = Common::Util::normalize_isrc($line->[ $offsets->{isrc} ]) if( $offsets && $offsets->{isrc} );

			if( $line->[1] eq 'Free Content' ) {
				$recordType = 'free';
				$headerFound = undef;
			}
			elsif( $line->[1] eq 'Premium Content' ) {
				$recordType = 'premium';
				$headerFound = undef;
			}


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

            my $artist               = $line->[ $offsets->{artist} ];
            my $title                = $line->[ $offsets->{title} ];
            my $units                = $line->[ $offsets->{units} ];
            my $upc                  = $line->[ $offsets->{upc} ];
            my $netPrice             = $line->[ $offsets->{price} ];
            my $retailPrice          = $line->[ $offsets->{retail_price} ] if( $offsets->{retail_price} );

            my $units;

			if( $offsets->{units} ) {
				$units = $line->[ $offsets->{units} ];
			} else {
				$units = 1;
			}

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

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

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

            if( $artist && $title )
            {
				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->upc($upc);
                $sale->isrc($isrc);
                $sale->units($units);
                $sale->retailPrice($retailPrice);
                $sale->price($unitPrice);
                $sale->countryCode($country);
                $sale->currencyCode($currencyCode);
                $sale->lineNum($linenum);

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

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

    return $linenum;
}

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