package RPS::Import::Fontana;

use strict;

use Date::Calc qw(Days_in_Month 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 FIELD_CATNO    => 1;
use constant FIELD_ALBUM    => 2;
use constant FIELD_ARTIST   => 3;
use constant FIELD_FORMAT   => 7;
use constant FIELD_SALES    => 12;
use constant FIELD_PSALES   => 13;
use constant FIELD_RETURNS  => 14;
use constant FIELD_PRETURNS => 15;

my %field_map = (
    1 => {
        'catno'    => 1,
        'album'    => 2,
        'artist'   => 3,
        'format'   => 7,
        'sales'    => 12,
        'psales'   => 13,
        'returns'  => 14,
        'preturns' => 15,
        'discount' => 16,
        'penalty'  => 17,
        'reward'   => 18,
    },
    2 => {
        'catno'    => 2,
        'album'    => 3,
        'artist'   => 4,
        'format'   => 5,
        'sales'    => 12,
        'psales'   => 9,
        'returns'  => 13,
        'preturns' => 10,
        'scode'    => 16,
    },
    3 => {
        'catno'    => 2,
        'album'    => 3,
        'artist'   => 4,
        'format'   => 5,
        'sales'    => 12,
        'psales'   => 9,
        'returns'  => 13,
        'preturns' => 10,
        'scode'    => 16,
    },
    4 => {
        'catno'    => 2,
        'album'    => 3,
        'artist'   => 4,
        'format'   => 5,
        'sales'    => 12,
        'psales'   => 9,
        'returns'  => 13,
        'preturns' => 10,
        'scode'    => 16,
    },
);

#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 $fileName = $fileObj->OrigFileName;
    my $version  = $fileObj->VersionNum;
    my $offsets  = $field_map{$version};

    my $free = 0;
    my $dateBegin;
    my $dateEnd;
    $lines = $sheets->[1] if ( $version == 3 );
    ( $dateBegin, $dateEnd ) = $self->_getDates($fileName);

    unless ( $dateBegin && $dateEnd ) {
        $self->errstr("couldn't get dates from $fileName");
        return undef;
    }

    my $linenum = 1;
    foreach my $line (@$lines) {
        $free = 0;
        my $price_level = RPS::File::Sale::PLEVEL_FULL;
        my $channel     = RPS::File::Sale::CHANNEL_RETAIL;
        my $catno       = $line->[ $offsets->{'catno'} ];
        my $artist      = $line->[ $offsets->{'artist'} ];
        my $album       = $line->[ $offsets->{'album'} ];
        my $config      = $line->[ $offsets->{'format'} ];
        my $prodtype;

        if ( $config eq 'CD' ) {
            $prodtype = RPS::File::Sale::TYPE_CD;
        } elsif ( $config eq 'DV' ) {
            $prodtype = RPS::File::Sale::TYPE_DVD;
        } elsif ( $config eq 'VH' ) {
            $prodtype = RPS::File::Sale::TYPE_VHS;
        } elsif ( $config eq 'CF' ) {
            $prodtype = RPS::File::Sale::TYPE_CD;
        } elsif ( $config eq 'S5' ) {
            $prodtype = RPS::File::Sale::TYPE_CD_SIN;
        } elsif ( $config eq 'ST' ) {
            $prodtype = RPS::File::Sale::TYPE_LP;
        } elsif ( $config eq 'DX' ) {

            #$prodtype = RPS::File::Sale::TYPE_DVD_CD_SET;
            $prodtype = RPS::File::Sale::TYPE_CD;
        }

        my $scode = $line->[ $offsets->{'scode'} ] if ( defined $offsets->{scode} );
        $free = 1 if ( defined $offsets->{scode} && $scode =~ /(51|53|54|59|85|91)/ );
        my ($sales)         = $line->[ $offsets->{'sales'} ] =~ m/^(-?\d+)$/;
        my ($returns)       = $line->[ $offsets->{'returns'} ] =~ m/^(-?\d+)$/;
        my ($sales_price)   = $line->[ $offsets->{'psales'} ] =~ m/^(-?\d*\.?\d*)$/;
        my ($returns_price) = $line->[ $offsets->{'preturns'} ] =~ m/^(-?\d*\.?\d*)$/;
        my $total;
        if ( $version == 1 ) {
            my ($discount) = $line->[ $offsets->{'discount'} ] =~ m/^(-?\d*\.?\d*)$/;
            my ($penalty)  = $line->[ $offsets->{'penalty'} ] =~ m/^(-?\d*\.?\d*)$/;
            my ($reward)   = $line->[ $offsets->{'reward'} ] =~ m/^(-?\d*\.?\d*)$/;
            $total = $sales_price + $returns_price + $discount + $penalty + $reward;
        } elsif ( $version > 1 ) {
            $total = $sales_price + $returns_price;
        }

        $returns       *= -1;
        $returns_price *= -1;

        if ( $prodtype && ( $sales || $returns ) && $album && $free == 0 ) {
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->artistName($artist);
            $sale->albumName($album);
            $sale->clientProductID($catno);
            $sale->sales($sales);
            $sale->salesRevenue($sales_price);
            $sale->returns($returns);
            $sale->returnsRevenue($returns_price);
            $sale->totalRevenue($total);
            $sale->priceLevel($price_level);
            $sale->channel($channel);
            $sale->configuration($config);
            $sale->countryCode('US');
            $sale->lineNum($linenum);

            ##$args{dumper}($sale);  ## for cmd-line testing
            $self->_insertSale( sale => $sale );
        }

#else { print STDERR "l: ".$linenum. " p: " . $prodtype . " c:". $config." f:".$scode ." s: " . $sales . " r: ". $returns . " a:". $album. "\n"; }
        $linenum++;
    }
    return $linenum;
}

#
# Private methods
#

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

    my ( $dateBegin, $dateEnd );

    if ( $date =~ m/^Detailed Sales ([A-Z][a-z]{2})(\d{2})\.xls$/ ) {

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

        $dateBegin = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        $dateEnd = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
    } elsif ( $date =~ m/Six Sales (\d{2})(\d{2})\.xls/ ) {
        my $month = $1;
        my $year  = $2 + 2000;
        $dateBegin = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        $dateEnd = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
    } elsif ( $date =~ m/([A-Z][a-z]{2,3})\.? (\d{2}) Sales\.xls$/ ) {

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

        $dateBegin = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        $dateEnd = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
    } elsif ( $date =~ m/Detail Sales (\d{2})(\d{2}) - USD\.xls/ ) {

        my $month = $1;
        my $year  = $2 + 2000;

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

    } elsif ( $date =~ m/(\d{4}) (\d+)/ ) {
        my $month = $2;
        my $year  = $1;

        $dateBegin = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        $dateEnd = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
    } else {
        return undef;
    }

    return ( $dateBegin, $dateEnd );
}

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