package RPS::Import::InnDigital::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 $productTypeMap = $self->_productTypeMap();
    my $linenum;

    return undef unless ($lines);

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

    my $sheetnum = 0;

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

        my $headerFound = undef;

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

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

            my $serviceName = $line->[ $offsets->{service} ];
            my $serviceID   = $self->getServiceID($serviceName);

            my $artist   = $line->[ $offsets->{artist} ];
            my $isrc     = Common::Util::normalize_isrc( $line->[ $offsets->{isrc} ] ) if ( $offsets->{isrc} );
            my $upc      = $line->[ $offsets->{upc} ] if ( $offsets->{upc} );
            my $units    = $line->[ $offsets->{units} ];
            my $netPrice = $line->[ $offsets->{price} ];
            my ( $dateBegin, $dateEnd ) = $self->_getDates(
                date_begin => $line->[ $offsets->{date_start} ],
                date_end   => $line->[ $offsets->{date_end} ]
            );

            my $currencyCode = $line->[ $offsets->{currency} ];

            my $f_country = $line->[ $offsets->{country} ];

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

            my $type;
            my $track;
            my $album;

            if ($isrc) {
                $type  = 'track';
                $track = $line->[ $offsets->{track} ];
            } else {
                $type  = 'album';
                $album = $line->[ $offsets->{album} ];
            }

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

            if ( $units && ( $album || $artist || $type ) ) {
                my $country;
                if ($f_country) {
                    my $countryObj = Common::Country->GetByAlpha2($f_country)
                      || die "Unknown country code '$f_country' (line: $linenum)";
                    $country = $countryObj->alpha2() if ($countryObj);
                }

                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->albumName($album);
                $sale->artistName($artist);
                $sale->upc($upc);
                $sale->isrc($isrc);
                $sale->units($units);
                $sale->price($unitPrice);
                $sale->countryCode($country);
                $sale->currencyCode($currencyCode);
                $sale->lineNum($linenum);

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

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

    return $linenum;
}

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