#---------------------------------------------------------------
# ____                   _ _         ____  _                    
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___ 
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/                            
#
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::Import::MusicloadALC;

use strict;

use Date::Calc qw(Days_in_Month Add_Delta_Days Decode_Month);

use lib '/app/tools/rps/lib';
use RPS::DB::Item::Client;

use lib '/app/tools/common/lib';
use Common::Consts;
use Common::Assert;
use Common::Log;
use Common::Client;

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

use lib '/app/tools/rps/lib';
use RPS::File::Sale;
use RPS::DB::Item::MediaType;

use lib '/app/tools/sale_import/lib';
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

my %fieldMap = (
        2 => {
            label           => 1,  
            units           => 3,            
            unit_price       => 4,                       
            service_id      => 6,    
            artist          => 7,    
            track_title     => 8,    
            album_title     => 8,                                
            isrc            => 9,                                
            upc             => 10, 
            catalog_id      => 11,            
             
        },        
);


#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;

    assert(defined $args{sheets});
    assert(defined $version);

    my $offsets = $fieldMap{$version};
    my $sheet_names = $args{sheet_names};
    my $sheets = $args{sheets};
    my $linenum = 1;

    my $currencyCode = 'EUR';
    my $mediaType = RPS::DB::Item::MediaType::kMediaTypeAudio;
    my $format = RPS::File::Sale::FORMAT_DOWNLOAD;      
       
    my ($country) = $fileName =~ m/^ALC_(\D\D)_/;
    if (!$country)
    {
        ($country) = $fileName =~ m/^ALC_\w*-(\D\D)_/;
    }   

    if (!$country)
    {
        print STDERR "Couldn't get country from filename: ". $fileName . "\n";
        $self->errstr("couldn't get country from filename: ". $fileName);
        return undef;    
    } 
             
    my ($dateBegin, $dateEnd) = $self->_getDates(date_begin => $fileName);
    
    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;
    }    

    for (my $sheetIndex = 0; $sheetIndex < scalar @{$args{sheets}}; $sheetIndex++)
    {
        my $sheet = $sheets->[$sheetIndex];
       
        for (my $lineIndex = 0; $lineIndex < scalar @$sheet; $lineIndex++)
        {
            $linenum = $lineIndex + 1;
            my $line = $sheet->[$lineIndex]; 
           
            my $upc                 = $line->[$offsets->{upc}];
            my $artist              = $line->[$offsets->{artist}];
            my $serviceProductID    = $line->[$offsets->{service_id}];
            my $catalogID = $line->[$offsets->{catalog_id}];
            my $label               = $line->[$offsets->{label}];
            
            my $isrc                = $line->[$offsets->{isrc}];
            my $trackTitle;
            my $albumTitle;  
            my $productType;          
            
            if ($isrc) {
                $trackTitle  = $line->[$offsets->{track_title}];
                $productType = RPS::File::Sale::TYPE_TRACK;
            }
            else
            {
                $albumTitle  = $line->[$offsets->{album_title}];
                $productType = RPS::File::Sale::TYPE_ALBUM;
            }            
            
            # Need to convert these to US style formatting.
            #
            my $units               = $line->[$offsets->{units}];
            $units =~ s/\.//;
            my $unitPrice           = $line->[$offsets->{unit_price}];
            $unitPrice =~ s/,/\./;

            # Probably wise to allow for blank lines...
            #
            if (($units > 0) && $artist && $unitPrice && ($albumTitle || $trackTitle))
            {          

                my $sale = RPS::Import::SaleRec->new();

                $sale->mediaType($mediaType);
                $sale->productType($productType);
                $sale->formatType($format);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->labelName($label);
                $sale->trackName($trackTitle);
                $sale->albumName($albumTitle);
                $sale->artistName($artist);
                $sale->serviceProductID($serviceProductID);
                $sale->clientProductID($catalogID);
                $sale->upc($upc);
                $sale->isrc($isrc);
                $sale->units($units);
                $sale->price($unitPrice);
                $sale->countryCode($country);
                $sale->currencyCode($currencyCode);
                $sale->lineNum($linenum);

                $self->_insertSale(sale => $sale);
            } 
        }
    }

	return $linenum;
}



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