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

package RPS::Import::MySpace::v2;

use strict;

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::CurrencyFormat;

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

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

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

sub _productType { 'track' }

sub _fieldMap {
    {
		country      => 1,
		date_start   => 2,
		upc          => 3,
		isrc         => 4,
		album_name   => 5,
		track_name   => 6,
		quantity     => 7,
		unit_price   => 8,
		net_price    => 9,
	}
}

sub _productTypeMap {
	{
         'track' =>
         {
             productType => RPS::File::Sale::TYPE_TRACK,
             formatType => RPS::File::Sale::FORMAT_STREAM,
             mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
         },
	}
}


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;
	my $productTypeMap = $self->_productTypeMap();

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

    my $offsets = $self->_fieldMap;
    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];

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

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

			my $country = $line->[$offsets->{country}];
			if ($country eq 'UK')
			{
			    $country = 'GB';   
			}
			
			my $currencyCode;
			
			my $startDate = $line->[$offsets->{date_start}];
			my ($dateBegin, $dateEnd) = $self->_getDates(date_begin => $startDate);
			
			# As of Nov 2009, currency code is based on country of sale.
			#
			if ($dateBegin ge "2009-11-01")
			{
                my $currencyFormat = Common::CurrencyFormat->new(countryCode => $country);
                $currencyCode = $currencyFormat->currencyCode();
                if (!$currencyCode)
                {
                    $self->errstr("Could not find country/currency: ".$country . " line:".$linenum);
                    print STDERR "Could not find country/currency: ".$country . " line:".$linenum."\n";
                    return undef;  
                }
            }
            # Before that date, currency code is always USD
            else
            {
                $currencyCode = "USD";    
            }
           
            my $productType = $self->_productType;
            my $format;
            my $mediaType;
            my $artist;

            my $trackTitle = $line->[$offsets->{track_name}];
            my $albumTitle = $line->[$offsets->{album_name}];

            my $upc                 = $line->[$offsets->{upc}];
            my $isrc                = $line->[$offsets->{isrc}];
            my $units               = $line->[$offsets->{quantity}];
            my $unitPrice           = $line->[$offsets->{unit_price}];
            my $netPrice            = $line->[$offsets->{net_price}];

            my $mapping = $productTypeMap->{ lc($productType) };
            if ($mapping)
            {
                $productType = $mapping->{productType};
                $format = $mapping->{formatType};
                $mediaType = $mapping->{mediaType};
            } else {
				die "Unknown product type";
			}

            # Probably wise to allow for blank lines...
            #
            if ($units || $artist || $albumTitle || $trackTitle)
            {
                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.
###
