package RPS::Import::WMG::SprintMultiMonthTab;

use strict;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_isrc);

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

use lib '/app/tools/rps/lib';
use RPS::File::Sale;
use RPS::Import::WMG::Util;
use base 'RPS::Import::WMG::SprintBase';

my %field_map = (
    13 => {
        units_adjust => -1,
        month        => 0,
        year         => 1,
        cli_prod_id  => 3,
        svc_prod_id  => 4,
        description  => 5,
        units        => 8,
        retail       => 9,
        rate         => 10,
        price        => 11,
        isrc         => 12,
    },
    14 => {
        units_adjust => 1,
        date         => 6,
        cli_prod_id  => 0,
        description  => 3,
        svc_prod_id  => 5,
        isrc         => 7,
        units        => 8,
        retail       => 10,
        rate         => 11,
        price        => 12,
    },
    15 => {
        units_adjust => -1,
        cli_prod_id  => 3,
        description  => 5,
        isrc         => 6,
        svc_prod_id  => 7,
        date         => 8,
        units        => 9,
        retail       => 11,
        price        => 12,
    },
    16 => {
        units_adjust => -1,
        cli_prod_id  => 1,
        svc_prod_id  => 2,
        date         => 4,
        description  => 7,
        isrc         => 8,
        units        => 9,
        retail       => 12,
        price        => 15,
    },
    17 => {
        units_adjust => -1,
        cli_prod_id  => 1,
        svc_prod_id  => 2,
        date         => 4,
        description  => 7,
        isrc         => 8,
        units        => 9,
        retail       => 11,
        price        => 14,
    },
    18 => {
        units_adjust => -1,
        svc_prod_id  => 1,
        cli_prod_id  => 3,
        date         => 4,
        description  => 7,
        isrc         => 8,
        units        => 9,
        retail       => 11,
        price        => 12,
    },
    19 => {
        units_adjust => -1,
        cli_prod_id  => 3,
        description  => 4,
        isrc         => 5,
        svc_prod_id  => 7,
        date         => 8,
        units        => 9,
        retail       => 11,
        price        => 12,
    },
    22 => {
        units_adjust => -1,
        cli_prod_id  => 4,
        description  => 5,
        isrc         => 12,
        svc_prod_id  => 2,
        date         => 7,
        units        => 8,
        retail       => 9,
        price        => 11,
    },
    23 => {
        units_adjust => -1,
        cli_prod_id  => 3,
        description  => 5,
        date         => 7,
        units        => 8,
        price        => 11,
        isrc         => 12,
        retail       => 13,
    },
    24 => {
        units_adjust => -1,
        cli_prod_id  => 2,
        description  => 5,
        date         => 7,
        units        => 8,
        retail       => 9,
        price        => 12,
        isrc         => 13,
    },
);

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

    my $sheets  = $args{sheets};
    my $fileObj = $args{file};
    my $version = $self->_version;

    my ( $billTo, $sellTo ) = RPS::Import::WMG::Util::getWirelessInfo( $self->{fileServiceID} );
    unless ( $billTo && $sellTo ) {
        $self->warning();
        print STDERR "unknown billto/sellto mapping: $self->{fileServiceID}\n";
    }

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

    my $offsets  = $field_map{$version};
    my $prodType = RPS::File::Sale::TYPE_TRACK;
    my %errors;
    my $i = 0;

    foreach my $sheet (@$sheets) {

        #my $linenum = 100000 * ($i + 1); # keeps linenum unique for each sheet
        my $linenum = 1;
        my ( $dateBegin, $dateEnd );
        $self->_setDateRangeFromNumeric( $sheet, $offsets->{date} ) if ( $version =~ /(14|17)/ );
        my ( $clientProdID, $formatName, $format );

        foreach my $line (@$sheet) {
            if (   $line->[ $offsets->{cli_prod_id} ] ne ""
                && $line->[ $offsets->{cli_prod_id} ] !~ /(content|partner)/i
                && $line->[ $offsets->{description} ] ne ""
                && $line->[ $offsets->{price} ] ne ""
                && $line->[ $offsets->{units} ] ne "" ) {
                my $prodID = $line->[ $offsets->{cli_prod_id} ];
                next if ( $prodID =~ /(content|partner)/i );
                if ( $prodID && $prodID =~ /^B\d+/ && $prodID ne $clientProdID ) {
                    $clientProdID = $prodID;
                    $formatName   = $self->_decodeSprintFormatID($clientProdID);
                    $format       = RPS::Import::WMG::Util::getWirelessFormatTypeID($formatName);

                    unless ( $formatName && $format ) {
                        unless ( $errors{$clientProdID} ) {
                            print STDERR "unknown format for $clientProdID\n";
                            $errors{$clientProdID} = 1;
                        }
                        $linenum++;
                        next;
                    }
                }
                my $priceType = RPS::Import::WMG::Util::getWirelessPriceType($formatName);
                unless ($priceType) {
                    unless ( $errors{$formatName} ) {
                        $self->warning();
                        print STDERR "unknown format mapping: $formatName\n";
                        $errors{$formatName} = 1;
                    }
                }

                my ( $track, $artist );
                if ( $version =~ /(13|14|24)/ ) {
                    $formatName =~ /^(\w)/;
                    ( $track, $artist ) = $self->_parseDescription( $line->[ $offsets->{description} ], $1 );
                    $track =~ s/-$//;
                } else {
                    $track = $line->[ $offsets->{description} ];
                }

                my $svcProdID = $line->[ $offsets->{svc_prod_id} ];
                next if ( $svcProdID =~ m/\s+Total$/ );
                my $units   = $self->numeric( $line->[ $offsets->{units} ] );
                my $retail  = $self->numeric( $line->[ $offsets->{retail} ] );
                my $ppdRate = exists $offsets->{rate} ? $self->numeric( $line->[ $offsets->{rate} ] ) : undef;
                my $price   = $self->numeric( $line->[ $offsets->{price} ] );
                my $isrc    = defined $offsets->{isrc} ? normalize_isrc( $line->[ $offsets->{isrc} ] ) : '';

                ( $dateBegin, $dateEnd ) =
                    $version =~ /(14|17|18)/          ? $self->_getDatesFromNumeric( $line->[ $offsets->{date} ] )
                  : $version =~ /(15|16|19|22|23|24)/ ? $self->_getDates( $line->[ $offsets->{date} ] )
                  :                                     $self->_getDatesMY( $line->[ $offsets->{month} ], $line->[ $offsets->{year} ] );
                my $origDate =
                    $version =~ /(14|17|18)/          ? $self->numericToDate( $line->[ $offsets->{date} ] )
                  : $version =~ /(15|16|19|22|23|24)/ ? $line->[ $offsets->{date} ]
                  :                                     join( '-', $line->[ $offsets->{year} ], $line->[ $offsets->{month} ], 1 );

                if ( $track && $units && $retail != 0 && $price =~ m/\d/ ) {
                    unless ( $dateBegin && $dateEnd ) {
                        $self->errstr("couldn't get dates");
                        $linenum++;
                        next;
                    }

                    if ( $ppdRate ne "" ) {
                        $ppdRate = $price / $retail;
                    } else {
                        $retail /= $units;
                        $price  /= $units;
                    }

                    $units *= $offsets->{units_adjust} unless ( $units < 0 );

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

                    $sale->productType($prodType);
                    $sale->formatType($format);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->serviceProductID($svcProdID);
                    $sale->clientProductID($clientProdID);
                    $sale->artistName($artist);
                    $sale->trackName($track);
                    $sale->isrc($isrc);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->retail($retail);
                    $sale->wholesaleRate($ppdRate);
                    $sale->priceType($priceType);
                    $sale->payoutType($origDate);    # had to stick this somewhere
                    $sale->lineNum($linenum);

                    #$args{dumper}($sale); # for cmd-line testing
                    $self->_insertSale( sale => $sale );
                } else {
                    print STDERR "skip l: $linenum r: $retail u: $units p: $price t: $track ($linenum)\n";
                }
            }
            $linenum++;
        }
        $i++;
    }

    return $i;
}

#
# Private methods
#

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