package RPS::Import::TrackItDown;

use strict;

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Consts;
use Date::Calc qw(Days_in_Month Decode_Month);

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

# map version num to the field order
my %fieldMap = (
    1 => {
        label              => 0,
        service_product_id => 1,
        catalog_number     => 2,
        track              => 3,
        artist             => 4,
        isrc               => 6,
        upc                => 7,
        units              => 8,
        price              => 9,
    },
    2 => {
        label              => 0,
        service_product_id => 1,
        catalog_number     => 2,
        track              => 3,
        artist             => 4,
        isrc               => 6,
        upc                => 7,
        units              => 9,
        price              => 10,
    },
);

use base 'RPS::Import::Importer';

#public methods

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

    my $lines    = $args{lines};
    my $fileObj  = $args{file};
    my $version  = $args{file}->VersionNum();
    my $fileName = $fileObj->OrigFileName;

    return undef unless ( defined $lines && defined $version );

    my $offsets = $fieldMap{$version};
    my ( $dateBegin, $dateEnd ) = _getDates($fileName);

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

    my $linenum = 1;
    foreach my $line (@$lines) {
        my $isrc              = $line->[ $offsets->{isrc} ];
        my $upc               = $line->[ $offsets->{upc} ];
        my $artist            = $line->[ $offsets->{artist} ];
        my $track             = $line->[ $offsets->{track} ];
        my $label             = $line->[ $offsets->{label} ];
        my $client_product_id = $line->[ $offsets->{catalog_number} ];

        my $units       = $line->[ $offsets->{units} ];
        my $price       = $line->[ $offsets->{price} ];
        my $currency    = "GBP";
        my $country     = "GB";
        my $format_type = RPS::File::Sale::FORMAT_DOWNLOAD;
        my $date        = $line->[ $offsets->{date} ];
        $price /= $units if ( $units > 1 );
        my $header = ( lc($label) eq 'recordlabel' );

        if ( $units && $price && $artist && $track && !$header ) {

            my $sale = RPS::Import::SaleRec->new();

            $sale->formatType($format_type);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->clientProductID($client_product_id);
            $sale->artistName($artist);
            $sale->labelName($label);

            if ( !$isrc ) {
                $sale->albumName($track);
                $sale->productType(RPS::File::Sale::TYPE_ALBUM);
            } else {
                $sale->trackName($track);
                $sale->productType(RPS::File::Sale::TYPE_TRACK);
            }

            $sale->isrc($isrc);
            $sale->units($units);
            $sale->price($price);
            $sale->upc($upc);
            $sale->countryCode($country);
            $sale->currencyCode($currency);
            $sale->lineNum($linenum);
            $self->_insertSale( sale => $sale );
        }

        #else { print STDERR "Skip-> l:".$linenum." u:".$units." p:".$price." ar: ".$artist."<-Skip\n";}
        $linenum++;
    }
    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my $date = shift;

    my $beginDate;
    my $endDate;

    if ( $date =~ m/(\d{1,2})\-(\d{4})/ ) {
        my $year  = $2;
        my $month = $1;
        $beginDate = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        $endDate = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
        return ( $beginDate, $endDate );
    }
    return undef;
}

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