package RPS::Import::HipHopSite;

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 %format_type = ( D => RPS::File::Sale::FORMAT_DOWNLOAD, );

my %product_type = (
    'Audio Track' => RPS::File::Sale::TYPE_TRACK,
    'Album'       => RPS::File::Sale::TYPE_ALBUM,
);

my %fieldMap = (
    1 => {
        artist             => 0,
        title              => 1,
        service_product_id => 2,
        upc                => 3,
        isrc               => 4,
        units              => 6,
        price              => 8,
    },
);

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

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

            foreach my $line (@$sheet) {
                my $artist          = $line->[ $offsets->{artist} ];
                my $title           = $line->[ $offsets->{title} ];
                my $country         = "US";
                my $format          = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $isrc            = $line->[ $offsets->{isrc} ];
                my $upc             = $line->[ $offsets->{upc} ];
                my $service_product = $line->[ $offsets->{service_product_id} ];
                my $units           = $line->[ $offsets->{units} ];
                my $price           = $line->[ $offsets->{price} ];
                my $currency        = "USD";
                my $free            = 0;
                my ( $album, $prodtype );
                $price /= $units if ( $units != 0 );
                $free     = 1                           if ( $price == 0 );
                $prodtype = RPS::File::Sale::TYPE_ALBUM if ( $price > 2 );
                $prodtype = RPS::File::Sale::TYPE_TRACK if ( $price < 3 );
                $isrc     = $service_product;
                $isrc =~ s/\-//g;

                if ( $units && $price ne "" && $title && $title !~ /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;
                    }

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

                    $sale->productType($prodtype);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->isrc( Common::Util::normalize_isrc($isrc) ) if ( $isrc ne "" );
                    $sale->upc( Common::Util::normalize_upc($upc) )    if ( $upc ne "" );
                    $sale->artistName($artist);
                    $sale->trackName($title) if ( $title ne "" && $prodtype eq RPS::File::Sale::TYPE_TRACK );
                    $sale->albumName($title) if ( $title ne "" && $prodtype eq RPS::File::Sale::TYPE_ALBUM );
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->free($free);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

                    #$args{dumper}($sale); ## for cmd-line testing
                    $self->_insertSale( sale => $sale );
                }

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

#
# Private methods
#

sub _getDates {
    my ( $month, $monthName, $year );
    my $passed_date = shift;
    if ( $passed_date =~ m/(\d{4})/ ) {
        $year = $1;
    }
    if ( $month eq "" ) {
        $monthName = "jan" if ( $passed_date =~ /jan/i );
        $monthName = "feb" if ( $passed_date =~ /feb/i );
        $monthName = "mar" if ( $passed_date =~ /mar/i );
        $monthName = "apr" if ( $passed_date =~ /apr/i );
        $monthName = "may" if ( $passed_date =~ /may/i );
        $monthName = "jun" if ( $passed_date =~ /jun/i );
        $monthName = "jul" if ( $passed_date =~ /jul/i );
        $monthName = "aug" if ( $passed_date =~ /aug/i );
        $monthName = "sep" if ( $passed_date =~ /sep/i );
        $monthName = "oct" if ( $passed_date =~ /oct/i );
        $monthName = "nov" if ( $passed_date =~ /nov/i );
        $monthName = "dec" if ( $passed_date =~ /dec/i );
        $month = Decode_Month($monthName) if ( $month eq "" && $monthName ne "" );
    }

    #print STDERR "|->".$passed_date."<>".$monthName."<>".$month."<>".$year."<-|\n";
    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.
###
