package RPS::Import::TNZ;

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 = (
    'Tru-Tone'    => RPS::File::Sale::FORMAT_RINGTONE,
    'Caller Tune' => RPS::File::Sale::FORMAT_RINGTONE,
);

my %fieldMap = (
    1 => {
        service_product_id => 0,
        format             => 1,
        title              => 2,
        artist             => 3,
        label              => 4,
        units              => 8,
        price              => 11,
    },
);

#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 $artist          = $line->[ $offsets->{artist} ];
                my $title           = $line->[ $offsets->{title} ];
                my $country         = "NZ";
                my $format          = $format_type{ $line->[ $offsets->{format} ] };
                my $prodtype        = RPS::File::Sale::TYPE_TRACK;
                my $service_product = $line->[ $offsets->{service_product_id} ];
                my $units           = $line->[ $offsets->{units} ];
                my $price           = $line->[ $offsets->{price} ];
                my $currency        = "NZD";
                my $label           = $line->[ $offsets->{label} ];
                $price /= $units if ( $units != 0 );

                if ( $units && $price && $title && $title !~ /Content Title/i && $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->artistName($artist)                if ( $artist ne "" );
                    $sale->labelName($label)                  if ( $label ne "" );
                    $sale->trackName($title)                  if ( $title ne "" );
                    $sale->serviceProductID($service_product) if ( $service_product ne "" );
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $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:".$title." 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/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(\d+)\D(\d+)/ ) {
            $year  = $1;
            $month = $2;
        } elsif ( $passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/ ) {
            $month = $1;
            $year  = $3;
        }
    } 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 = $1;
    } 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.
###
