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

package RPS::Import::Mercury;

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 lib '/app/tools/rps/lib';
use RPS::DB::Item::MediaType;

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

my %fieldMap = (
        1 => {
            service_product_id => 0,
            track_title        => 1,
            artist             => 2,
            album_title        => 3,
            label              => 5,
            format             => 6,
            date_begin         => 7,
            date_end           => 8,
            units              => 9,
            net_price          => 13,
            isrc               => 15,         
            upc                => 16,
            country_code       => 18,
            currency_code      => 19,
            catalog_id         => 20,
        },
        2 => {
            service_product_id => 0,
            track_title        => 1,
            artist             => 2,
            album_title        => 3,
            format             => 5,
            date_begin         => 6,
            date_end           => 7,
            units              => 8,
            net_price          => 12,
            isrc               => 14,         
            upc                => 15,
            country_code       => 17,
            currency_code      => 18,
        },        
);

my %gProductFormatMap = (
    'audio' =>
    {
        formatType => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'video' =>
    {
        formatType => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeVideo,
    },    
    'realmusic' =>
    {
        formatType => RPS::File::Sale::FORMAT_RINGTONE,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'tru-tone' =>
    {
        formatType => RPS::File::Sale::FORMAT_RINGTONE,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },   
    'caller tune' =>
    {
        formatType => RPS::File::Sale::FORMAT_RINGBACK,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },             
); 


#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 $productType = RPS::File::Sale::TYPE_TRACK; 

    for (my $sheetIndex = 0; $sheetIndex < scalar @{$args{sheets}}; $sheetIndex++)
    {
        my $sheet = $sheets->[$sheetIndex];

        my $foundHeader = 0;
        for (my $lineIndex = 0; $lineIndex < scalar @$sheet; $lineIndex++)
        {
            $linenum = $lineIndex + 1;
            my $line = $sheet->[$lineIndex];


            if (! $foundHeader)
            {
                if ('SupplierReference' eq $line->[$offsets->{service_product_id}])
                {
                    $foundHeader = 1;
                }
                next;
            }
            

            my $country             = uc($line->[$offsets->{country_code}]);
            my $currencyCode        = uc($line->[$offsets->{currency_code}]);
            my $trackTitle          = $line->[$offsets->{track_title}];
            my $albumTitle          = $line->[$offsets->{album_title}];
            my $upc                 = $line->[$offsets->{upc}];
            my $isrc                = $line->[$offsets->{isrc}];
            my $artist              = $line->[$offsets->{artist}];        
            my $serviceProductID    = $line->[$offsets->{service_id}];
            my $units               = $line->[$offsets->{units}];
            my $netPrice            = $line->[$offsets->{net_price}];
            
            my $label;
            my $catalogID;           
            if ($version == 1)
            {
                $label     = $line->[$offsets->{label}];
                $catalogID = $line->[$offsets->{catalog_id}];                                
            }

            my $format;
            my $mediaType;

            my $mapping = $gProductFormatMap{ lc($line->[$offsets->{format}]) };
            if ($mapping)
            {
                $format = $mapping->{formatType};
                $mediaType = $mapping->{mediaType};
            }
            
                       
            # Probably wise to allow for blank lines...
            #
            if ($units && $artist && ($albumTitle || $trackTitle))
            {          
                my ($dateBegin, $dateEnd) = $self->_getDates(date_begin => $line->[$offsets->{date_begin}], date_end => $line->[$offsets->{date_end}]);
                
                my $unitPrice = $netPrice / $units;

                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->upc($upc);
                $sale->isrc($isrc);
                $sale->units($units);
                $sale->price($unitPrice);
                $sale->countryCode($country);
                $sale->currencyCode($currencyCode);
                $sale->clientProductID($catalogID);
                $sale->lineNum($linenum);

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

	return $linenum;
}



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