package RPS::Import::AOL_Stream;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_upc normalize_isrc);
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';

# version mapping
my %field_map = (
    3 => {
        label  => 0,
        artist => 1,
        track  => 2,
        upc    => 3,
        isrc   => 4,
        units  => 5,
    },
);

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

    my $lines    = $args{lines};
    my $fileObj  = $args{file};
    my $fileName = $fileObj->OrigFileName;

    #my $fileName = $args{OrigFileName}; # for cmd-line testing

    my $map = $field_map{ $fileObj->VersionNum };

    #my $map = $field_map{$args{version}}; # for cmd-line testing

    return undef unless $lines;
    my ( $dateBegin, $dateEnd ) = $self->_getDates($fileName);

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

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

        my $format    = RPS::File::Sale::FORMAT_STREAM;              # was FORMAT_VIDEOSTREAM (FB1324)
        my $mediaType = RPS::DB::Item::MediaType::kMediaTypeVideo;

        my $upc     = $line->[ $map->{upc} ];
        my $isrc    = $line->[ $map->{isrc} ];
        my $artist  = $line->[ $map->{artist} ];
        my $track   = $line->[ $map->{track} ];
        my $label   = $line->[ $map->{label} ];
        my ($units) = $line->[ $map->{units} ] =~ m/^(\d+)$/;

        if ( $units >= 0 && ( $artist || $track ) ) {

            $upc  = normalize_upc($upc);
            $upc  = '' if ( length $upc < 12 || $upc !~ /^\d+$/ );
            $isrc = normalize_isrc($isrc);
            $isrc = '' if ( length $isrc < 10 || $isrc !~ /^[A-Z]+\d+/ );

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

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->mediaType($mediaType);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->upc($upc);
            $sale->isrc($isrc);
            $sale->artistName($artist);
            $sale->trackName($track);
            $sale->labelName($label);
            $sale->units($units);
            $sale->lineNum($linenum);

            #$args{dumper}($sale);
            $self->_insertSale( sale => $sale );
        }    #else { print STDERR "skipped $linenum\n", join(',', @$line), "\n"; }
        $linenum++;
    }

    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my $self  = shift;
    my $fName = shift;
    my ( $month, $year );

    if ( $fName =~ m/(\d+)-(\d+)/ ) {
        ( $month, $year ) = ( $1, $2 );
    } elsif ( $fName =~ m/([a-zA-Z]{3})-(\d+)/ ) {
        ( $month, $year ) = ( $1, $2 );
        $month = Decode_Month($month);
    } elsif ( $fName =~ m/\W([a-zA-Z]+)\W(\d{4})\./ ) {
        ( $month, $year ) = ( $1, $2 );
        $month = Decode_Month($month);
    }

    $year += 2000 unless ( $year > 1999 );

    return undef unless ( $month && $year );

    return ( sprintf( "%04d-%02d-%02d", $year, $month, 1 ), sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) ), );
}

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