package RPS::Import::StarCon;

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 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 => {
        label       => 0,
        format      => 7,
        serviceName => 8,
        product     => 11,
        album       => 13,
        upc         => 14,
        artist      => 12,
        track       => 16,
        isrc        => 17,
        units       => 22,
        price       => 23,

    },
);

my %format_map = (
    'ad supported streams'           => RPS::File::Sale::FORMAT_STREAM,
    'kiosk'                          => RPS::File::Sale::FORMAT_DOWNLOAD,
    'ota permanent downloads'        => RPS::File::Sale::FORMAT_DOWNLOAD,
    'ota video downloads'            => RPS::File::Sale::FORMAT_DOWNLOAD,    # was FORMAT_VIDEO (FB1324)
    'permanent downloads'            => RPS::File::Sale::FORMAT_DOWNLOAD,
    'portable sub client plays'      => RPS::File::Sale::FORMAT_STREAM,
    'portable sub device plays'      => RPS::File::Sale::FORMAT_STREAM,
    'portable sub streams'           => RPS::File::Sale::FORMAT_STREAM,
    'programmed streaming'           => RPS::File::Sale::FORMAT_STREAM,
    'ringbacks'                      => RPS::File::Sale::FORMAT_RINGBACK,
    'standard mobile ringsounds'     => RPS::File::Sale::FORMAT_RINGTONE,
    'streams'                        => RPS::File::Sale::FORMAT_STREAM,
    'subscription mastertones'       => RPS::File::Sale::FORMAT_RINGTONE,
    'tethered/conditional downloads' => RPS::File::Sale::FORMAT_TETHERED,
    'video downloads'                => RPS::File::Sale::FORMAT_DOWNLOAD,    # was FORMAT_VIDEO (FB1324)
    'video streams'                  => RPS::File::Sale::FORMAT_STREAM,      # was FORMAT_VIDEOSTREAM (FB1324)
);

#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 ) = $self->_getDates($fileName);

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

            foreach my $line (@$sheet) {
                my $free     = 0;
                my $country  = "US";
                my $currency = "USD";
                my $artist   = $line->[ $offsets->{artist} ];
                my $track    = $line->[ $offsets->{track} ];
                my $album    = $line->[ $offsets->{album} ];
                my $format   = $line->[ $offsets->{format} ];
                my $product  = $line->[ $offsets->{product} ];
                my $units    = $line->[ $offsets->{units} ];
                my $price    = $line->[ $offsets->{price} ];
                my $upc      = $line->[ $offsets->{upc} ];
                my $isrc     = $line->[ $offsets->{isrc} ];
                my $label    = $line->[ $offsets->{label} ];
                $format =~ s/\s\(.*$//;
                $price /= $units if ( $units != 0 );
                $free = 1 if ( $price == 0 );
                my $formatted = $format_map{ lc($format) };

                if ( $units && ( $track || $album ) && $track !~ /Track Title/i ) {

                    unless ( defined $dateBegin && defined $dateEnd ) {
                        print STDERR "Couldn't get dates from: " . $fileName . "\n";
                        $self->errstr( "couldn't get dates from: " . $fileName );
                        return undef;
                    }
                    unless ( $formatted ne "" ) {
                        print STDERR "Couldn't get format for: " . $line->[ $offsets->{format} ] . "(" . $linenum . ")\n";
                        $self->errstr( "Couldn't get format for: " . $line->[ $offsets->{format} ] . "(" . $linenum . ")" );
                        return undef;
                    }
                    unless ( $product =~ /(A|T)/ ) {
                        print STDERR "Couldn't get get product for: " . $line->[ $offsets->{product} ] . "(" . $linenum . ")\n";
                        $self->errstr( "Couldn't get get product for: " . $line->[ $offsets->{product} ] . "(" . $linenum . ")" );
                        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->labelName($label);
                    $sale->trackName($track);
                    $sale->albumName($album);
                    $sale->formatType($formatted);
                    $sale->isrc($isrc);
                    $sale->upc($upc);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->free($free);
                    $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, $monthName, $year );
    my $self        = shift;
    my $passed_date = shift;

    #print STDERR "|->".$passed_date."<-|\n";
    if ( $passed_date =~ m/(\w{3})(\d\d)/ ) {
        $year      = 2000 + $2;
        $monthName = $1;
    }
    if ( $month eq "" && $monthName ne "" ) {
        $month = Decode_Month($monthName) if ( $month eq "" && $monthName ne "" );
    }
    my $dateBegin = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
    my $dateEnd = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
    return ( $dateBegin, $dateEnd );
    return undef;
}

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