package RPS::Import::AOL;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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 = (
    1 => {
        dateRow => 3,
        dateCol => 0,
        artist  => 0,
        track   => 1,
        units   => 2,
        price   => 3,
    },
    2 => {
        dateRow => 3,
        dateCol => 1,
        artist  => 2,
        track   => 3,
        units   => 4,
        price   => 5,
    },
);

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

    my $lines   = $args{sheets}->[2];
    my $version = $args{file}->VersionNum;

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

    my $map = $field_map{$version};

    return undef unless $lines;
    my $dateStr = $lines->[ $map->{dateRow} ][ $map->{dateCol} ];
    my ( $dateBegin, $dateEnd ) = $self->_getDates($dateStr);

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

    my $prodtype = RPS::File::Sale::TYPE_TRACK;

    my $format;

    my $mediaType = RPS::DB::Item::MediaType::kMediaTypeVideo;

    #$format = $self->is_wmg ? RPS::File::Sale::FORMAT_VIDEOSTREAM :
    #RPS::File::Sale::FORMAT_VIDEO;
    $format =
      $self->is_wmg
      ? RPS::File::Sale::FORMAT_STREAM
      : RPS::File::Sale::FORMAT_DOWNLOAD;    # FB1324

    my $linenum = 1;
    foreach my $line (@$lines) {
        my $artist = $line->[ $map->{artist} ];
        my ($track) = $line->[ $map->{track} ] =~ m/(?:.+:+\s*)?(.*)$/;
        $track =~ s/^$artist\s//;
        $track =~ s/\s+feature$//i if ( $version == 2 );
        my ($units) = $line->[ $map->{units} ] =~ m/^(-?\d+)$/;
        my ($price) = $line->[ $map->{price} ] =~ m/^(\d*\.\d*)$/;
        if ( $price eq "" && $line->[ $map->{price} + 1 ] ne "" ) {
            $price = $line->[ $map->{price} + 1 ];
            $price /= $units if ( $units != 0 );
        }

        if ( $units && $price && $artist && $track ) {
            my $sale = RPS::Import::SaleRec->new();

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

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

    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my $self     = shift;
    my $dateCell = shift;

    my ( $month, $year ) = $dateCell =~ m/for the month of (\w+)\s(\d{4})$/i;
    $month = Decode_Month($month);

    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.
###
