package RPS::Import::DrumAndBassArena;

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 = (
    1 =>
    {
        type => 0,
        artist => 1,
        title => 2,
        upc => 3,
        isrc => 4,
        units => 6,
        price => 7,
        revenue => 13,     
    },
    2 =>
    {
        type        => 0,
        label       => 0,
        catalog_no  => 1,
        artist      => 2,
        title       => 3,
        upc         => 4,
        isrc        => 5,
        units       => 7,
        price       => 8,
        revenue     => 14,     
    },
);

#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 $linenum = 1;
            
    my $offsets = $field_map{$version};
    my %errors = ();
    
    $fileName =~ s/_//g;
    
    my ($dateBegin, $dateEnd) = 
    $self->_getDates(date_begin=>$fileName, type=>'quarter');
    
    my $format = RPS::File::Sale::FORMAT_DOWNLOAD;
    my $currency = "GBP";
    my $country = "GB";
    
    my $label;
    foreach my $lines(@sheets)
    {
        
        $linenum = 1;
        
        foreach my $line(@$lines)
        {
            my $type = $line->[$offsets->{type}];
            my $artist = $line->[$offsets->{artist}];
            my $title = $line->[$offsets->{title}];
            my $upc = $line->[$offsets->{upc}];
            my $isrc = $line->[$offsets->{isrc}];
            my $units = $line->[$offsets->{units}];
            my $revenue = $line->[$offsets->{revenue}];
            my $album;
            my $track;
            my $productType;
            my $catalogNumber;
            if (defined $offsets->{catalog_no})
            {
                $catalogNumber = $line->[$offsets->{catalog_no}];
            }

            if (defined $offsets->{label})
            {
                if ($line->[$offsets->{label}] =~ /Label: (.+)/)
                {
                    $label = $1;
                    next;
                }
            }
        
            if($type && $artist && $title && $units =~ /\d+/ && $revenue =~ /\d+/) 
            {
#                unless ($dateBegin && $dateEnd) {
#                    $self->errstr("couldn't get dates from filename: $fileName");
#                    print STDERR "couldn't get dates from filename: $fileName";
#                    return undef;
#                }               
                
                if($type =~ /album/i){
                    $productType = RPS::File::Sale::TYPE_ALBUM;
                    $album = $title;              
                }
                elsif($type =~ /entire release/i)
                {
                    $productType = RPS::File::Sale::TYPE_ALBUM;
                    $album = $title;
                }
                elsif($type =~ /track/i){
                    $productType = RPS::File::Sale::TYPE_TRACK;
                    $track = $title;
                }
                else{
                    die "Unknown product type: $type at line num: $linenum";
#                    $self->errstr("unknown product type: $type at line num: $linenum");
#                    print STDERR "unknown product type: $type at line num: $linenum";
#                    return undef;
                }
                
                my $price = $revenue / $units if($units > 0);  
            
                my $sale = RPS::Import::SaleRec->new();
                $sale->productType($productType);
                $sale->formatType($format);
                $sale->mediaType(1);
                $sale->artistName($artist);
                $sale->albumName($album) if($album);
                $sale->trackName($track) if($track);
                $sale->upc($upc) if($upc);
                $sale->isrc($isrc) if($isrc);
                $sale->units($units);
                $sale->price($price);
                $sale->currencyCode($currency);
                $sale->countryCode($country);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->lineNum($linenum);
                $sale->serviceProductID($catalogNumber);
                $sale->labelName($label);
            
                #$args{dumper}($sale);  ## for cmd-line testing
                $self->_insertSale(sale => $sale);
            }
        
            $linenum++;
        }
    }
    

    return $linenum;
}

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