package RPS::Import::WMG::MusicChoiceWireless;

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

my %field_map = (
    11 => {
        service => 0,
        units   => 2,
        artist  => 3,
        track   => 4,
        label   => 5,
    },
    12 => {
        service => 0,
        track   => 1,
        artist  => 2,
        units   => 3,
        label   => 4,
    },
);

#public methods

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

    my $version  = $self->_version();
    my $fileObj  = $args{file};
    my $fileName = $fileObj->OrigFileName;

    my $offsets = $field_map{$version};
    my $lines   = $args{lines};
    return 0 unless ($lines);

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

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

    my %errors  = ();
    my $linenum = 1;
    foreach my $line (@$lines) {
        my $service = $line->[ $offsets->{service} ];
        my ($units) = $line->[ $offsets->{units} ] =~ m/^(-?\d+)$/;
        my $artist  = $line->[ $offsets->{artist} ];
        my $track   = $line->[ $offsets->{track} ];
        my $label   = $line->[ $offsets->{label} ];

        if ( $units && $artist && $track ) {
            my $serviceID = $self->getServiceID($service);
            unless ($serviceID) {
                unless ( $errors{$service} ) {
                    $self->errstr('unknown service');
                    print STDERR "can't map service: '$service'\n";
                    $errors{$service} = 1;
                }
                $linenum++;
                next;
            }

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

            $sale->artistName($artist);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->formatType($format);
            $sale->mediaType($mediaType);
            $sale->lineNum($linenum);
            $sale->labelName($label);
            $sale->productType($prodType);
            $sale->serviceID($serviceID);
            $sale->trackName($track);
            $sale->outlet(12);
            $sale->units($units);

            #$args{dumper}($sale);  ## for cmd-line testing
            $self->_insertSale( sale => $sale );
        }    #else { print STDERR "skip ($linenum) u: $units p: $price a: $artist t: $track\n" }
        $linenum++;
    }
    return $linenum;
}

#
# Private methods
#

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

    my ( $year, $month );
    if ( $fName =~ m/\W(\w{3})\W(\d\d)\D+/ )    # MusicChoice MAR 07 Detailed Report
    {
        ( $month, $year ) = ( $1, $2 );
        $year += 2000;
    } elsif ( $fName =~ m/\D(\d{4})\D\d+\D(\w{3})\W/ )    # MusicChoice 2007 12 SEP Detailed Report
    {
        ( $year, $month ) = ( $1, $2 );
    } elsif ( $fName =~ m/\D(\d+)\D(\w{3})\W/ )           # MusicChoice MAR 07 Detailed Report
    {
        ( $year, $month ) = ( $1, $2 );
        $year += 2000 if ( $year < 2000 );
    }

    $month = Decode_Month($month);

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

    return (
        Common::Util::normalize_date( join( '-', $year, $month, 1 ) ),
        Common::Util::normalize_date( join( '-', $year, $month, Days_in_Month( $year, $month ) ) ),
    );
}

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