package RPS::Import::NineSquaredTen;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);
use Spreadsheet::ParseExcel;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_date);
use Common::Consts;

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

my %field_map = (
    10 => {
        artist             => 0,
        vendor             => 2,
        track              => 4,
        service_product_id => 5,
        service            => 7,
        price              => 13,
        units              => 10,
    },
);

my %service_map = (

);

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

#public methods

sub _importLines {
    my $self        = shift;
    my %args        = @_;
    my $fileObj     = $args{file};
    my $version     = $args{file}->VersionNum();
    my $fileName    = $args{file}->OrigFileName();
    my $offsets     = $field_map{$version};
    my $sheet_count = 0;
    my $has_grid    = 0;
    return undef unless ( defined $args{sheets} && defined $version );

    my $sheet_names = $args{sheet_names};
    my ( $dateBegin, $dateEnd, $linenum );

    foreach my $sheet ( @{ $args{sheets} } ) {
        $linenum  = 1;
        $has_grid = 0;
        if ( $sheet_names->[$sheet_count] !~ /summary/i ) {
            ( $dateBegin, $dateEnd ) = $self->_getDates( $fileName . "-" . $sheet_names->[$sheet_count] );

            foreach my $line (@$sheet) {
                $has_grid++ if ( $linenum == 1 && $line->[3] =~ /grid/i );
                my $country     = "US";
                my $currency    = "USD";
                my $outlet      = $line->[ $offsets->{'vendor'} ];
                my $artist      = $line->[ $offsets->{'artist'} ];
                my $title       = $line->[ $offsets->{'track'} ];
                my $units       = $line->[ $offsets->{'units'} ];
                my $price       = $line->[ $offsets->{'price'} ];
                my $sp_id       = $line->[ $offsets->{'service_product_id'} ];
                my $serviceName = $line->[ $offsets->{'service'} ];
                if ( $has_grid == 1 ) {
                    $serviceName = $line->[ $offsets->{service} + 1 ];
                    $sp_id       = $line->[ $offsets->{service_product_id} + 1 ];
                    $units       = $line->[ $offsets->{units} + 1 ];
                    $price       = $line->[ $offsets->{price} + 1 ];
                    $title       = $line->[ $offsets->{track} + 1 ];
                }
                my $free = 0;
                $price /= $units if ( $units > 1 );
                my $serviceID = $self->getServiceID($serviceName) || $service_map{$serviceName};

                #$self->errstr("Can't find service: " . $svcName) if ($serviceID eq "");

                if ( $units && $artist && $artist ne "Author" && $title && $title ne "Product" ) {
                    my $sale = RPS::Import::SaleRec->new();
                    $free = 1 if ( $price == 0 );
                    $sale->productType(RPS::File::Sale::TYPE_TRACK);
                    $sale->formatType(RPS::File::Sale::FORMAT_RINGTONE);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->currencyCode($currency);
                    $sale->countryCode($country);
                    $sale->serviceID($serviceID) if ( $serviceID ne "" );
                    $sale->serviceProductID($sp_id);
                    $sale->artistName($artist);
                    $sale->trackName($title);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->free($free);
                    $sale->lineNum($linenum);
                    $self->_insertSale( sale => $sale );
                }

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

    return $linenum;
}

#
# Private methods
#

# Overloading, because this is a weird one...
sub _getDates {
    my $self        = shift;
    my $passed_date = shift;
    my ( $month, $monthName, $year, $qtr, $dateBegin, $dateEnd );
    $monthName = $passed_date;
    if ( $passed_date =~ m/Q(\d) (\d\d)/ ) {
        $year = $2 + 2000;
    }
    $monthName =~ s/^.*\-//g;
    $monthName =~ s/ .*$//;
    $monthName = "jan" if ( $monthName =~ /jan/i );
    $monthName = "feb" if ( $monthName =~ /feb/i );
    $monthName = "mar" if ( $monthName =~ /mar/i );
    $monthName = "apr" if ( $monthName =~ /apr/i );
    $monthName = "may" if ( $monthName =~ /may/i );
    $monthName = "jun" if ( $monthName =~ /jun/i );
    $monthName = "jul" if ( $monthName =~ /jul/i );
    $monthName = "aug" if ( $monthName =~ /aug/i );
    $monthName = "sep" if ( $monthName =~ /sep/i );
    $monthName = "oct" if ( $monthName =~ /oct/i );
    $monthName = "nov" if ( $monthName =~ /nov/i );
    $monthName = "dec" if ( $monthName =~ /dec/i );
    $month = Decode_Month($monthName) if ( $month eq "" && $monthName ne "" );

    if ( $month ne "" & $year ne "" ) {
        $dateBegin = $year . "-" . $month . "-01";
        $dateBegin = sprintf( "%04d-%02d-01", $year, $month );
        $dateEnd   = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
        return ( $dateBegin, $dateEnd );
    } else {
        return undef;
    }
}

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