package RPS::Import::VidZoneDigital2;

use strict;
use Data::Dumper;

use Date::Calc qw(Days_in_Month Decode_Month);
use Spreadsheet::ParseExcel;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_date);
use Common::Consts;

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

my %field_map = (
    2 => {
        isrc    => 0,
        artist  => 1,
        track   => 2,
        format  => 6,
        units   => 8,
        price   => 9,        
        country => 10,
    },
);

my %service_map = (

);

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

#public methods

sub _importLines
{
	my $self = shift;
	my %args = @_;
  my $fileObj = $args{file};
  my $version = $args{file}->VersionNum();
  my $fileName = $args{file}->OrigFileName();
  my $offsets = $field_map{$version};
  my $sheet_count = 0;
  return undef unless (defined $args{sheets} && defined $version);

  my $sheet_names = $args{sheet_names};
  my $currency    = "GBP";
  
	my ($dateBegin,$dateEnd,$linenum,%conversion);
  ($dateBegin,$dateEnd) = _getDates($fileName);
  
  unless ($dateBegin && $dateEnd) {
    print STDERR "Couldn't find dates from:".$fileName."\n";
    $self->errstr("Couldn't find dates from:".$fileName);
    return undef;
  }

  foreach my $sheet(@{ $args{sheets} }) {
    if($sheet_names->[$sheet_count] =~ /summary/i) {
      my ($area_column,$conversion_rate_column);
      foreach my $line(@$sheet) {
        my $x = 0;
        foreach my $column(@$line) {
          $area_column = $x if($column =~ /TERRITORY/i); 
          $conversion_rate_column = $x if($column =~ /CONVERSION RATE/i);
          $x++;
        }
        $conversion{$line->[$area_column]} = $line->[$conversion_rate_column] if($area_column ne "" && $conversion_rate_column ne "" && $line->[$area_column] ne "" && $line->[$conversion_rate_column] ne "" && $line->[$conversion_rate_column] ne "n/a");
      } 
    } 
    elsif($sheet_names->[$sheet_count] !~ /summary/i) 
    {		  
      $linenum = 1;          

	    foreach my $line (@$sheet) {
        my $crate = 1;	        	
  	
  		  my $country = $line->[$offsets->{'country'}];
  		  $country = $Common::Consts::COUNTRY_CODE{lc $country};
  		  
  		  my $conv_country;
  		  # VidZone used SA for South Africa, instead of ZA.
  		  #
  		  if ($country eq 'ZA')
  		  {
  		  	$conv_country = 'SA';
  		  }
  		  else 
  		  {
  		  	$conv_country = $country;
  		  }
  	
	      my $isrc        = $line->[$offsets->{'isrc'}];
	      my $artist		= $line->[$offsets->{'artist'}];
			  my $title		= $line->[$offsets->{'track'}];
			  my $units		= $line->[$offsets->{'units'}];
			  my $price	    = $line->[$offsets->{'price'}];
	      my $format;
	      $format      = RPS::File::Sale::FORMAT_DOWNLOAD if($line->[$offsets->{format}] =~ /track/i);
	      $format      = RPS::File::Sale::FORMAT_RINGTONE if($line->[$offsets->{format}] =~ /tune/i);
        $format      = RPS::File::Sale::FORMAT_STREAM if($line->[$offsets->{format}] =~ /stream/i);	       
        $price       /= $units if($units != 0);
	
	  		if ($price && $units && $artist && $artist ne "ARTIST" && $title && $title ne "TITLE") {
	  		   $crate = $conversion{$conv_country} if($conversion{$conv_country} ne "");
	         $price /= $crate;

		        my $sale = RPS::Import::SaleRec->new();
		
				    $sale->productType(RPS::File::Sale::TYPE_TRACK);
				    $sale->formatType($format);
		        $sale->isrc($isrc);
				    $sale->dateBegin($dateBegin);
				    $sale->dateEnd($dateEnd);
		        $sale->currencyCode($currency);
		        $sale->countryCode($country);
				    $sale->artistName($artist);
				    $sale->trackName($title);
				    $sale->units($units);
				    $sale->price($price);
				    $sale->lineNum($linenum);
				    $self->_insertSale(sale => $sale);
			  }
			  
			  $linenum++;
		  }
      #else { print STDERR "skip s:".$sheet_names->[$sheet_count]." l:".$linenum." u: $units p: $price a: $artist t: $title\n"; }
      
	  }
    $sheet_count++;
  }

  #print STDERR Dumper %conversion;

	return $linenum;
}

#
# Private methods
#

sub _getDates {
    my $passed_date = shift;
    my ($month,$monthName,$emonth,$year,$qtr,$dateBegin,$dateEnd);
    $monthName = $passed_date;
    if($passed_date =~ m/(\d{4}) (\d+)\-(\d+)/) {
        $year = $1;
        $month  = $2;
        $emonth = $3;
    }
    elsif($passed_date =~ m/(\d{4}) (\d+)\.xls/) {
        $year = $1;
        $month = $2;
    }
    elsif($passed_date =~ m/(\w+)_(\d{4})/) {
        $year = $2;
        $monthName = $1;
    }
    elsif($passed_date =~ m/ (\w+) (\d{4})\.xls/) {
        $year = $2;
        $monthName = $1; 
    }
    $monthName =~ s/^.*_//g;
    $monthName = "jan" if($monthName =~ /jan/i);
    $monthName = "feb" if($monthName =~ /feb/i);
    $monthName = "mar" if($monthName =~ /mar/i);
    $monthName = "apr" if($monthName =~ /apr/i);
    $monthName = "may" if($monthName =~ /may/i);
    $monthName = "jun" if($monthName =~ /jun/i);
    $monthName = "jul" if($monthName =~ /jul/i);
    $monthName = "aug" if($monthName =~ /aug/i);
    $monthName = "sep" if($monthName =~ /sep/i);
    $monthName = "oct" if($monthName =~ /oct/i);
    $monthName = "nov" if($monthName =~ /nov/i);
    $monthName = "dec" if($monthName =~ /dec/i);
    $month = Decode_Month($monthName) if($month eq "" && $monthName ne "");
    if($month ne "" & $year ne "") {
        $dateBegin = $year."-".$month."-01";
        $dateEnd = $year."-".$month."-".sprintf("%02d",Days_in_Month($year,$month));
        $dateEnd = $year."-".$emonth."-".sprintf("%02d",Days_in_Month($year,$emonth)) if($emonth ne "");
        return ($dateBegin,$dateEnd);
    }
    else {
        return undef;
    }
}

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