package RPS::Import::Dock;

use strict;

use Date::Calc qw(Days_in_Month Decode_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 DATE_ROW   => 1;
use constant DATE_START => 6;
use constant DATE_END   => 13;

use constant FIELD_VENDORID => 0;
use constant FIELD_CATNO    => 1;
use constant FIELD_FORMAT   => 3;
use constant FIELD_ARTIST   => 4;
use constant FIELD_ALBUM    => 5;
use constant FIELD_SALES    => 8;
use constant FIELD_PRICE    => 9;

#public methods

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

    # This importer appears to be calculating sale revenue incorrectly.  It hasn't been used since
    # 2007-07-30 so instead of fixing it I deprecated it.
    die "Importer no longer valid";

    my $lines = $args{lines};

    my $dateBegin;
    my $dateEnd;

    ( $dateBegin, $dateEnd ) = $self->_getDates( $lines->[DATE_ROW]->[DATE_START], $lines->[DATE_ROW]->[DATE_END] );

    unless ( $dateBegin && $dateEnd ) {
        $self->errstr( "couldn't get dates from " . $lines->[DATE_ROW]->[DATE_START] );
        print STDERR "couldn't get dates from " . $lines->[DATE_ROW]->[DATE_START] . "\n";
        return undef;
    }

    my $linenum = 1;
    foreach my $line (@$lines) {
        my $prodtype    = RPS::File::Sale::TYPE_PHYSICAL;
        my $price_level = RPS::File::Sale::PLEVEL_FULL;
        my $channel     = RPS::File::Sale::CHANNEL_RETAIL;
        my $artist      = $line->[FIELD_ARTIST];
        my $album       = $line->[FIELD_ALBUM];
        my $config      = $line->[FIELD_FORMAT];

        my ($sales) = $line->[FIELD_SALES] =~ m/^(-?\d+)$/;
        my ($price) = $line->[FIELD_PRICE] =~ m/^(-?\d*\.?\d*)$/;

        ## clean up album name
        $album =~ s/\([^\(]*$//;

        if ( $prodtype && $sales && $album ) {
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->artistName($artist);
            $sale->albumName($album);
            $sale->sales($sales);

            # This looks wrong to me.  Should be units * price?  See create space
            $sale->salesRevenue($price);
            $sale->totalRevenue($price);
            $sale->priceLevel($price_level);
            $sale->channel($channel);
            $sale->configuration($config);
            $sale->countryCode('ES');
            $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 $date1 = shift;
    my $date2 = shift;

    my ( $dateBegin, $dateEnd );

    if ( $date1 =~ m/^(\d{4})-(\d{2})-(\d{2})$/ ) {
        $dateBegin = sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    }
    if ( $date2 =~ m/^(\d{4})-(\d{2})-(\d{2})$/ ) {
        $dateEnd = sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    }

    return ( $dateBegin, $dateEnd );
}

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