package RPS::Import::LiquidAudioCrystal;

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 = (
    3 => {
        artist => 1,
        track  => 2,
    },
);

#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 );
    my ( $dateBegin, $dateEnd ) = _getDates($fileName);

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

            foreach my $line (@$sheet) {
                if ( $line->[ $offsets->{track} + 1 ] =~ /(\d+)\D(\d+)\D(\d{4})/ ) {
                    my @columns = @$line;
                    for ( my $x = 0 ; $x < @columns ; $x++ ) {
                        $total_unit_column  = $x     if ( $line->[$x] eq "Total" );
                        $total_price_column = $x + 1 if ( $line->[$x] eq "Total" );
                    }

                }
                my $country  = "US";
                my $currency = "USD";
                my $product  = RPS::File::Sale::TYPE_TRACK;
                my $format   = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $artist   = $line->[ $offsets->{artist} ];
                my $track    = $line->[ $offsets->{track} ];
                my $price    = $line->[$total_price_column];
                my $units    = $line->[$total_unit_column];
                $price /= $units if ( $units != 0 );
                $artist = $last_artist if ( $line->[ $offsets->{artist} ] eq "" && $last_artist ne "" );

                if ( $units && $country && $price && $track ne "" && $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($track);
                    $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:".$track."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my ( $month, $monthName, $year, $passed_date, $dateBegin, $dateEnd );
    $passed_date = shift;
    if ( $passed_date =~ m/^(\w+) (\d{4}) / ) {
        $monthName = $1;
        $year      = $2;
        $month     = Decode_Month($monthName);
    }
    if ( $month && $year ) {
        return ( $year . "-" . sprintf( "%02d", $month ) . "-01",
            $year . "-" . sprintf( "%02d", $month ) . "-" . Days_in_Month( $year, $month ) );
    }

    return undef;
}

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