package RPS::Import::SanctHistorical;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month Add_Delta_Days);

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Consts;

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

use constant FIELD_FORMAT => 0;
use constant FIELD_ARTIST => 1;
use constant FIELD_ALBUM  => 2;
use constant FIELD_UPC    => 3;

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

#public methods

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

    my $retval = undef;
    my $lines  = $args{lines};

    return undef unless ($lines);

    my @date_info;
    my $max_col;
    for ( my $i = 4 ; $lines->[0]->[$i] ne "" ; $i++ ) {
        next unless ( $lines->[0]->[$i] eq "Ships" || $lines->[0]->[$i] eq "Returns" );
        $date_info[$i]->{type} = $lines->[0]->[$i];
        ( $date_info[$i]->{start}, $date_info[$i]->{end} ) = $self->_getDates( $lines->[1]->[$i] );
        unless ( $date_info[$i]->{start} && $date_info[$i]->{end} ) {
            $self->errstr( "can't get dates from " . $lines->[1]->[$i] );
            return undef;
        }
        $max_col = $i;
    }

    my $linenum = 1;
    foreach my $line (@$lines) {
        my $prodtype;
        if ( $line->[FIELD_FORMAT] eq 'CD' ) {
            $prodtype = RPS::File::Sale::TYPE_CD;
        } elsif ( $line->[FIELD_FORMAT] eq 'DVD' ) {
            $prodtype = RPS::File::Sale::TYPE_DVD;
        } elsif ( $line->[FIELD_FORMAT] eq 'LP' || $line->[FIELD_FORMAT] eq 'VINYL' ) {
            $prodtype = RPS::File::Sale::TYPE_LP;
        }
        my $upc    = Common::Util::normalize_upc( $line->[FIELD_UPC] );
        my $artist = $line->[FIELD_ARTIST];
        my $album  = $line->[FIELD_ALBUM];

        my $price_level = RPS::File::Sale::PLEVEL_UNKNOWN;
        my $channel     = RPS::File::Sale::CHANNEL_RETAIL;

        unless ( $prodtype && $artist && $album ) {
            print STDERR "skipping $linenum - ptype: $prodtype . a: $artist . alb: $album\n";
            $linenum++;
            next;
        }

        for ( my $i = 4 ; $i <= $max_col ; $i++ ) {
            my $units = $line->[$i];

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

                $sale->productType($prodtype);
                $sale->dateBegin( $date_info[$i]->{start} );
                $sale->dateEnd( $date_info[$i]->{end} );
                $sale->upc($upc);
                $sale->artistName($artist);
                $sale->albumName($album);
                if ( $date_info[$i]->{type} eq "Ships" ) {
                    $sale->sales($units);
                } elsif ( $date_info[$i]->{type} eq "Returns" ) {
                    $sale->returns($units);
                }
                $sale->priceLevel($price_level);
                $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;

    if ( $date =~ m/^([A-Z][a-z]+)-(\d\d)$/ ) {
        my $month     = Decode_Month($1);
        my $year      = $2 + 2000;
        my $dateBegin = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        my $dateEnd   = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
        return ( $dateBegin, $dateEnd );
    } elsif ( $date =~ m/^(\d\d\d\d)-(\d\d)-\d\d$/ ) {
        my $month     = $2;
        my $year      = $1;
        my $dateBegin = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        my $dateEnd   = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
        return ( $dateBegin, $dateEnd );
    } elsif ( $date =~ m/^\d+$/ ) {
        my ( $year, $month, $day ) = Add_Delta_Days( 1900, 1, 1, $date );
        my $dateBegin = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        my $dateEnd = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
        return ( $dateBegin, $dateEnd );
    }

    return undef;
}

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