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

package RPS::Import::DX3;

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/data_classes/lib';

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

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

my %fieldMap = (
        1 => {
            sale_date     => 0,
            product_type  => 1,
            isrc          => 2,
            upc           => 3,
            artist        => 4,
            title         => 5,
            grid_code     => 6,
            retail_price  => 7,
            currency_code => 8,
            dist_channel  => 9,
            retail_name   => 10,
            country_code  => 11,
            price         => 12,
            quantity      => 13,
        },
        2 => {
            sale_date     => 0,
            product_type  => 1,
            isrc          => 2,
            artist        => 3,
            title         => 4,
            grid_code     => 5,
            retail_price  => 6,
            currency_code => 7,
            dist_channel  => 9,
            retail_name   => 10,
            country_code  => 11,
            price         => 12,
            quantity      => 13,
        },
        3 => {
            sale_date     => 0,
            product_type  => 1,
            upc           => 2,
            isrc          => 3,
            artist        => 4,
            title         => 5,
            grid_code     => 6,
            retail_price  => 7,
            currency_code => 8,
            dist_channel  => 9,
            retail_name   => 10,
            country_code  => 11,
            price         => 12,
            quantity      => 13,
        }, 
        4 => {
            sale_date     => 0,
            product_type  => 1,
            isrc          => 2,
            upc           => 3,
            artist        => 4,
            title         => 5,
            grid_code     => 6,
            retail_price  => 7,
            currency_code => 8,
            dist_channel  => 9,
            retail_name   => 10,
            country_code  => 11,
            price         => 12,
            quantity      => 13,
        },                     
);

my %gProductTypeMap = (
    'realtone' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType  => RPS::File::Sale::FORMAT_RINGTONE,
        mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'ft2m' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'track' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'drm free track' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'single' =>
    {
        productType => RPS::File::Sale::TYPE_ALBUM,
        formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'single track' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },    
    'album' =>
    {
        productType => RPS::File::Sale::TYPE_ALBUM,
        formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'multialbum' =>
    {
        productType => RPS::File::Sale::TYPE_ALBUM,
        formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'drm free album' =>
    {
        productType => RPS::File::Sale::TYPE_ALBUM,
        formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
        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;

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

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


            my $productType;
            my $format;
            my $mediaType;

            my $country               = uc($line->[$offsets->{country_code}]);
            my $currencyCode          = uc($line->[$offsets->{currency_code}]);
            if ($currencyCode eq 'DE')
            {
                $currencyCode = 'EUR';   
            }    
            
            my $isrc                  = $line->[$offsets->{isrc}];
            my $artist                = $line->[$offsets->{artist}];
            my $units                 = $line->[$offsets->{quantity}];
            my $netPrice              = $line->[$offsets->{price}];

            my $upc;
            if ($version != 2)
            {
                $upc = $line->[$offsets->{upc}];
            }

            my $config = lc($line->[$offsets->{product_type}]);
            
            # Case 15432 - 'single' is a track product for versions 3 and 4 only.
            # It's an album product for versions 1 and 2.
            
            if (($version == 3 || $version == 4) && $config eq 'single')
            {
                $config = 'single track';
            }
            
            my $mapping = $gProductTypeMap{ $config };
            if ($mapping)
            {
                $productType = $mapping->{productType};
                $format = $mapping->{formatType};
                $mediaType = $mapping->{mediaType};

				$productType = RPS::File::Sale::TYPE_TRACK if( $isrc && ! $upc );
            }

			my ($albumTitle, $trackTitle);
			if (RPS::File::Sale::TYPE_TRACK eq $productType)
            {
                $trackTitle = $line->[$offsets->{title}];
            }
            elsif (RPS::File::Sale::TYPE_ALBUM eq $productType)
            {
                $albumTitle = $line->[$offsets->{title}];
            }

			# Probably wise to allow for blank lines...
            #
            if ($units || $artist || $albumTitle || $trackTitle)
            {
                
                my ($dateBegin, $dateEnd) = $self->_getDates( date_begin => $line->[$offsets->{sale_date}] );
                
                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->trackName($trackTitle);
                $sale->albumName($albumTitle);
                $sale->artistName($artist);
                $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.
###
