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

use RPS::Import::WMG::Util;

use constant DATE_ROW   => 0;
use constant DATE_COL   => 1;
use constant TRACK      => 2;
use constant ARTIST     => 3;
use constant ISRC       => 4;
use constant UNITS      => 5;
use constant PRICE_TYPE => 8;
use constant PRICE      => 9;

#public methods

sub _importLines
{
    my $self = shift;
    my %args = @_;
    my $fileObj = $args{file};
    my $lines = $args{lines};

    my $prodType = RPS::File::Sale::TYPE_TRACK;
    my $formatID = RPS::File::Sale::FORMAT_RINGTONE;

    my @billTo = RPS::Import::WMG::Util::getWirelessBillto($self->{fileServiceID});
    if ($billTo[0])
    {
        unless (RPS::Import::WMG::Util::getWirelessSellto($self->{fileServiceID}, $billTo[0]))
        {
            $self->warning();
            print STDERR "unknown sellto mapping: $billTo[0]\n";
        }
    }
    else
    {
        $self->warning();
        print STDERR "unknown billto mapping: $self->{fileServiceID}\n";
    }

    $fileObj->Atomic(1);
    $fileObj->Save();

    my $linenum;
    my ($dateBegin, $dateEnd) = $self->_getDates($lines->[DATE_ROW][DATE_COL]);

    unless ($dateBegin && $dateEnd)
    {
        $self->errstr("couldn't get dates");
        print STDERR 'date cell: ' . $lines->[DATE_ROW][DATE_COL] ."\n";
        return 0;
    }

    foreach my $line(@$lines)
    {
        my $track       = $line->[TRACK];
        my $artist      = $line->[ARTIST];
        my $isrc        = Common::Util::normalize_isrc($line->[ISRC]);
        my $units       = $self->numeric($line->[UNITS]);
        my $price       = $self->numeric($line->[PRICE]);
        my $priceType   = $line->[PRICE_TYPE];

        if ($track && $artist && $units && $price)
        {
            $price /= $units;

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

            $sale->artistName($artist);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->formatType($formatID);
            $sale->isrc($isrc);
            $sale->lineNum($linenum);
            $sale->price($price);
            $sale->productType($prodType);
            $sale->priceType($priceType);
            $sale->trackName($track);
            $sale->units($units);

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

#
# Private methods
#

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

    # January 2007
    return undef unless ($dateCell =~ m/(\w+)\W+(\d\d\d\d)/);
    my $year = $2;
    my $month = Decode_Month($1);

    return (
        sprintf("%04d-%02d-01", $year, $month),
        sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month))
    );
}

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