package RPS::Import::WMG::Warner;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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

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

use constant FIELD_PRODUCT  => 0;
use constant FIELD_TRACK    => 1;
use constant FIELD_ALBUM    => 3;
use constant FIELD_ARTIST   => 4;
use constant FIELD_UPC      => 5;
use constant FIELD_ISRC     => 6;
use constant FIELD_UNITS    => 8;
use constant FIELD_PRICE    => 9;
use constant FIELD_FORMAT   => 10;
use constant FIELD_LABEL    => 11;

#
# public methods
#

sub _importLines
{
    my $self = shift;
    my %args = @_;
    my $fileName = $args{file}->OrigFileName;
    #my $fileName = $args{OrigFileName}; # for cmd-line testing
    my $sheets = $args{sheets};

    return undef unless ($sheets);

    my ($dateBegin, $dateEnd) = _get_dates($fileName);

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

    my %errors = ();
    my $linenum;
    foreach my $lines(@$sheets)
    {
        $linenum = 1;

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

            my $prod_id = $line->[FIELD_PRODUCT];
            my $upc     = $line->[FIELD_UPC];
            my $isrc    = $line->[FIELD_ISRC];
            my $artist  = $line->[FIELD_ARTIST];
            my $album   = $line->[FIELD_ALBUM];
            my $track   = $line->[FIELD_TRACK];
            my $label   = $line->[FIELD_LABEL];
            my ($units) = $line->[FIELD_UNITS];
            my ($price) = $line->[FIELD_PRICE];

            if (($upc || $artist) && $track && $units)
            {
                $price /= $units;
                $price      *= -1 if($units<0 && $price<0);

                my $svc_format = uc($line->[FIELD_FORMAT]);
                unless ($svc_format =~ /STR|PLAYS|DL/)
                {
                    if ($svc_format)
                    {
                        print STDERR "unknown format ($svc_format) l:".$linenum."\n" unless $errors{$svc_format};
                        $errors{$svc_format} = 1;
                    }
                    $linenum++;
                    next;
                }
                my $format = $svc_format =~ /^DL$/ ? RPS::File::Sale::FORMAT_TETHERED : RPS::File::Sale::FORMAT_STREAM;
                my $free = ($price == 0 && $svc_format eq 'STRFTR') ? 1 : 0;

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

                $sale->productType($prodtype);
                $sale->formatType($format);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->serviceProductID($prod_id);
                $sale->upc($upc);
                $sale->isrc($isrc);
                $sale->artistName($artist);
                $sale->albumName($album);
                $sale->trackName($track);
                $sale->labelName($label);
                $sale->units($units);
                $sale->price($price);
                $sale->free($free);
                $sale->lineNum($linenum);

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

#
# Private methods
#

sub _get_dates
{
    my $fName = shift;

    my ($month1, $day1, $year1, $month2, $day2, $year2);
    if ($fName =~ m/\w+\s*_(\d\d)(\d\d)(\d{4})_(\d\d)(\d\d)(\d{4})_?/)
    {
        ($month1, $day1, $year1, $month2, $day2, $year2) = ($1, $2, $3, $4, $5, $6);
    }
    # Warner Standard Music Report-LAUNCHcast-A la Carte April 2003.xls
    elsif ($fName =~ m/([a-z]+)\D(\d{4})\D/i)
    {
        ($month1, $year1) = ($1, $2);
        $year2 = $year1;
        $month1 = $month2 = Decode_Month($month1);
    }
    # Warner_Video_Report-200511.xls
    elsif ($fName =~ m/\D(\d{4})(\d\d)\D/)
    {
        ($year1, $month1) = ($1, $2);
        $year2 = $year1;
        $month2 = $month1;
    }
    # Musicnow - 2005 6 MAR Datafile (REVISED).TAB
    elsif ($fName =~ m/\D(\d{4})[^a-zA-Z]+([a-zA-Z]+)/)
    {
        ($year1, $month1) = ($1, $2);
        $year2 = $year1;
        $month1 = $month2 = Decode_Month($month1);
    }
    # RUCKUS_Dec04.TAB
    elsif ($fName =~ m/([a-z]+)(\d{2})\D/i)
    {
        ($month1, $year1) = ($1, $2);
        $month1 = $month2 = Decode_Month($month1);
        $year1 += $year1 < 90 ? 2000 : 1900;
        $year2 = $year1;
    }

    return unless ($month1 && $year1 && $month2 && $year2);

    return (
        sprintf("%04d-%02d-01", $year1, $month1), 
        sprintf("%04d-%02d-%02d", $year2, $month2, Days_in_Month($year2, $month2))
    );
}

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