package RPS::Import::30th;

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_CODE);

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

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

my %fieldMap = (
    1 => {
        date_row => 3,
        date_col => 2,
        catalog  => 0,
        isrc     => 1,
        artist   => 2,
        title    => 3,
        currency => 9,
        format   => 10,
        country  => 11,
        units    => 14,
        price    => 15,
    },
);

my %format_map = ( REALTONE => RPS::File::Sale::FORMAT_RINGTONE, );

#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 ( $total_unit_column, $total_price_column, $last_artist );

    foreach my $sheet ( @{ $args{sheets} } ) {
        $linenum = 1;
        my ( $dateBegin, $dateEnd ) = _getDates( $sheet->[ $offsets->{date_row} ]->[ $offsets->{date_col} ] );
        if ( $sheet_names->[$sheet_count] !~ /summary/i ) {

            foreach my $line (@$sheet) {

                my $country  = $Common::Consts::COUNTRY_CODE{ lc( $line->[ $offsets->{country} ] ) };
                my $currency = $line->[ $offsets->{currency} ];
                my $product  = RPS::File::Sale::TYPE_TRACK, my $format = $format_map{ uc( $line->[ $offsets->{format} ] ) };
                my $artist   = $line->[ $offsets->{artist} ];
                my $title    = $line->[ $offsets->{title} ];
                my $price    = $line->[ $offsets->{price} ];
                my $units    = $line->[ $offsets->{units} ];
                my $catalog  = $line->[ $offsets->{catalog} ];
                my $isrc     = $line->[ $offsets->{isrc} ];
                $price /= $units if ( $units != 0 );
                $price /= 2;

                if ( $units && $country && $price && $title ne "TITLE" && $artist ) {

                    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->artistName($artist);
                    $sale->trackName($title);
                    $sale->serviceProductID($catalog);
                    $sale->isrc($isrc);
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

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

                #else { print STDERR "skip l:" . $linenum . " - c:".$country." a:".$artist." u:".$units." p:".$price." t:".$title."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my ( $smonth, $syear, $emonth, $eyear, $passed_date, $dateBegin, $dateEnd );
    $passed_date = shift;
    if ( $passed_date =~ m/(\w+) \- (\w+) (\d{4})/ ) {
        $smonth = Decode_Month($1);
        $emonth = Decode_Month($2);
        $syear  = $3;
        $eyear  = $3;
    }
    if ( $smonth && $syear && $emonth && $eyear ) {
        return ( $syear . "-" . $smonth . "-01", $eyear . "-" . $emonth . "-" . Days_in_Month( $eyear, $emonth ) );
    }

    return undef;
}

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