package RPS::Import::WMG::Batanga;

use strict;

use Date::Calc qw(Days_in_Month Decode_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';
use RPS::Import::WMG::Util;

my %field_map = (
    1 => {
        catalog => 0,
        title   => 1,
        artist  => 4,
        upc     => 5,
        isrc    => 6,
        units   => 8,
        price   => 9,
        format  => 10,
    },
);

my %format_map = ( stream => RPS::File::Sale::FORMAT_STREAM, );

#
# public methods
#

sub _importLines {
    my $self     = shift;
    my %args     = @_;
    my $fileObj  = $args{file};
    my $fileName = $args{file}->OrigFileName;
    my $sheets   = $args{sheets};
    my $version  = $fileObj->VersionNum;
    my $offsets  = $field_map{$version};
    my $product  = RPS::File::Sale::TYPE_TRACK;

    return undef unless ($sheets);

    my ( $dateBegin, $dateEnd ) = _get_dates($fileName);

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

    my %errors = ();
    my $linenum;
    foreach my $lines (@$sheets) {
        $linenum = 1;

        foreach my $line (@$lines) {
            my $prodtype = RPS::File::Sale::TYPE_TRACK;

            my $catalog = $line->[ $offsets->{catalog} ];
            my $upc     = $line->[ $offsets->{upc} ];
            my $isrc    = $line->[ $offsets->{isrc} ];
            my $artist  = $line->[ $offsets->{artist} ];
            my $track   = $line->[ $offsets->{title} ];
            my $units   = $line->[ $offsets->{units} ];
            my $price   = $line->[ $offsets->{price} ];
            my $format  = $format_map{ lc( $line->[ $offsets->{format} ] ) };
            $price =~ s/\$//;
            $price /= $units if ( $units != 0 );
            $price *= -1 if ( $units < 0 && $price < 0 );

            if ( ( $upc || $artist ) && $track && $units && $format ne "" ) {
                my $sale = RPS::Import::SaleRec->new();

                $sale->productType($product);
                $sale->formatType($format);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->serviceProductID($catalog);
                $sale->upc($upc);
                $sale->isrc($isrc);
                $sale->artistName($artist);
                $sale->trackName($track);
                $sale->units($units);
                $sale->price($price);
                $sale->lineNum($linenum);

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

            #else { print STDERR "skip l:".$linenum." a:".$artist." u:".$units." p:".$price." f:".$format." t:".$track." up:".$upc."\n"; }

            $linenum++;
        }
    }
    $self->warning() if ( keys %errors );
    return $linenum;
}

#
# Private methods
#

sub _get_dates {
    my $fName = shift;

    my ( $month1, $day1, $year1, $month2, $day2, $year2 );
    if ( $fName =~ m/\w+\s*_(\d\d)(\d\d)(\d{4})_(\d\d)(\d\d)(\d{4})_?/ ) {
        ( $month1, $day1, $year1, $month2, $day2, $year2 ) = ( $1, $2, $3, $4, $5, $6 );
    } elsif ( $fName =~ m/BATANGA_(\d\d)(\d\d)(\d{4})_(\d\d)(\d\d)(\d{4})_(\d\d)\.txt/ ) {
        ( $month1, $day1, $year1, $month2, $day2, $year2 ) = ( $1, $2, $3, $4, $5, $6 );
    }

    return unless ( $month1 && $year1 && $month2 && $year2 );

    return ( sprintf( "%04d-%02d-01", $year1, $month1 ), sprintf( "%04d-%02d-%02d", $year2, $month2, Days_in_Month( $year2, $month2 ) ) );
}

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