package RPS::Import::TouchNGoRevisedV1;

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 = (
        1 => {
            service_id      => 0,
            units           => 1,
            artist          => 3,
            title           => 4,
            label           => 5,
            price           => 7,
        },                
);

#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(date_begin=>$fileName, type=>'us');
  
  unless (defined $dateBegin && defined $dateEnd) {
      print STDERR "Couldn't get dates from filename: ". $fileName . "\n";
      $self->errstr("couldn't get dates from filename: ". $fileName);
      return undef;
  }

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

            foreach my $line (@$sheet) {
                my $country             = "US";
                my $currency            = "USD";
                my $product             = RPS::File::Sale::TYPE_TRACK;
                my $artist              = $line->[$offsets->{artist}];
                my $format              = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $title               = $line->[$offsets->{title}];
                my $label               = $line->[$offsets->{label}];
                my $units               = $line->[$offsets->{units}]; 
                my $price               = $line->[$offsets->{price}];
                my $service_id          = $line->[$offsets->{service_id}];
                my $upc                 = $service_id if($service_id =~ /^(\d)/);
                my $isrc                = $service_id if($service_id !~ /^(\d)/);
                $product                = RPS::File::Sale::TYPE_ALBUM if($service_id =~ /^(\d)/);
                $price                  /= $units if($units != 0);

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

                    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) if($product eq RPS::File::Sale::TYPE_TRACK);
                    $sale->upc($upc) if($product eq RPS::File::Sale::TYPE_ALBUM);
                    $sale->artistName($artist);
                    $sale->trackName($title) if($product eq RPS::File::Sale::TYPE_TRACK);
                    $sale->albumName($title) if($product eq RPS::File::Sale::TYPE_ALBUM);
                    $sale->labelName($label);
                    $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:".$title."\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.*$//;
#    if($passed_date =~ m/_(\d\d)_(\d\d)_(\d\d)/) {
#            $year   = $3+2000;
#            $month  = $1;
#            $day    = $2;
#    }
#    if($year && $month && $day) {
#        return ($year."-".sprintf("%02d",$month)."-01",$year."-".sprintf("%02d",$month)."-".sprintf("%02d",$day));
#    }

#    return undef;
#}

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