package RPS::Import::VerizonPIX;
use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

use lib '/app/tools/data_classes/lib';

#private constants
use constant FIELD_TRACK  => 0;
use constant FIELD_GRID   => 1;
use constant FIELD_UNITS  => 3;
use constant FIELD_RETAIL => 4;
use constant FIELD_RATE   => 5;

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 $lines    = $args{lines};
    my $fileObj  = $args{file};
    my $fileName = $fileObj->OrigFileName();

    #my $fileName = $args{OrigFileName};

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

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

    my $prodType   = RPS::File::Sale::TYPE_TRACK;
    my $formatType = RPS::File::Sale::FORMAT_WALLPAPER;
    my $formatName = 'wallpaper';
    my $priceType;

    if ( $self->{clientID} == RPS::Import::Importer::WMG )    # warner
    {
        # 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 ( $priceType = RPS::Import::WMG::Util::getWirelessPriceType($formatName) ) {
            $self->warning();
            print STDERR "unknown format mapping: $formatName\n";
        }

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

    my $linenum = 1;
    my $seenHeader;
    my %errors = ();
    foreach my $line (@$lines) {
        my $track   = $line->[FIELD_TRACK];
        my $grid    = $line->[FIELD_GRID];
        my ($units) = $line->[FIELD_UNITS] =~ m/^(-?\d*,*\d,*\d*)$/;
        my $retail  = $line->[FIELD_RETAIL];
        my ($wRate) = $line->[FIELD_RATE] =~ m/^(\d?\.\d+)$/;

        if ( $track && $grid && $units && $wRate ) {
            $units =~ s/,//g;
            my $price = $wRate * $retail;
            my $sale  = RPS::Import::SaleRec->new();

            $sale->productType($prodType);
            $sale->formatType($formatType);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->upc($grid);
            $sale->trackName($track);
            $sale->units($units);
            $sale->price($price);
            $sale->retail($retail);
            $sale->wholesaleRate($wRate);
            $sale->lineNum($linenum);
            $sale->priceType($priceType);

            #$args{dumper}($sale);
            $self->_insertSale( sale => $sale );
        }
        $linenum++;
    }

    return $linenum;
}

#
# Private methods
#

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

    $fileName =~ m/\s(\w+)[\s_\-']+(\d+)\D/;
    my ( $month, $year ) = ( $1, $2 );
    $month = Decode_Month($1);
    $year += 2000 if ( $year < 2000 );

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

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