package RPS::Import::Sunnyside;

use strict;

#use Date::Calc qw(Days_in_Month Decode_Month);
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';

use constant FIELD_SERVICE   => 1;
use constant FIELD_MONTH     => 2;
use constant FIELD_UPC       => 3;
use constant FIELD_ISRC      => 4;
use constant FIELD_UNITS     => 6;
use constant FIELD_PRICE     => 7;
use constant FIELD_ARTIST    => 8;
use constant FIELD_ALBUM     => 9;
use constant FIELD_TRACK     => 10;
use constant FIELD_PROD_TYPE => 12;
use constant FIELD_COUNTRY   => 13;
use constant FIELD_LABEL     => 15;
use constant FIELD_PROD_ID   => 17;

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

    return undef unless $lines;

    my %dateCache = ();
    my $linenum   = 2;
    shift @$lines;    # skip first row

    foreach my $line (@$lines) {
        unless ( $dateCache{ $line->[FIELD_MONTH] } ) {
            my ( $y, $m ) = split( '-', $line->[FIELD_MONTH] );
            unless ( $y && $m ) {
                $self->errstr("couldn't get dates");
                print STDERR "date cell: $line->[FIELD_MONTH]\n";
                return undef;
            }

            #$y += 2000;
            #$m = Decode_Month($m);

            @{ $dateCache{ $line->[FIELD_MONTH] } } =
              ( sprintf( "%04d-%02d-01", $y, $m ), sprintf( "%04d-%02d-%02d", $y, $m, Days_in_Month( $y, $m ) ) );
        }
        my ( $dateBegin, $dateEnd ) = @{ $dateCache{ $line->[FIELD_MONTH] } };

        my $prodtype =
            $line->[FIELD_PROD_TYPE] =~ /a/i ? RPS::File::Sale::TYPE_ALBUM
          : $line->[FIELD_PROD_TYPE] =~ /t/i ? RPS::File::Sale::TYPE_TRACK
          :                                    '';

        my $format  = RPS::File::Sale::FORMAT_DOWNLOAD;
        my $prod_id = $line->[FIELD_PROD_ID];
        my $upc     = $line->[FIELD_UPC];
        my $isrc    = $line->[FIELD_ISRC];
        my $artist  = $line->[FIELD_ARTIST];
        my $album   = $line->[FIELD_ALBUM];
        my $track   = $line->[FIELD_TRACK];
        my $label   = $line->[FIELD_LABEL];
        my ($units) = $line->[FIELD_UNITS] =~ m/^(-?\d+)$/;
        $line->[FIELD_PRICE] =~ s/,//g;
        my ($price) = $line->[FIELD_PRICE] =~ m/^(-?\$?\d+\.?\d*)$/;
        my $country = $line->[FIELD_COUNTRY];

        $country = $Common::Consts::COUNTRY_CODE{ lc $country } if ( length $country > 2 );

        if ( $units && $price && ( $artist || $album ) ) {
            $price =~ s/\$//;
            $units *= -1 if ( $price < 0 && $units > 0 );
            $price /= $units;
            $price = abs($price);

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

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->clientProductID($prod_id);
            $sale->upc($upc);
            $sale->isrc($isrc);
            $sale->artistName($artist);
            $sale->albumName($album);
            $sale->trackName($track);
            $sale->units($units);
            $sale->price($price);
            $sale->countryCode($country);
            $sale->lineNum($linenum);

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

#
# Private methods
#

sub _get_dates {
    my $cell = shift;

    my ( $month, $year ) = split( '-', $cell );
    return undef unless ( $month && $year );
    $year += 2000;
    $month = Decode_Month($month);

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

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