package RPS::Import::Nokia;

use strict;

use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Consts qw(%COUNTRY_TO_CODE);

use lib '/app/tools/data_classes/lib';

use lib '/app/tools/rps/lib';
use RPS::File::Sale;
use base 'RPS::Import::Importer';

use constant FIELD_TITLE          => 5;
use constant FIELD_ARTIST         => 4;
use constant FIELD_ISRC           => 6;
use constant FIELD_UPC            => 7;
use constant FIELD_CURRENCY_CODE  => 15;
use constant FIELD_TOTAL          => 14;
use constant FIELD_DELIVERY       => 2;
use constant FIELD_PRICE          => 6;
use constant FIELD_SALE_DATE      => 3;
use constant FIELD_COUNTRY        => 13;
use constant FIELD_LABEL          => 8;
use constant FIELD_CATALOG_NUMBER => 9;
use constant FIELD_EXCHANGE       => 13;
use constant FIELD_RATE           => 14;

my %delivery_formats = (
    'DOWNLOAD'              => RPS::File::Sale::FORMAT_DOWNLOAD,
    'Stream'                => RPS::File::Sale::FORMAT_STREAM,
    'Limited Download'      => RPS::File::Sale::FORMAT_DOWNLOAD,
    'Over the Air Download' => RPS::File::Sale::FORMAT_DOWNLOAD,
    'Ringtone'              => RPS::File::Sale::FORMAT_RINGTONE,
    'Ringback'              => RPS::File::Sale::FORMAT_RINGBACK,
    'Over the Air Download' => RPS::File::Sale::FORMAT_DOWNLOAD,
    'Burn'                  => RPS::File::Sale::FORMAT_DOWNLOAD,
    'Other'                 => RPS::File::Sale::FORMAT_STREAM,
    'Play'                  => RPS::File::Sale::FORMAT_STREAM,
    'Realtone'              => RPS::File::Sale::FORMAT_RINGTONE,
);

#
# public methods
#

sub _importLines {
    my $self  = shift;
    my %args  = @_;
    my $lines = $args{lines};
    return undef unless ($lines);

    #my $version = $args{version}; # cmd-line testing
    #my $fileName = $args{OrigFileName}; # cmd-line testing
    my $version  = $args{file}->VersionNum();
    my $fileName = $args{file}->OrigFileName();

    my $format          = RPS::File::Sale::FORMAT_STREAM;
    my $linenum         = 1;
    my $was_album_title = 0;
    my (
        %errors,        $passed_format,  $album,         $dateBegin,  $dateEnd,
        $total_columns, $fee_percentage, $exchange_rate, $commission, $commisable_units
    );

    foreach my $line (@$lines) {
        $exchange_rate = $line->[FIELD_RATE] if ( $line->[FIELD_EXCHANGE] eq "XR" );
        $commission    = $line->[FIELD_RATE] if ( $line->[FIELD_EXCHANGE] eq "Net Revenue Commission" );
        $commisable_units++ if ( $line->[FIELD_ARTIST] ne "" && $line->[FIELD_ARTIST] !~ /artist/i && $line->[FIELD_TITLE] ne "" );
        $linenum++;
    }
    $commission /= $commisable_units if ( $commission ne "" && $commisable_units > 0 );
    $linenum = 1;
    foreach my $line (@$lines) {
        my $artist = $line->[FIELD_ARTIST];
        my $isrc   = $line->[FIELD_ISRC];
        my $title  = $line->[FIELD_TITLE];
        my $upc    = $line->[FIELD_UPC];

        #my $price           = .01;
        my $price           = $line->[FIELD_TOTAL];
        my $currency_code   = $line->[FIELD_CURRENCY_CODE];
        my $format          = $delivery_formats{'Stream'};
        my $units           = 1;
        my $label           = $line->[FIELD_LABEL];
        my $service_product = $line->[FIELD_CATALOG_NUMBER];
        my $country         = $Common::Consts::COUNTRY_CODE{ lc $line->[FIELD_COUNTRY] };

        $price /= $exchange_rate if ( $exchange_rate ne "" && $currency_code ne "GBP" );
        $price += $commission if ( $commission != 0 );
        $currency_code = "GBP" if ( $exchange_rate ne "" && $currency_code ne "GBP" );

        if ( ( $artist ne "" ) && ( $artist !~ /artist/i ) && ( $units > 0 ) && ( $title ne "" ) ) {
            print STDERR "Country: $country line num: $linenum\n";
            my $sale = RPS::Import::SaleRec->new();
            my ( $dateBegin, $dateEnd ) =
              $self->_getDates( date_begin => $line->[FIELD_SALE_DATE], type => "iso" );

            $sale->productType(RPS::File::Sale::TYPE_TRACK);
            $sale->upc($upc);
            $sale->isrc($isrc)       if ( $isrc ne "" );
            $sale->trackName($title) if ( $isrc ne "" );
            $sale->albumName($title) if ( $isrc eq "" );
            $sale->formatType($format);
            $sale->serviceProductID($service_product);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->artistName($artist);
            $sale->labelName($label);
            $sale->price( sprintf( "%.8f", ( $price / $units ) ) );
            $sale->units($units);
            $sale->currencyCode($currency_code);
            $sale->countryCode($country);
            $sale->lineNum($linenum);

            #$args{dumper}($sale); ## for cmd-line testing
            $self->_insertSale( sale => $sale );
        }

        #else { print STDERR "skip l:".$linenum." a:".$artist." t:".$title." u:".$units." p:".$price."\n"; }
        $linenum++;
    }
    return $linenum;
}

#
# Private methods
#

#sub _get_dates
#{
#    my $passed_date = shift;

#    return undef unless ($passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/);
#    my ($year,$month,$day) = ($1, $2, $3);
#    return (
#        sprintf("%04d-%02d-01", $year, $month),
#        sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month))
#    );
#}

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