package RPS::Import::Rykodisc2;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

use lib '/app/tools/common/lib';

#use Common::Util;
use Common::Util qw(normalize_date normalize_isrc);
use Common::Consts qw(%COUNTRY_CODE);

use lib '/app/tools/data_classes/lib';
use Client::Service;

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

my %field_map = (
    3 => {
        'artist'       => 0,
        'title'        => 1,
        'upc'          => 2,
        'label'        => 3,
        'sales_units'  => 4,
        'return_units' => 5,
        'sale_price'   => 7,
        'return_price' => 8,
        'net_price'    => 9,
    },
);

#public methods

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

    my $lines    = $args{lines};
    my $version  = $args{file}->VersionNum();
    my $fileName = $args{file}->OrigFileName();
    my $fileObj  = $args{file};

    if ( $version == 3 ) {
        $fileObj->Physical(1);
    }

    my @sheets = @{ $args{sheets} };

    return undef unless ($lines);

    my $gross_total = 0;

    my $linenum  = 1;
    my $sheetnum = 0;

    my $offsets = $field_map{$version};
    my %errors  = ();

    my ( $dateBegin, $dateEnd ) = $self->_getDates($fileName);

    my $num_lines = @$lines;

    foreach my $line (@$lines) {
        my $artist       = $line->[ $offsets->{artist} ];
        my $title        = $line->[ $offsets->{title} ];
        my $upc          = Common::Util::normalize_upc( $line->[ $offsets->{upc} ] );
        my $label        = $line->[ $offsets->{label} ];
        my $sales_units  = $line->[ $offsets->{sales_units} ];
        my $return_units = $line->[ $offsets->{return_units} ];
        my $sale_price   = $line->[ $offsets->{sale_price} ];
        my $return_price = $line->[ $offsets->{return_price} ];
        my $net_price    = $line->[ $offsets->{net_price} ];

        $return_units *= -1;
        $return_price *= -1;

        my $type        = RPS::File::Sale::TYPE_CD;
        my $price_level = RPS::File::Sale::PLEVEL_FULL;
        my $channel     = RPS::File::Sale::CHANNEL_RETAIL;

        if ( $artist && $title && $upc && $label && $net_price =~ /\d+/ ) {
            unless ( $dateBegin && $dateEnd ) {
                $self->errstr("couldn't get dates from filename $fileName");
                print STDERR "$fileName\n";
                return undef;
            }

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

            $sale->productType($type);
            $sale->dateBegin($dateBegin);
            $sale->priceLevel($price_level);
            $sale->channel($channel);
            $sale->dateEnd($dateEnd);
            $sale->artistName($artist);
            $sale->albumName($title);
            $sale->upc($upc);
            $sale->labelName($label);

            $sale->sales($sales_units);
            $sale->returns($return_units);
            $sale->salesRevenue($sale_price);
            $sale->returnsRevenue($return_price);
            $sale->totalRevenue($net_price);

            $sale->lineNum($linenum);
            $sale->countryCode('US');

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

        $linenum++;
    }

    return $linenum;
}

#
# Private methods
#

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

    my $year;
    my $month;

    my ( $dateBegin, $dateEnd );

    my %months = (
        "jan" => 1,
        "feb" => 2,
        "mar" => 3,
        "apr" => 4,
        "may" => 5,
        "jun" => 6,
        "jul" => 7,
        "aug" => 8,
        "sep" => 9,
        "oct" => 10,
        "nov" => 11,
        "dec" => 12
    );

    if ( ( lc $fileName ) =~ /(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/ ) {

        my $month_abbr = $1;

        $month = $months{$month_abbr};
    } else {
        return undef;
    }

    if ( $fileName =~ /(\d+)/ ) {
        my $year_str = $1;

        if ( $year_str < 2000 ) {
            $year = $year_str + 2000;
        } else {
            $year = $year_str;
        }
    } else {
        return undef;
    }

    if ( !$year || !$month ) {
        return undef;
    }

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

    #return undef;
}

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