package RPS::Import::WMG::NapsterStream;

use strict;


use Date::Calc qw(Days_in_Month);

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

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

# private constants

# header record
use constant FIELD_DATEBEGIN    => 2;
use constant FIELD_DATEEND        => 3;

# detail recordS
use constant FIELD_DATE            => 0;
use constant FIELD_VENDORID        => 1;
use constant FIELD_FORMAT        => 3;
use constant FIELD_COUNTRY        => 5;
use constant FIELD_UNITS        => 6;
use constant FIELD_ARTIST        => 9;
use constant FIELD_TRACK        => 10;
use constant FIELD_CURRENCY        => 11;
use constant FIELD_ISRC            => 14;
use constant FIELD_ISRCFLAG        => 15;

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

#public methods

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

    my $lines = $args{lines} || return undef;

    my $linenum = 1;
    foreach my $line(@$lines)
    {
        my $dateBegin   = Common::Util::normalize_date($line->[FIELD_DATE]);
        my $dateEnd     = substr($dateBegin, 0, 8) .  Days_in_Month(substr($dateBegin, 0, 4), substr($dateBegin, 5, 2));
        unless ($dateBegin && $dateEnd)
        {
            $self->errstr('unable to get dates from ' . $line->[FIELD_DATE]);
            return undef;
        }
        my $prodtype    = RPS::File::Sale::TYPE_TRACK;
        my $artist      = $line->[FIELD_ARTIST];
        my $track       = $line->[FIELD_TRACK];
        my $isrc;

        if ($line->[FIELD_ISRCFLAG] =~ m/^ISRC$/i)
        {
            $isrc = Common::Util::normalize_isrc($line->[FIELD_ISRC]);
        }

        # we ignore napster format 1 which is the original tethered download
        # instead we just count the plays of those (all they pay for)
        # note check for format before writing sale record
        my $format;
        if ($line->[FIELD_FORMAT] =~ m/^STR$/i)
        {
            $format = RPS::File::Sale::FORMAT_STREAM;
        }
        elsif ($line->[FIELD_FORMAT] =~ m/^TDL$/i)
        {
            $format = RPS::File::Sale::FORMAT_TETHERED;
        }
        my $vendorid    = $line->[FIELD_VENDORID];
        my ($units)     = $line->[FIELD_UNITS] =~ m/^(-?\d+)$/;;
        my $countryCode = $line->[FIELD_COUNTRY];
        my $currCode    = $line->[FIELD_CURRENCY];

        if ($format && $units && $track)
        {
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->serviceProductID($vendorid);
            $sale->isrc($isrc);
            $sale->artistName($artist);
            $sale->trackName($track);
            $sale->units($units);
            $sale->countryCode($countryCode);
            $sale->currencyCode($currCode);
            $sale->lineNum($linenum);

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

    return $linenum;
}

#
# Private methods
#


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