package RPS::Import::Verizon;

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

#private constants
use constant FIELD_LABEL		=> 1;
use constant FIELD_TRACK		=> 2;
use constant FIELD_ARTIST		=> 3;
use constant FIELD_ISRC			=> 4;
use constant FIELD_UNITS		=> 5;

use lib '/app/tools/sale_import/lib';
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 $sheets = $args{sheets};
    my $fileObj = $args{file};
    my $version = $fileObj->VersionNum;

    if ($version == 2) {
        $lines = $sheets->[1];
    }

    return 0 unless $lines;
    my ($dateBegin, $dateEnd) = $self->_getDates($lines->[0][1]);

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

    my $prodtype = RPS::File::Sale::TYPE_TRACK;
    my $formatTypeDefault = RPS::File::Sale::FORMAT_RINGTONE;
    my $formatNameDefault = 'realtone';
    my $priceTypeDefault;

    if ($self->{clientID} == RPS::Import::Importer::WMG) # warner
    {
        $formatTypeDefault = RPS::File::Sale::FORMAT_RINGTONE; # was FORMAT_MASTERTONE (FB1324)
        # check for warner specific stuff
        require RPS::Import::WMG::Util;
     
        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";
        }

        unless ($priceTypeDefault = RPS::Import::WMG::Util::getWirelessPriceType($formatNameDefault))
        {
            $self->warning();
            print STDERR "unknown format mapping: $formatNameDefault\n";
        }

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

    my $linenum = 1;
    my $seenHeader;
    my %errors = ();
    foreach my $line(@$lines)
    {
        if ($seenHeader)
        {
            my $format      = $formatTypeDefault;
            my $priceType   = $priceTypeDefault;
            my $isrc		= Common::Util::normalize_isrc($line->[FIELD_ISRC]);
            my $artist		= $line->[FIELD_ARTIST];
            my $track		= $line->[FIELD_TRACK];
            my $label		= $line->[FIELD_LABEL];
            my ($units)		= $line->[FIELD_UNITS] =~ m/^(-?\d*,*\d,*\d*)$/;
            $units =~ s/,//g;

            if ($units && $artist && $track)
            {
                if ($track =~ m/voice\s*ringer/i) # voice ringers mixed in
                {
                    $format = RPS::File::Sale::FORMAT_RINGTONE; # was FORMAT_VOICERINGER (FB1324)
                    if ($self->{clientID} == RPS::Import::Importer::WMG) # warner
                    {
                        unless ($priceType = RPS::Import::WMG::Util::getWirelessPriceType('voiceringer'))
                        {
                            unless ($errors{$format})
                            {
                                $self->warning();
                                print STDERR "unknown format mapping: $format\n";
                                $errors{$format} = 1;
                            }
                        }
                    }
                }

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

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

                $self->_insertSale(sale => $sale);
            }
        }
        else
        {
            if ($line->[FIELD_LABEL] =~ m/LBL/i &&
                $line->[FIELD_TRACK] =~ m/TITLE/i &&
                $line->[FIELD_ARTIST] =~ m/ARTIST/i &&
                $line->[FIELD_ISRC] =~ m/ISRC/i)
            {
                $seenHeader = 1;
            }
        }
        $linenum++;
    }

	return $linenum;
}

#
# Private methods
#


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

	my $dateBegin;
	my $dateEnd;

	my %months = (Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6,
				  Jul => 7, Aug => 8, Sep => 8, Oct => 10, Nov => 11, Dec => 12); 

	my ($month, $year) = $dateCell =~ m/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w* (20\d\d)/i;

	if ($year && $month)
	{
		$month = $months{$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.
###
