package RPS::Import::WMG::MusicMatchStream;

use strict;


use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Consts;

use lib '/app/tools/data_classes/lib';

# map version_num to the field order
my %field_map = (
                  5 => {
                        'product_id'=> 1,
                        'units'        => 6,
                        'artist'    => 9,
                        'track'        => 10,
                        'isrc'        => 14,
                        },
                 );

use lib '/app/tools/rps/lib';
use RPS::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

#public methods

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

    my $retval = undef;
    my $lines = $args{lines};
    my $fileObj = $args{file};
    my $version = $fileObj->VersionNum;
    #my $version = $args{version};

    return undef unless ($lines);
    unless ($version && exists $field_map{$version})
    {
        $self->errstr("invalid version ($version)");
        return undef;
    }
    my ($dateBegin, $dateEnd) = $self->_getDates($lines->[0][2], $lines->[0][3]);
    unless ($dateBegin && $dateEnd)
    {
        $self->errstr('unable to get dates');
        return undef;
    }

    my $offsets = $field_map{$version};

    my $linenum = 1;
    foreach my $line(@$lines)
    {
        if ($linenum > 1)
        {
            my $prodtype    = RPS::File::Sale::TYPE_TRACK;
            my $format        = RPS::File::Sale::FORMAT_STREAM;
            my $product_id  = $line->[$offsets->{'product_id'}];
            my $isrc        = Common::Util::normalize_isrc($line->[$offsets->{'isrc'}]);
            my $artist        = $line->[$offsets->{'artist'}];
            my $track        = $line->[$offsets->{'track'}];
            my $units        = $line->[$offsets->{'units'}];

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

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

                #$args{dumper}($sale);
                $self->_insertSale(sale => $sale);
            }
        }
        $linenum++;
    }

    return $linenum;
}

#
# Private methods
#


sub _getDates
{
    my $self = shift;
    my ($dateBegin, $dateEnd) = @_;

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

    if ($year && $month)
    {
        $dateBegin = sprintf("%04d-%02d-%02d", $year, $month, 1);
        $dateEnd = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month));
    }

    return ($dateBegin, $dateEnd);
}

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