package RPS::Import::AMIEStreet2;

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::Import::Importer;
use RPS::File::Sale;
use base 'RPS::Import::Importer';

my %field_map = (
    2 => {
        year         => 0,
        month        => 1,
        upc          => 2,
        isrc         => 3,
        product_type => 5,
        format_type  => 6,
        artist       => 9,
        album        => 10,
        track        => 11,
        units        => 12,
        price        => 13,
        currency     => 17,
    },
    3 => {
        year         => 0,
        month        => 1,
        upc          => 2,
        isrc         => 3,
        content_id   => 5,
        product_type => 6,
        format_type  => 7,
        artist       => 10,
        album        => 11,
        track        => 12,
        units        => 13,
        price        => 14,
        currency     => 18,
    },
);

#
# public methods
#

sub _importLines {
    my $self     = shift;
    my %args     = @_;
    my $lines    = $args{lines};
    my $version  = $args{file}->VersionNum();
    my $fileName = $args{file}->OrigFileName();

    my $linenum     = 1;
    my $country     = "US";
    my $sheet_count = 0;
    my $sheet_names = $args{sheet_names};
    my $headerFound;

    foreach my $sheet ( @{ $args{sheets} } ) {
        $linenum     = 1;
        $headerFound = 0;

        # Shift some columns over on for streaming sheet
        if ( $sheet_count == 2 ) {
            $field_map{$version}{units}--;
            $field_map{$version}{format_type}--;
            $field_map{$version}{artist}--;
            $field_map{$version}{album}--;
            $field_map{$version}{track}--;
            $field_map{$version}{price}++;
            $field_map{$version}{currency}--;
        }

        foreach my $line (@$sheet) {
            my $isrc = $line->[ $field_map{$version}{isrc} ];

            if ($headerFound) {
                my $upc          = $line->[ $field_map{$version}{upc} ];
                my $product_type = $line->[ $field_map{$version}{product_type} ];
                my $format_type  = uc( $line->[ $field_map{$version}{format_type} ] );
                my $artist       = $line->[ $field_map{$version}{artist} ];
                my $album        = $line->[ $field_map{$version}{album} ];
                my $track        = $line->[ $field_map{$version}{track} ];
                my $units        = $line->[ $field_map{$version}{units} ];
                my $price        = $line->[ $field_map{$version}{price} ];
                my $currency     = $line->[ $field_map{$version}{currency} ];
                my $clientProductID;

                if ( $format_type eq 'D' ) {
                    $format_type = RPS::File::Sale::FORMAT_DOWNLOAD;
                } elsif ( $format_type eq 'S' ) {
                    $format_type = RPS::File::Sale::FORMAT_STREAM;
                } elsif ( $format_type eq 'T' ) {
                    $format_type = RPS::File::Sale::FORMAT_TETHERED;
                }

                $product_type = "T" if ( $product_type eq "S" );
                $product_type = "A" if ( $product_type eq "CD5" );
                $product_type = "A" if ( $product_type eq "C" );
                $product_type = "T" if ( $sheet_count == 2 );        # stream download

                if ( defined( $field_map{$version}{content_id} ) ) {
                    $clientProductID = $line->[ $field_map{$version}{content_id} ];
                }

                if ( $price != 0 && ( $album ne "" || $upc ne "" || $track ne "" || $isrc ne "" ) ) {
                    my ( $dateBegin, $dateEnd ) =
                      $self->_getDates( $line->[ $field_map{$version}{year} ], $line->[ $field_map{$version}{month} ] );
                    unless ( $dateBegin && $dateEnd ) {
                        print STDERR "Couldn't get dates from l:"
                          . $linenum . " d:"
                          . $line->[ $field_map{$version}{year} ] . " - "
                          . $line->[ $field_map{$version}{month} ] . "\n";
                        $self->errstr( "Couldn't get dates from l:"
                              . $linenum . " d:"
                              . $line->[ $field_map{$version}{year} ] . " - "
                              . $line->[ $field_map{$version}{month} ] );
                        return undef;
                    }

                    # For version three of this file the third sheet lists streamed
                    # tracks.  The price column is the total revene not per unit like
                    # the first sheet.  Case #11982
                    if ( $units && $sheet_count == 2 && $version == 3 ) {
                        $price /= $units;
                    }

                    my $sale = RPS::Import::SaleRec->new();
                    $sale->productType($product_type);
                    $sale->trackName($track);
                    $sale->isrc($isrc);
                    $sale->upc($upc);
                    $sale->formatType($format_type);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->artistName($artist);
                    $sale->albumName($album);
                    $sale->price($price);
                    $sale->units($units);
                    $sale->countryCode($country);
                    $sale->clientProductID($clientProductID);
                    $sale->lineNum($linenum);

                    $self->_insertSale( sale => $sale );
                }

                #else { print STDERR "Skip l:".$linenum." a:".$artist." a:".$album." u:".$upc. " t:".$track." i:".$isrc."\n"; }
            }

            $headerFound = 1 if ( $isrc =~ /^isrc$/i );
            $linenum++;
        }

        $sheet_count++;
    }

    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my $self  = shift;
    my $year  = shift;
    my $month = shift;
    my $dateBegin;
    my $dateEnd;

    if ( $year && $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.
###
