package RPS::Import::DigiRama;

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 => {
        date    => 0,
        product => 1,
        isrc    => 2,
        artist  => 3,
        title   => 4,
        label   => 5,
        price   => 6,
    },
);

#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} } ) {
        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      = "NZ";
                my $format       = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $prodtype     = $product_type{ $line->[ $offsets->{product} ] };
                my $units        = 1;
                my $price        = $line->[ $offsets->{price} ];
                my $currency     = "NZD";
                my $album        = $line->[ $offsets->{album} ] if ( defined $offsets->{album} );
                my $album_artist = $line->[ $offsets->{album_artist} ];
                my $label        = $line->[ $offsets->{label} ];
                my ( $upc, $isrc );
                $price =~ s/\$//;

                if ( $prodtype eq RPS::File::Sale::TYPE_ALBUM ) {
                    $album = $title;
                    $title = "";
                    $upc   = $line->[ $offsets->{isrc} ];
                    $isrc  = "";
                } else {
                    $isrc = $line->[ $offsets->{isrc} ];
                    $isrc =~ s/\-//g;
                }
                if (   $units
                    && $price
                    && ( $title && $title !~ /title/i || $album && $album !~ /albumtitle/i )
                    && $format
                    && $line->[ $offsets->{date} ] ne "" ) {
                    my ( $dateBegin, $dateEnd ) = $self->_getDates( $line->[ $offsets->{date} ] )
                      if ( defined $offsets->{date} && $line->[ $offsets->{date} ] ne "" );

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

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

                    $sale->productType($prodtype);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->isrc($isrc) if ( $isrc ne "" );
                    $sale->upc($upc)   if ( $upc ne "" );
                    $sale->artistName($artist);
                    $sale->labelName($label) if ( $label ne "" );
                    $sale->trackName($title) if ( $title ne "" );
                    $sale->albumName($album) if ( $album ne "" );
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $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 $self        = shift;
    my $passed_date = shift;
    if ( $passed_date =~ m/2(\d{3})/ && $passed_date !~ m/(\d{4})\D(\d+)\D(\d)/ && $passed_date !~ m/(\d+)\D(\d+)\D(\d{4})/ ) {
        $year = "2" . $1;
        if ( $passed_date =~ m/(\d{4})(\d\d)(\d\d)/ ) {
            $year  = $1;
            $month = $2;
        } elsif ( $passed_date =~ m/(\d{4})\.(\d+)/ ) {
            $year  = $1;
            $month = $2;
        }
    } elsif ( $passed_date =~ m/(\d{4})\.(\d+)/ ) {
        $year  = $1;
        $month = $2;
    } elsif ( $passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/ ) {
        $year  = $1;
        $month = $2;
    } elsif ( $passed_date =~ m/(\d{4})\s(\w+)/ ) {
        $year      = $1;
        $monthName = $2;
    } elsif ( $passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/ ) {
        $year  = $3;
        $month = $2;
    } elsif ( $passed_date =~ m/(\d{4})(\d\d)(\d\d)/ ) {
        $year  = $1;
        $month = $2;
    }
    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.
###
