package RPS::Import::ShockHound2;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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

use lib '/app/tools/data_classes/lib';
use Client::Service;

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

my %field_map = (
    4 =>
    {
        label  => 1,
        type   => 2,
        album  => 4,
        track  => 5,
        units  => 7,
        revenue=> 9,       
    },
    5 =>
    {
        svc_prod_id => 4,
        album => 5,
        upc=>9,
        label=>11,
        artist=>14,
        track=>15,
        isrc=>18,
        type=>21,
        currency_code=>22,
        country_code=>23,
        units=>26,
        revenue=>31,
    }
);

#public methods

sub _importLines
{
    my $self = shift;
    my %args = @_;

    my $lines = $args{lines};
    my $version = $args{file}->VersionNum();
    my $fileName = $args{file}->OrigFileName();
    my @sheets = @{ $args{sheets} };

    return undef unless ($lines);
    
    my ($dateBegin, $dateEnd) = 
    $self->_getDates(date_begin=>$fileName, type=>'numerical');
    
    unless ($dateBegin && $dateEnd) {
       $self->errstr("couldn't get dates from filename $fileName");
       print STDERR "$fileName\n";
       return undef;
    }  
    
    my $linenum = 1;    
        
    my $offsets = $field_map{$version};
    my %errors = ();   
    
    
    my $format = RPS::File::Sale::FORMAT_DOWNLOAD;
    
    foreach my $line(@$lines)
    {
        my $f_type = $line->[$offsets->{type}];
        
        my $type;
        my $track;
        my $album;
        
        my $artist = $line->[$offsets->{artist}];  
        my $isrc   => normalize_isrc($line->[$offsets->{isrc}]);
        my $currency_code = $line->[$offsets->{currency_code}] if defined $offsets->{currency_code};
        my $country_code = 'US';
        
        $country_code = $line->[$offsets->{country_code}] if(defined $offsets->{country_code});
              
        my $units = $line->[$offsets->{units}];
        my $label = $line->[$offsets->{label}];
        my $revenue = $line->[$offsets->{revenue}];
        
        if($f_type =~ /album/i){
            $type = RPS::File::Sale::TYPE_ALBUM;            
        }
        elsif($f_type =~ /track/i){
            $type = RPS::File::Sale::TYPE_TRACK;            
        }
       
        $album = $line->[$offsets->{album}];
        $track = $line->[$offsets->{track}];       
        
            
        
        if($units =~ /\d+/ && $label && $type && $revenue && ($album || $track)) 
        {           
            
            my $price = ($revenue / $units) if($units > 0);
            
            my $sale = RPS::Import::SaleRec->new();
            
            $sale->artistName($artist);
            $sale->isrc($isrc);
            $sale->currencyCode($currency_code);
            $sale->countryCode($country_code);

            $sale->productType($type);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->trackName($track) if($type eq RPS::File::Sale::TYPE_TRACK);
            $sale->albumName($album) if($type eq RPS::File::Sale::TYPE_ALBUM);
            $sale->labelName($label);
            $sale->units($units);
            $sale->price($price);
            $sale->lineNum($linenum);
            
            #$args{dumper}($sale);  ## for cmd-line testing
            $self->_insertSale(sale => $sale);
        }
        
        $linenum++;
    }
    

    return $linenum;
}

#
# Private methods
#

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