package RPS::Import::WE7_BF;

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::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

my %fieldMap = (
    2 => {
        date_row   => 0,
        date_start => 1,
        date_end   => 2,
        format     => 0,
        isrc       => 1,
        upc        => 2,
        product    => 6,
        units      => 8,
        price      => 10,
        currency   => 11,
        country    => 12,
        track      => 13,
        album      => 14,
        artist     => 15,
    },
);

#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;
        my $f_date_begin = $sheet->[ $offsets->{date_row} ]->[ $offsets->{date_start} ];
        my $f_date_end   = $sheet->[ $offsets->{date_row} ]->[ $offsets->{date_end} ];

        ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $f_date_begin, date_end => $f_date_end, type => 'iso' );

        if ( $sheet_names->[$sheet_count] !~ /summary/i ) {

            foreach my $line (@$sheet) {
                my $country  = $line->[ $offsets->{country} ];
                my $currency = $line->[ $offsets->{currency} ];
                my $product  = $line->[ $offsets->{product} ];
                my $artist   = $line->[ $offsets->{artist} ];
                my $isrc     = $line->[ $offsets->{isrc} ];
                my $upc      = $line->[ $offsets->{upc} ];
                my $track    = $line->[ $offsets->{track} ];
                my $album    = $line->[ $offsets->{album} ];
                my $format   = uc( $line->[ $offsets->{format} ] );
                my $units    = $line->[ $offsets->{units} ];
                my $price    = $line->[ $offsets->{price} ];
                $isrc =~ s/\-//g;
                $price /= $units if ( $units != 0 );

                if ( $product eq 't' ) {
                    $product = RPS::File::Sale::TYPE_TRACK;
                } else {
                    $product = RPS::File::Sale::TYPE_ALBUM;
                }

                if ( $format ne "H" && $units && $country && $price && $linenum > 1 ) {

                    unless ( defined $dateBegin && defined $dateEnd ) {
                        print STDERR "Couldn't get dates from: " . $sheet->[ $offsets->{date_row} ]->[ $offsets->{date_start} ] . "\n";
                        $self->errstr( "couldn't get dates from: " . $sheet->[ $offsets->{date_row} ]->[ $offsets->{date_end} ] );
                        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->isrc($isrc);
                    $sale->upc($upc);
                    $sale->artistName($artist);
                    $sale->trackName($track);
                    $sale->albumName($album);
                    $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 . " - c:".$country." u:".$units." p:".$price."t:".$track." f:".$format."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
    return $linenum;
}

#
# Private methods
#

#sub _getDates
#{
#    my ($year,$month,$day,$monthName,$qtr,$passed_date,$dateBegin,$dateEnd);
#	$passed_date = shift;
#    $passed_date =~ s/\s.*$//;
#    print STDERR "|>".$passed_date."<|\n";
#    if($passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/) {
#            $year   = $1;
#            $month  = $2;
#            $day    = $3;
#    }
#    if($year && $month && $day) {
#        return ($year."-".sprintf("%02d",$month)."-".sprintf("%02d",$day));
#    }

#    return undef;
#}

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