package RPS::Import::Stompy;

use strict;

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

use lib '/app/tools/common/lib';
use Common::Consts qw(%COUNTRY_TO_CODE);
use Common::Date;

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';

my %fieldMap = (
    1 => {
        artist => 0,
        track  => 1,
        label  => 2,
        isrc   => 3,
        units  => 6,
        price  => 7,
    },
    2 => {
        date_row => 1,
        date_col => 0,
        artist   => 0,
        track    => 1,
        label    => 2,
        isrc     => 3,
        upc      => 4,
        units    => 7,
        price    => 8,
    },
);

#public methods

sub _importLines {
    my $self        = shift;
    my %args        = @_;
    my $fileObj     = $args{file};
    my $version     = $args{file}->VersionNum();
    my $fileName    = $args{file}->OrigFileName();
    my $serviceID   = $args{file}->ServiceID;
    my $sheet_count = 0;

    return undef unless ( defined $args{sheets} && defined $version );

    my $offsets      = $fieldMap{$version};
    my $sheet_names  = $args{sheet_names};
    my $header_found = 0;
    my $linenum      = 1;
    my ( $dateBegin, $dateEnd ) = _getDates($fileName);

    foreach my $sheet ( @{ $args{sheets} } ) {
        $linenum = 1;
        if ( $sheet_names->[$sheet_count] !~ /summary/i ) {

            foreach my $line (@$sheet) {
                if ( defined $offsets->{date_row} && $linenum == $offsets->{date_row} ) {
                    ( $dateBegin, $dateEnd ) = _getDates( $line->[ $offsets->{date_col} ] );
                }
                my $country  = "US";
                my $currency = "USD";
                my $track    = $line->[ $offsets->{track} ];
                my $artist   = $line->[ $offsets->{artist} ];
                my $format   = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $product  = RPS::File::Sale::TYPE_TRACK;
                my $units    = $line->[ $offsets->{units} ];
                my $price    = $line->[ $offsets->{price} ];
                my $label    = $line->[ $offsets->{label} ];
                my $isrc     = $line->[ $offsets->{isrc} ];
                my $upc      = $line->[ $offsets->{upc} ] if ( defined $offsets->{upc} );
                $price /= $units if ( $units != 0 );

                if ( $units && $artist && $country && $price && $track && $track ne "Title" ) {

                    unless ( defined $dateBegin && defined $dateEnd ) {
                        print STDERR "Couldn't get dates from: " . $fileName . "\n";
                        $self->errstr( "couldn't get dates from: " . $fileName );
                        return undef;
                    }

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

                    $sale->mediaType(2) if ( $format =~ /video/i );
                    $sale->productType($product);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->labelName($label);
                    $sale->trackName($track);
                    $sale->artistName($artist);
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->isrc($isrc);
                    $sale->upc($upc) if ( defined $offsets->{upc} );
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

                    $self->_insertSale( sale => $sale );
                }

#else { print STDERR "skip sh: ".$sheet_names->[$sheet_count] . ": l: " . $linenum . " - u:".$units." p:".$price." a:".$artist." t:".$track." f:".$format."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my ( $month, $year, $qtr, $passed_date, $dateBegin, $dateEnd );
    $passed_date = shift || return;

    if ( $passed_date =~ m/Q(\d)-(\d{4})/i ) {
        $year = $2;
        $qtr  = $1;
    }

    elsif ( $passed_date =~ m/Q(\d) (\d{2})/i ) {
        $year = $2 + 2000;
        $qtr  = $1;
    }

    elsif ( $passed_date =~ m/(\w{3})-(\d{4})$/i ) {
        $year  = $2;
        $month = $1;
    }

    if ( $year && $qtr ) {
        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 );
    }

    if ( $year && $month ) {
        my $date = new Common::Date("$month-$year");
        return ( $date->monthBegin(), $date->monthEnd() );
    }

    return;
}

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