package RPS::Import::BMGSony2;

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

use lib '/app/tools/rps/lib';
use RPS::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

# map version number to data columns
my %fieldMap = (
    2 => {
        'date'        => 1,
        'channel'     => 4,
        'upc'         => 5,
        'artist'      => 6,
        'title'       => 7,
        'config'      => 8,
        'label'       => 14,
        'transaction' => 15,
        'retail'      => 17,
        'wholesale'   => 19,
        'price1'      => 20,
        'price1'      => 20,
        'price2'      => 23,
        'price3'      => 24,
        'units'       => 26,
    },
    5 => {
        'date'        => 1,
        'channel'     => 4,
        'upc'         => 5,
        'artist'      => 6,
        'title'       => 7,
        'config'      => 8,
        'label'       => 14,
        'transaction' => 15,
        'price1'      => 16,
        'price2'      => 17,

        #'price3' => 24,
        'units' => 19,
    },
);

#public methods

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

    my $fileObj = $args{file};
    my $version = $fileObj->VersionNum;

    my $offset = $fieldMap{$version};

    my $lines = $args{lines};

    shift @$lines;
    my $linenum = 2;

    foreach my $line (@$lines) {

        my $price_level = RPS::File::Sale::PLEVEL_FULL;

        my $upc    = $line->[ $offset->{'upc'} ];
        my $album  = $line->[ $offset->{'title'} ];
        my $artist = $line->[ $offset->{'artist'} ];
        my $label  = $line->[ $offset->{'label'} ];
        my $config = $line->[ $offset->{'config'} ];

        my ( $sales, $returns, $sales_p, $returns_p, $total, $price1, $price2, $price3 );
        if ( $line->[ $offset->{'transaction'} ] eq 'shipment' ) {
            ($sales)  = $line->[ $offset->{'units'} ] =~ m/^\s*(-?[0-9]+)\s*$/;
            ($price1) = $line->[ $offset->{'price1'} ] =~ m/^\s*(-?[0-9]*\.?[0-9]*)\s*$/;
            ($price2) = $line->[ $offset->{'price2'} ] =~ m/^\s*(-?[0-9]*\.?[0-9]*)\s*$/;
            ($price3) = $line->[ $offset->{'price3'} ] =~ m/^\s*(-?[0-9]*\.?[0-9]*)\s*$/;
            $sales_p   = $price1 + $price2 + $price3;
            $total     = $sales_p;
            $returns   = 0;
            $returns_p = 0;
        } elsif ( $line->[ $offset->{'transaction'} ] eq 'return' ) {
            ($returns) = $line->[ $offset->{'units'} ] =~ m/^\s*(-?[0-9]+)\s*$/;
            ($price1)  = $line->[ $offset->{'price1'} ] =~ m/^\s*(-?[0-9]*\.?[0-9]*)\s*$/;
            ($price2)  = $line->[ $offset->{'price2'} ] =~ m/^\s*(-?[0-9]*\.?[0-9]*)\s*$/;
            ($price3)  = $line->[ $offset->{'price3'} ] =~ m/^\s*(-?[0-9]*\.?[0-9]*)\s*$/;
            $returns_p = $price1 + $price2 + $price3;
            $total     = $returns_p;
            $returns   *= -1;
            $returns_p *= -1;
            $sales   = 0;
            $sales_p = 0;
        }

        my $retail_price    = $line->[ $offset->{'retail'} ];
        my $wholesale_price = $line->[ $offset->{'wholesale'} ];

        if ( $album && $config && defined $upc && ( $total || $sales || $returns ) ) {

            my ( $dateBegin, $dateEnd ) = $self->_getDates( $line->[ $offset->{'date'} ] );
            unless ( $dateBegin && $dateEnd ) {
                $self->errstr(
                    "couldn't get dates from " . $line->[ $offset->{'date'} ] . " on $linenum - " . $line->[ $offset->{'title'} ] );
                return undef;
                next;
            }

            my $channel = $line->[ $offset->{'channel'} ];
            if ( $channel eq 'commercial' ) {
                $channel = RPS::File::Sale::CHANNEL_RETAIL;
            } elsif ( $channel eq 'military' ) {
                $channel = RPS::File::Sale::CHANNEL_MILITARY;
            } else {
                $self->errstr( "unknown channel: " . $channel );
                return undef;
            }

            my $prodtype;
            if ( $config eq 'CD' || $config eq 'SGCD' ) {
                $prodtype = RPS::File::Sale::TYPE_CD;
            } elsif ( $config eq 'LP' || $config eq 'SGVY' ) {
                $prodtype = RPS::File::Sale::TYPE_LP;
            } elsif ( $config eq 'DVD' || $config eq 'DVDS' ) {
                $prodtype = RPS::File::Sale::TYPE_DVD;
            } elsif ( $config eq 'VID' ) {
                $prodtype = RPS::File::Sale::TYPE_VHS;
            } else {
                $self->errstr("unknown config: $config");
                return undef;
            }

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

            $sale->productType($prodtype);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->upc($upc);
            $sale->artistName($artist);
            $sale->albumName($album);
            $sale->labelName($label);
            $sale->retailPrice($retail_price);
            $sale->wholesalePrice($wholesale_price);
            $sale->sales($sales);
            $sale->returns($returns);
            $sale->salesRevenue($sales_p);
            $sale->returnsRevenue($returns_p);
            $sale->totalRevenue($total);
            $sale->priceLevel($price_level);
            $sale->configuration($config);
            $sale->channel($channel);
            $sale->countryCode("US");
            $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 ( $startDate, $endDate );
    if ( $date =~ m/^(\d\d\d\d)(\d\d)$/ ) {
        my $year  = $1;
        my $month = $2;
        $startDate = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        $endDate = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
    } else {
        return undef;
    }

    return ( $startDate, $endDate );
}

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