package RPS::Import::AudioLunchbox;

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 RPS::Import::Importer;
use base 'RPS::Import::Importer';

#private constants
use constant FIELD_LABEL    => 0;
use constant FIELD_DATE     => 1;
use constant FIELD_PRODTYPE => 3;
use constant FIELD_VENDORID => 4;
use constant FIELD_UPC      => 5;
use constant FIELD_ISRC     => 6;
use constant FIELD_CATNO    => 7;
use constant FIELD_ARTIST   => 8;
use constant FIELD_TITLE    => 9;
use constant FIELD_PRICE_V2 => 10;
use constant FIELD_PRICE_V1 => 11;

my %field_map = (
    1 => {
        label    => 0,
        date     => 1,
        prodtype => 3,
        vendorid => 4,
        upc      => 5,
        isrc     => 6,
        catno    => 7,
        artist   => 8,
        title    => 9,
        price    => 11,
    },
    2 => {
        label    => 0,
        date     => 1,
        prodtype => 3,
        vendorid => 4,
        upc      => 5,
        isrc     => 6,
        catno    => 7,
        artist   => 8,
        title    => 9,
        price    => 10,
    },
);

#public methods

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

    my $retval  = undef;
    my $lines   = $args{lines};
    my $version = $args{file}->VersionNum();
    ##my $version = $args{version}; # for cmd-line testing
    my $offsets = $field_map{$version};

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

        ( $dateBegin, $dateEnd ) = $self->_getDates( $lines, $offsets );
        unless ( $dateBegin && $dateEnd ) {
            $self->errstr("couldn't get dates");
            return undef;
        }

        my $linenum = 1;
        foreach my $line (@$lines) {
            if ( $linenum > 1 ) {
                my $prodtype;
                my $upc = Common::Util::normalize_upc( $line->[ $offsets->{upc} ] );
                $upc =~ s/^N\/A$//;
                my $isrc = Common::Util::normalize_isrc( $line->[ $offsets->{isrc} ] );
                $isrc =~ s/^N\/A$//;
                my $artist = $line->[ $offsets->{artist} ];
                my $title  = $line->[ $offsets->{title} ];
                my $album;
                my $track;

                if ( lc( $line->[ $offsets->{prodtype} ] ) eq 'album' ) {
                    $prodtype = RPS::File::Sale::TYPE_ALBUM;
                    $album    = $title;
                } elsif ( lc( $line->[ $offsets->{prodtype} ] ) eq 'track' ) {
                    $prodtype = RPS::File::Sale::TYPE_TRACK;
                    $track    = $title;
                }
                my $label    = $line->[ $offsets->{label} ];
                my $format   = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $vendorid = $line->[ $offsets->{vendorid} ];
                my $units    = 1;
                my ($price)  = $line->[ $offsets->{price} ] =~ m/^(\d*\.?\d*)$/;

                # no special flag for free tracks so we have to go by price
                my $free = $price == 0 ? 1 : 0;

                if ( $prodtype && $artist && $title && $price >= 0 ) {
                    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->labelName($label);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->lineNum($linenum);
                    $sale->free($free);
                    $sale->countryCode('US');

                    $self->_insertSale( sale => $sale );

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

    return $retval;
}

#
# Private methods
#

sub _getDates {
    my $self    = shift;
    my $lines   = shift;
    my $offsets = shift;

    my $dateBegin;
    my $dateEnd;

    foreach my $line (@$lines) {
        my $date = Common::Util::normalize_date( $line->[ $offsets->{date} ] );

        if ($date) {
            if ( $date < $dateBegin || !$dateBegin ) {
                $dateBegin = $date;
            }

            if ( $date > $dateEnd || !$dateEnd ) {
                $dateEnd = $date;
            }
        }
    }

    if ($dateBegin) {
        substr( $dateBegin, -2 ) = "01";
    }

    if ($dateEnd) {
        substr( $dateEnd, -2 ) = Days_in_Month( substr( $dateEnd, 0, 4 ), substr( $dateEnd, 5, 2 ) );
    }

    return ( $dateBegin, $dateEnd );
}

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