package RPS::Import::WiderThan;

use strict;


use Date::Calc qw(Decode_Month);

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

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 constant DATE_COL => 0;
use constant DATE_ROW => 2;

use constant FIELD_TITLE  => 0;
use constant FIELD_ARTIST => 1;
use constant FIELD_ISRC   => 2;
use constant FIELD_FORMAT => 3;
use constant FIELD_RETAIL => 4;
use constant FIELD_UNITS  => 5;
use constant FIELD_PRICE  => 6;

#public methods

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

	my $retval = undef;
    my $lines = $args{lines};
    my $fileObj = $args{file};

    if ($self->{clientID} == RPS::Import::Importer::WMG) {
        require RPS::Import::WMG::Util;
        $fileObj->Atomic(1);
        $fileObj->Save();
    }

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

    my $service     = undef;
    my $past_header = undef;

    my $linenum = 1;
    foreach my $line(@$lines)
    {
        unless ($past_header) {
            if ($line->[FIELD_TITLE] eq 'Title' && $line->[FIELD_ARTIST] eq 'Artist' && 
                $line->[FIELD_ISRC] eq 'ID'     && $line->[FIELD_FORMAT] eq 'Format') {
                $past_header = 1;
            }
            $linenum++;
            next;
        }

        if ($line->[FIELD_TITLE] ne '' && $line->[FIELD_ARTIST] eq '' && 
            $line->[FIELD_ISRC] eq '' && $line->[FIELD_FORMAT] eq '') {
            if ($line->[FIELD_TITLE] eq 'DOBSON') {
                $service = Client::Service::DSP_DOBSON;
            } elsif ($line->[FIELD_TITLE] eq 'IDT') {
                $service = Client::Service::DSP_IDT;
            } elsif ($line->[FIELD_TITLE] eq 'Dig-a-Dub') {
                $service = Client::Service::DSP_DIGADUB;
            } else {
                $service = undef;
            }
            $linenum++;
            next;
        }

        my $prodtype;
        if ($line->[FIELD_FORMAT] eq 'T') {
            $prodtype = RPS::File::Sale::TYPE_TRACK;
        }

        my $track  = $line->[FIELD_TITLE];
        my $artist = $line->[FIELD_ARTIST];
        my $isrc   = $line->[FIELD_ISRC];

        my $format = RPS::File::Sale::FORMAT_RINGTONE;  # was FORMAT_MASTERTONE (FB1324)
        my $format_name = "mastertone";
        my $priceType;

        if ($self->{clientID} == RPS::Import::Importer::WMG) {
            $priceType = RPS::Import::WMG::Util::getWirelessPriceType($format_name);
        }

        my ($retail)= $line->[FIELD_RETAIL] =~ m/^(-?\d*\.?\d*)$/;
        my ($units)	= $line->[FIELD_UNITS] =~ m/^(-?\d+)$/;
        my ($price)	= $line->[FIELD_PRICE] =~ m/^(-?\d*\.?\d*)$/;

        $price = ($units != 0)?($price / $units):0;

        if ($service && $prodtype && $track && $artist && $units && $price)
        {

            my $ppd = $retail ? $price / $retail : 0;
            my $sale = RPS::Import::SaleRec->new();
 
            $sale->serviceID($service);
            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->artistName($artist);
            $sale->trackName($track);
            $sale->isrc($isrc);
            $sale->units($units);
            $sale->price($price);
            $sale->priceType($priceType) if ($self->{clientID} == RPS::Import::Importer::WMG);
            $sale->wholesaleRate($ppd);
            $sale->retail($retail);
            $sale->lineNum($linenum);
    
            ##$args{dumper}($sale);  ## for cmd-line testing
            $self->_insertSale(sale => $sale);
        }
        $linenum++;
    }
	return $linenum;
}

#
# Private methods
#


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

    my ($dateBegin, $dateEnd);

    if ($date =~ m/^From (\w+) (\d+) to (\w+) (\d+), (\d{4})$/) {

        my $month1 = Decode_Month($1);
        my $day1   = $2;
        my $month2 = Decode_Month($3);
        my $day2   = $4;
        my $year   = $5;

        $dateBegin = sprintf("%04d-%02d-%02d", $year, $month1, $day1);
        $dateEnd   = sprintf("%04d-%02d-%02d", $year, $month2, $day2);
    
    } else {
        return undef;
    }

	return ($dateBegin, $dateEnd);
}

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