package RPS::Import::Napster;

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
use constant FIELD_RECTYPE => 0;

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

my %field_map = (
    1 => {
        'prodtype' => 3,
        'vendorid' => 4,
        'isrc'     => 5,
        'upc'      => 6,
        'artist'   => 7,
        'track'    => 8,
        'album'    => 9,
        'country'  => 12,
        'units'    => 14,
        'price'    => 15,
    },

    5 => {
        'vendorid' => 3,
        'isrc'     => 4,
        'upc'      => 5,
        'artist'   => 6,
        'track'    => 7,
        'country'  => 10,
        'units'    => 12,
        'price'    => 13,
    },
);

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 $retval = undef;
    my $lines  = $args{lines};

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

    return undef unless ( $lines && $version );
    my $offsets = $field_map{$version};

    if ($lines) {
        my $dateBegin;
        my $dateEnd;

        if ( $version == 5 ) {
            ## version 5 has no header, must get dates from filename
            $fileName =~ m/([01]\d)(\d\d)/;
            my $year  = 2000 + $2;
            my $month = $2;
            $dateBegin = $year . "-" . $month . "-01";
            $dateEnd = $year . "-" . $month . "-" . Days_in_Month( $year, $month );

            unless ( $dateBegin && $dateEnd ) {
                $self->errstr("couldn't get dates");
                print STDERR "filename: $fileName\n";
                return undef;
            }
        }

        my $linenum = 1;
        foreach my $line (@$lines) {
            if ( $line->[FIELD_RECTYPE] eq 'H' ) {

                # standard napster date format matches ours, but run through normalize
                # in case somebody decorated it along the way
                $dateBegin = Common::Util::normalize_date( $line->[FIELD_DATEBEGIN] ) || $line->[FIELD_DATEBEGIN];
                $dateEnd   = Common::Util::normalize_date( $line->[FIELD_DATEEND] )   || $line->[FIELD_DATEEND];
            } elsif ( $line->[FIELD_RECTYPE] eq 'D' ) {
                my $prodtype;

                if ( $version == 5 ) {
                    $prodtype = RPS::File::Sale::TYPE_TRACK;
                } else {    ## version 1
                    if ( lc( $line->[ $offsets->{'prodtype'} ] ) eq 'album' ) {
                        $prodtype = RPS::File::Sale::TYPE_ALBUM;
                    } elsif ( lc( $line->[ $offsets->{'prodtype'} ] ) eq 'track' ) {
                        $prodtype = RPS::File::Sale::TYPE_TRACK;
                    }
                }
                my $upc      = Common::Util::normalize_upc( $line->[ $offsets->{'upc'} ] );
                my $isrc     = Common::Util::normalize_isrc( $line->[ $offsets->{'isrc'} ] );
                my $artist   = $line->[ $offsets->{'artist'} ];
                my $album    = $line->[ $offsets->{'album'} ] unless ( $version == 5 );         ## no album in vers. 5
                my $track    = $line->[ $offsets->{'track'} ];
                my $format   = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $vendorid = $line->[ $offsets->{'vendorid'} ];
                my ($units)  = $line->[ $offsets->{'units'} ] =~ m/^(-?\d+)$/;
                my ($price) = $line->[ $offsets->{'price'} ] =~ m/^(\d*\.?\d*)$/ unless ( $version == 5 );
                my $countryCode = $line->[ $offsets->{'country'} ];
                my $currCode    = "USD";

                #				my $currConv	= 1;
                if ( $countryCode ne 'US' ) {
                    $currCode = $Common::Consts::CURRENCY_CODE{$countryCode};

                    #					$currConv = 0;
                }

                if ( $prodtype && ( $price || $version == 5 ) && ( $album || $track ) ) {
                    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);

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

    return $retval;
}

#
# Private methods
#

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