package RPS::Import::Insound;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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

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';

my %fieldMap = (
    1 => {
        date   => 0,
        upc    => 1,
        spid   => 2,
        artist => 3,
        title  => 4,
        price  => 5,
    },
    2 => {
        date   => 0,
        upc    => 1,
        artist => 2,
        title  => 3,
        price  => 4,
    },
);

#public methods

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

    my $fileObj  = $args{file};
    my $fileName = $fileObj->OrigFileName;
    my $version  = $args{file}->VersionNum();
    my $lines    = $args{lines};
    my $format   = RPS::File::Sale::FORMAT_DOWNLOAD;
    my $product  = RPS::File::Sale::TYPE_ALBUM;
    my ( $dateBegin, $dateEnd ) = $self->_getDates($fileName);
    unless ( $dateBegin && $dateEnd ) {
        $self->errstr("couldn't get dates");
        print STDERR "date from file_name: $fileName\n";
        return;
    }

    my $linenum = 1;

    my $offsets = $fieldMap{$version};

    foreach my $line (@$lines) {
        my $upc    = $line->[ $offsets->{upc} ];
        my $spid   = $line->[ $offsets->{spid} ] if ( defined $offsets->{spid} );
        my $artist = $line->[ $offsets->{artist} ];
        my $title  = $line->[ $offsets->{title} ];
        my $price  = $line->[ $offsets->{price} ];
        my $units  = 1;
        if ( $artist !~ /artist/ && $price != 0 && $title !~ /title/i && $title ne "" && $artist ne "" ) {

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

            $sale->albumName($title);
            $sale->artistName($artist);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->formatType($format);
            $sale->serviceProductID($spid) if ( defined $offsets->{spid} );
            $sale->lineNum($linenum);
            $sale->price($price);
            $sale->productType($product);
            $sale->units($units);
            $sale->upc($upc);
            $sale->countryCode('US');

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

    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my $self  = shift;
    my $fname = lc shift;

    my ( $month_b, $month_e, $year_b, $year_e );
    if ( $fname =~ m/\binsound\s+([a-z]+)(\d+)-([a-z]+)(\d+)/ ) {
        ( $year_b, $year_e ) = ( $2, $4 );

        $month_b = Decode_Month($1);
        $month_e = Decode_Month($3);
    } elsif ( $fname =~ m/\binsound\s+([a-z]+)(\d+)\./ ) {
        $month_b = $month_e = Decode_Month($1);
        $year_b  = $year_e  = $2;
    } else {
        return undef;
    }

    return unless ( $month_b && $month_e );
    $year_b += 2000 if ( $year_b < 2000 );
    $year_e += 2000 if ( $year_e < 2000 );

    my $last_day = Days_in_Month( $year_e, $month_e );

    my $format    = '%4d-%02d-%02d';
    my $beginDate = sprintf( $format, $year_b, $month_b, 1 );
    my $endDate   = sprintf( $format, $year_e, $month_e, $last_day );

    return ( $beginDate, $endDate );
}

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