package RPS::Import::NapsterCombined;

use strict;

use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Util;

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

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

# data record indexes
my %FIELD_MAP = (
    3 => {
        vendor_id     => 0,
        date_begin    => 1,
        date_end      => 2,
        country       => 3,
        isrc          => 5,
        upc           => 6,
        prod_type     => 8,
        units         => 9,
        money_code    => 10,
        price         => 11,
        currency_code => 13,
        total_price   => 14,
        artist        => 15,
        album         => 16,
        track         => 17,
    },
    4 => {
        vendor_id     => 0,
        date_begin    => 1,
        date_end      => 2,
        country       => 3,
        isrc          => 5,
        upc           => 6,
        format        => 7,
        units         => 8,
        money_code    => 9,
        price         => 10,
        currency_code => 12,
        total_price   => 13,
        artist        => 14,
        album         => 15,
        track         => 16,
    },
);

sub _importLines {
    my $self = shift;
    my %args = @_;

    return undef unless $args{lines};

    my $version = $self->_version();

    unless ( $version == 3 or $version == 4 ) {
        $self->errstr('wrong version');
        return undef;
    }

    my $map = $FIELD_MAP{$version};

    my $linenum = 1;
    foreach my $line ( @{ $args{lines} } ) {
        my ( $prodType, $format );
        my $free = 0;

        if ( $version == 3 ) {
            my $pType = lc( $line->[ $map->{prod_type} ] );
            if ( $pType =~ /^ota/ ) {
                $prodType = RPS::File::Sale::TYPE_TRACK;
                $format =
                    $pType eq 'ota'             ? RPS::File::Sale::FORMAT_DUALDOWNLOAD
                  : $pType eq 'ota_pc_download' ? RPS::File::Sale::FORMAT_DOWNLOAD
                  :                               '';
            } else {
                $prodType =
                    $pType =~ /album/ ? RPS::File::Sale::TYPE_ALBUM
                  : $pType =~ /track/ ? RPS::File::Sale::TYPE_TRACK
                  :                     '';
                $format = RPS::File::Sale::FORMAT_DOWNLOAD;
            }
        } elsif ( $version == 4 ) {
            $prodType = RPS::File::Sale::TYPE_TRACK;
            my $fName = lc( $line->[ $map->{format} ] );
            if ( $fName eq 'free_stream' ) {
                $format = RPS::File::Sale::FORMAT_STREAM;
                ## these are napster "free" streams, but they have revenue attached.
                ## decision was to NOT mark these as free internally.
                ## NOTE: if they are ever marked as free in the future, it WILL REQUIRE
                ## that additional code in the importer and/or raptor ui be changed.
                if ( $self->numeric( $line->[ $map->{total_price} ] ) == 0 ) {
                    $free = 1;
                }
            } else {
                $format =
                    $fName eq 'stream'                  ? RPS::File::Sale::FORMAT_STREAM
                  : $fName =~ /^(client|device) plays$/ ? RPS::File::Sale::FORMAT_TETHERED
                  :                                       '';
            }
        }

        my $dateBegin   = Common::Util::normalize_date( $line->[ $map->{date_begin} ] );
        my $dateEnd     = Common::Util::normalize_date( $line->[ $map->{date_end} ] );
        my $vendorid    = $line->[ $map->{vendor_id} ];
        my $upc         = Common::Util::normalize_upc( $line->[ $map->{upc} ] );
        my $isrc        = Common::Util::normalize_isrc( $line->[ $map->{isrc} ] );
        my $artist      = $line->[ $map->{artist} ];
        my $album       = $line->[ $map->{album} ];
        my $track       = $line->[ $map->{track} ];
        my $units       = $self->numeric( $line->[ $map->{units} ] );
        my $price       = $self->numeric( $line->[ $map->{total_price} ] );
        my $currCode    = $line->[ $map->{currency_code} ];
        my $countryCode = $line->[ $map->{country} ];
        $price = $price / $units if ( $units != 0 );

        if ( $prodType && ( $album || $track ) && $format ne '' ) {
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodType);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);

            #$sale->serviceProductID($vendorid);
            $sale->upc($upc);
            $sale->isrc($isrc);
            $sale->artistName($artist);
            $sale->albumName($album);
            $sale->trackName($track);
            $sale->units($units);
            $sale->price($price);
            $sale->currencyCode($currCode);
            $sale->countryCode($countryCode);
            $sale->lineNum($linenum);
            $sale->free($free);

            #$args{dumper}($sale);
            $self->_insertSale( sale => $sale );
        }

        #else { print STDERR "$linenum pt: $prodType p: $price (a:$album or t: $track) f: $format\n"; }
        $linenum++;
    }

    return $linenum;
}

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