package RPS::Import::LaLa;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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

use lib '/app/tools/data_classes/lib';
use Client::Service;

my %field_map = (
    1 => {

        #'distributor' => 0,
        'date_begin' => 1,
        'date_end'   => 2,
        'isrc'       => 4,
        'upc'        => 5,
        'label'      => 6,
        'artist'     => 7,
        'track'      => 8,
        'album'      => 9,
        'units'      => 10,
    },
    2 => {

        #'distributor' => 0,
        'date_begin'  => 1,
        'date_end'    => 2,
        'isrc'        => 3,
        'upc'         => 4,
        'album_track' => 5,
        'label'       => 6,
        'artist'      => 7,
        'track'       => 8,
        'album'       => 9,
        'units'       => 10,
        'revenue'     => 11,
    },
);

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 $fileName = $fileObj->OrigFileName;
    my $version  = $fileObj->VersionNum;

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

    my $offsets = $field_map{$version};

    my $countryCode  = 'US';
    my $currencyCode = 'USD';
    my $format;

    if ( $fileName =~ /Download/i ) {
        $format = RPS::File::Sale::FORMAT_DOWNLOAD;
    } else {
        $format = RPS::File::Sale::FORMAT_STREAM;
    }

    my %errors  = ();
    my $linenum = 1;

    foreach my $line (@$lines) {

        my $prodtype;

        # setting this to track by default
        #
        my $albumOrTrack = 0;

        if ( defined( $line->[ $offsets->{'album_track'} ] ) ) {
            $albumOrTrack = $line->[ $offsets->{'album_track'} ];
        }

        if ( $albumOrTrack == 1 ) {
            $prodtype = RPS::File::Sale::TYPE_ALBUM;
        } else {
            $prodtype = RPS::File::Sale::TYPE_TRACK;
        }

        my $dateBegin = $line->[ $offsets->{'date_begin'} ];
        $dateBegin = substr( $dateBegin, 0, 4 ) . "-" . substr( $dateBegin, 4, 2 ) . "-" . substr( $dateBegin, 6, 2 );
        my $dateEnd = $line->[ $offsets->{'date_end'} ];
        $dateEnd = substr( $dateEnd, 0, 4 ) . "-" . substr( $dateEnd, 4, 2 ) . "-" . substr( $dateEnd, 6, 2 );

        #my $distributor	= $line->[$offsets->{'distributor'}];
        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'} ];
        my $track   = $line->[ $offsets->{'track'} ];
        my $label   = $line->[ $offsets->{'label'} ];
        my ($units) = $line->[ $offsets->{'units'} ] =~ m/^(-?\d+)$/;
        my $revenue = $line->[ $offsets->{'revenue'} ];
        my $price;

        if ($revenue) {
            if ( $revenue > 0 ) {
                $price = $revenue / $units;
            }
        }

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

        if ( $prodtype && $units && ( $artist || $album || $track ) ) {

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

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

            #$sale->distributor($distributor);
            $sale->upc($upc);
            $sale->isrc($isrc);
            $sale->artistName($artist);
            $sale->albumName($album);
            $sale->trackName($track);
            $sale->labelName($label);
            $sale->units($units);
            $sale->price($price);
            $sale->currencyCode($currencyCode);
            $sale->countryCode($countryCode);
            $sale->lineNum($linenum);

            $self->_insertSale( sale => $sale );
        }
        $linenum++;
    }

    return $linenum;
}

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