package RPS::Import::Echo;

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 FIELD_ALBUM     => 0;
use constant FIELD_UNITS     => 1;
use constant FIELD_WHOLESALE => 2;
use constant FIELD_PRICE     => 3;

#public methods

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

    my $retval      = undef;
    my $sheets      = $args{sheets};
    my $sheet_names = $args{sheet_names};

    my $linenum;

    for ( my $sheet_i = 0 ; $sheet_i < @$sheets ; $sheet_i++ ) {
        my $sheet      = $sheets->[$sheet_i];
        my $sheet_name = $sheet_names->[$sheet_i];

        my $dateBegin;
        my $dateEnd;

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

        unless ( $dateBegin && $dateEnd ) {
            $self->errstr( "couldn't get dates from " . $sheet_name );
            return undef;
        }

        $linenum = 1;
        foreach my $line (@$sheet) {
            my $prodtype    = RPS::File::Sale::TYPE_PHYSICAL;
            my $price_level = RPS::File::Sale::PLEVEL_UNKNOWN;
            my $channel     = RPS::File::Sale::CHANNEL_RETAIL;
            my $album       = $line->[FIELD_ALBUM];

            if ( $album =~ m/^\s*(TOTAL\:)?\s*$/ ) {
                $linenum++;
                next;
            }

            my ($units)           = $line->[FIELD_UNITS] =~ m/^(-?\d+)$/;
            my ($price)           = $line->[FIELD_PRICE] =~ m/^(-?\d*\.?\d*)$/;
            my ($wholesale_price) = $line->[FIELD_WHOLESALE] =~ m/^(-?\d*\.?\d*)$/;

            my $catno = "";

            if ( $album =~ m/^(.*)\s*\[([^\]]+)\]\s*$/ ) {
                $album = $1;
                $catno = $2;
            } elsif ( $album =~ m/^\[([^\]]+)\]\s*(.*)\s*$/ ) {
                $catno = $1;
                $album = $2;
            }

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

                $sale->productType($prodtype);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->clientProductID($catno);
                $sale->albumName($album);
                $sale->wholesalePrice($wholesale_price);
                $sale->sales($units);
                $sale->salesRevenue($price);
                $sale->totalRevenue($price);
                $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;

    my ( $dateBegin, $dateEnd );

    if ( $date =~ m/^Q(\d)_(\d{4})$/ ) {

        my $qtr  = $1;
        my $year = $2;

        if ( $qtr == 1 ) {
            $dateBegin = $year . "-01-01";
            $dateEnd = $year . "-03-" . Days_in_Month( $year, 3 );
        } elsif ( $qtr == 2 ) {
            $dateBegin = $year . "-04-01";
            $dateEnd = $year . "-06-" . Days_in_Month( $year, 6 );
        } elsif ( $qtr == 3 ) {
            $dateBegin = $year . "-07-01";
            $dateEnd = $year . "-09-" . Days_in_Month( $year, 9 );
        } elsif ( $qtr == 4 ) {
            $dateBegin = $year . "-10-01";
            $dateEnd = $year . "-12-" . Days_in_Month( $year, 12 );
        }
    } else {
        return undef;
    }

    return ( $dateBegin, $dateEnd );
}

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