package RPS::Import::Shellshock;

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_CATNO => 0;
use constant FIELD_SALES => 1;
use constant FIELD_PRICE => 4;

#public methods

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

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

    my ( $dateBegin, $dateEnd );

    my $linenum = 1;

    ( $dateBegin, $dateEnd ) = $self->_getDates($fileName) if ( $fileName =~ /(\d{4})/ );

    foreach my $line (@$lines) {
        my $prodtype    = RPS::File::Sale::TYPE_CD;
        my $price_level = RPS::File::Sale::PLEVEL_FULL;
        my $channel     = RPS::File::Sale::CHANNEL_RETAIL;
        my $catno       = $line->[FIELD_CATNO];

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

        if ( $catno =~ m/^[A-Z][a-z][a-z]\. \d\d$/ && !$sales && !$price ) {
            ( $dateBegin, $dateEnd ) = $self->_getDates($catno);
            unless ( $dateBegin && $dateEnd ) {
                $self->errstr("couldn't get dates from $catno");
                return undef;
            }
            $linenum++;
            next;
        }

        if ( $catno && $sales && $price && $catno !~ /total/i && $line->[3] ne "" ) {
            unless ( $dateBegin && $dateEnd ) {
                $self->errstr("couldn't get dates");
                return undef;
            }
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->clientProductID($catno);
            $sale->sales($sales);
            $sale->salesRevenue($price);
            $sale->totalRevenue($price);
            $sale->priceLevel($price_level);
            $sale->channel($channel);
            $sale->countryCode('GB');
            $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 ( $month, $year, $qtr, $dateBegin, $dateEnd );
    if ( $date =~ m/ (\d)Q (\d{4})/ ) {
        $qtr  = $1;
        $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 );
    } elsif ( $date =~ m/ (\d)H (\d{4})/ ) {
        $qtr  = $1;
        $year = $2;
        if ( $qtr == 1 ) {
            $dateBegin = $year . "-01-01";
            $dateEnd = $year . "-06-" . Days_in_Month( $year, 6 );
        } elsif ( $qtr == 2 ) {
            $dateBegin = $year . "-07-01";
            $dateEnd = $year . "-12-" . Days_in_Month( $year, 12 );
        } else {
            return undef;
        }
        return ( $dateBegin, $dateEnd );
    } elsif ( $date =~ m/^([A-Z][a-z][a-z])\. (\d\d)$/ ) {
        $month     = Decode_Month($1);
        $year      = $2 + 2000;
        $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.
###
