package RPS::Import::Kosmic;

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::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

my %fieldMap = (
    1 => {
        date_row => 2,
        date_col => 0,
        album    => 2,
        aartist  => 3,
        catalog  => 4,
        format   => 5,
        track    => 6,
        tartist  => 7,
        isrc     => 8,
        units    => 10,
        price    => 12,
    },
);

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

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

            foreach my $line (@$sheet) {
                my $country  = "US";
                my $currency = "USD";
                my $product  = RPS::File::Sale::TYPE_ALBUM;
                my $format   = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $catalog  = $line->[ $offsets->{catalog} ];
                my $isrc     = $line->[ $offsets->{isrc} ];
                my $artist   = $line->[ $offsets->{aartist} ];
                my $track    = $line->[ $offsets->{track} ];
                my $album    = $line->[ $offsets->{album} ];
                my $units    = $line->[ $offsets->{units} ];
                my $price    = $line->[ $offsets->{price} ];
                $price /= $units if ( $units != 0 );

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

                    $isrc =~ s/\-//g;

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

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

                    $sale->mediaType(2) if ( $format =~ /video/i );
                    $sale->productType($product);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    if ( $isrc ne "" ) {
                        $artist = $line->[ $offsets->{tartist} ];
                        $sale->isrc($isrc);
                        $sale->productType(RPS::File::Sale::TYPE_TRACK);
                        $sale->trackName($track);

                    } else {
                        $sale->productType(RPS::File::Sale::TYPE_ALBUM);
                    }
                    $sale->artistName($artist);
                    $sale->formatType($format);
                    $sale->albumName($album);
                    $sale->serviceProductID($catalog);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

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

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

#
# Private methods
#

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

    #print STDERR "|>".$passed_date."<|\n";
    if ( $passed_date =~ m/(\d+)\D(\d+)\D(\d{4})\> to \<(\d+)\D(\d+)\D(\d{4})/ ) {
        return ( $3 . "-" . sprintf( "%02d", $2 ) . "-" . sprintf( "%02d", $1 ),
            $6 . "-" . sprintf( "%02d", $5 ) . "-" . sprintf( "%02d", $4 ) );
    } elsif ( $passed_date =~ m/Q(\d) (\d{4})/ ) {
        $year = $2;
        $qtr  = $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 );
    }

    return undef;
}

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