package RPS::Import::4DeeJays;

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_CODE);

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

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

my %fieldMap = (
    1 => {
        catalog => 0,
        date    => 1,
        country => 2,
        label   => 3,
        track   => 4,
        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;
    my ( $dateBegin, $dateEnd );

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

            foreach my $line (@$sheet) {
                my ( $dateBegin, $dateEnd ) = _getDates( $line->[ $offsets->{date} ] );
                my $country  = $Common::Consts::COUNTRY_CODE{ lc( $line->[ $offsets->{country} ] ) };
                my $currency = "EUR";
                my $product  = RPS::File::Sale::TYPE_TRACK;
                my $format   = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $catalog  = $line->[ $offsets->{catalog} ];
                my $label    = $line->[ $offsets->{label} ];
                my $track    = $line->[ $offsets->{track} ];
                my $price    = $line->[ $offsets->{price} ];
                my $units    = 1;
                $country = "AE" if ( $line->[ $offsets->{country} ] =~ /emirates/i );

                if ( $units && $country && $price && $track ne "Track Title" ) {

                    unless ( defined $dateBegin && defined $dateEnd ) {
                        print STDERR "Couldn't get dates from: " . $line->[ $offsets->{date} ] . "\n";
                        $self->errstr( "couldn't get dates from: " . $line->[ $offsets->{date} ] );
                        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->trackName($track);
                    $sale->labelName($label);
                    $sale->serviceProductID($catalog);
                    $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 l:" . $linenum . " - c:".$country." u:".$units." p:".$price." t:".$track."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my ( $month, $monthName, $year, $passed_date, $dateBegin, $dateEnd );
    $passed_date = shift;
    if ( $passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/ ) {
        $month = $2;
        $year  = $1;
    } elsif ( $passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/ ) {
        $month = $1;
        $year  = $3;
    }
    if ( $month && $year ) {
        return ( $year . "-" . sprintf( "%02d", $month ) . "-01",
            $year . "-" . sprintf( "%02d", $month ) . "-" . Days_in_Month( $year, $month ) );
    }

    return undef;
}

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