package RPS::Import::WMG::MusicChoiceWirelessHistory;

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

use constant SVC    => 0;
use constant DATE   => 1;
use constant LABEL  => 3;
use constant ARTIST => 4;
use constant TRACK  => 5;
use constant UNITS  => 6;

#public methods

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

    my $version = $self->_version();

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

    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 ( $serviceID, $dateBegin, $dateEnd, $label );

    my $linenum = 1;
    foreach my $line (@$lines) {
        if ( $line->[SVC] && lc( $line->[SVC] ) !~ m/(total|\sorders|carrier)$/ ) {
            my $service = $line->[SVC];
            $serviceID = $self->getServiceID($service);
            unless ($serviceID) {
                $self->errstr('unknown service');
                print STDERR "service: $service\n";
                $linenum++;
                next;
            }
        }

        if ( $line->[DATE] =~ m/^\d+$/ ) {
            my $date = $line->[DATE];
            ( $dateBegin, $dateEnd ) = $self->_getDates($date);
            unless ( $dateBegin && $dateEnd ) {
                $self->errstr("couldn't get dates");
                print STDERR "date field: $date\n";
                return undef;
            }
        }

        if ( $line->[LABEL] && lc $line->[LABEL] ne 'label name' ) {
            $label = $line->[LABEL];
        }

        my $artist  = $line->[ARTIST];
        my $track   = $line->[TRACK];
        my ($units) = $line->[UNITS] =~ m/^(-?\d+)$/;

        if ( $units && $track && $artist !~ /\stotal$/i ) {
            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 $date = shift;

    my ( $year, $month );
    if ( $date =~ m/^(\d{4})(\d\d)$/ ) {
        ( $year, $month ) = ( $1, $2 );
    }

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

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