package RPS::Import::WMG::NineSquaredBounty;

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


my %field_map = (
    1 => {
        vendorid=> 0,
        units   => 3,
        price   => 8,
        isrc    => 11,
        album   => 12,
        artist  => 13,
    },
    2 => {
        vendorid=> 0,
        units   => 6,
        price   => 9,
        isrc    => 13,
        album   => 17,
        artist  => 23,
    },
    3 => {
        vendorid=> 1,
        units   => 8,
        price   => 11,
        isrc    => 13,
        album   => 16,
        artist  => 20,
    },
);

#public methods

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

    my $sheets = $args{sheets};
    return undef unless ($sheets);

    my $fileObj = $args{file};
    $fileObj->Atomic(1);
    $fileObj->Save();

    my $linenum;
    my $i = 0;
    foreach my $sheet(@$sheets)
    {
        my $version = $self->_getVersion($sheet);
        unless ($version)
        {
            print STDERR 'unknow version in ', $args{sheet_names}->[$i++], "\n";
            next;
        }
        print STDERR "version $version\n";

        my $offsets = $field_map{$version};

        my ($dateBegin, $dateEnd) = $self->_getDates($args{sheet_names}->[$i]);

        unless ($dateBegin && $dateEnd)
        {
            $self->errstr("couldn't get dates");
            print STDERR 'bad date in sheet name: ', $args{sheet_names}->[$i++], "\n";
            next;
        }

        $linenum = (++$i * 10000) + 1;
        foreach my $line (@$sheet)
        {

            my $vendorID = $line->[$offsets->{vendorid}];
            last if ($vendorID =~ m/disregard/i);

            my $album   = $line->[$offsets->{album}];
            my $artist  = $line->[$offsets->{artist}];
            my $isrc    = Common::Util::normalize_isrc($line->[$offsets->{isrc}]);
            my $price   = $self->numeric($line->[$offsets->{price}]);
            my $units   = $self->numeric($line->[$offsets->{units}]);

            if ($units && $price >= 0 && ($isrc || $artist))
            {
                $price /= $units;

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

                $sale->albumName($album);
                $sale->artistName($artist);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->formatType(RPS::File::Sale::FORMAT_RINGTONE); # was FORMAT_MASTERTONE (FB1324)
                $sale->isrc($isrc);
                $sale->lineNum($linenum);
                $sale->price($price);
                $sale->priceType('Standard/Bounty');
                $sale->productType(RPS::File::Sale::TYPE_TRACK);
                $sale->serviceProductID($vendorID);
                $sale->units($units);

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

#
# Private methods
#

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

    return undef unless ($sheetName =~ m/\b(\w+)[\W]+(\d\d)\b/);

    my $year  = $2 + 2000;
    my $month = Decode_Month($1);
    return undef unless ($month);

    my $dateBegin = sprintf("%04d-%02d-01", $year, $month);
    my $dateEnd   = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year,$month));

    return ($dateBegin, $dateEnd);
}

sub _getVersion
{
    my $self = shift;
    my $lines = shift;

    foreach my $line(@$lines)
    {
        next unless (lc $line->[0] eq 'campaignid' || lc $line->[1] eq 'campaignid');
        return
            lc $line->[3] eq 'subscribers' ? 1 :
            lc $line->[6] eq 'subscribers' ? 2 :
            lc $line->[8] eq 'subscribers' ? 3 :
            0;
    }
}

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