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

package RPS::Import::Spotify;

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/common/lib';
use Common::Locale;

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 => {
            #header
            start_date      => 1,
            end_date        => 2,

            #detail
            country_code    => 0,
            label           => 1,
            service_id      => 3,
            upc             => 4,
            isrc            => 5,
            track_title     => 6,
            artist          => 7,
            album_title     => 9,
            units           => 10,
            price           => 11,
        },
        2 => {
            #header
            start_date      => 1,
            end_date        => 2,
            label           => 5,

            #detail
            country_code    => 0,
            service_id      => 2,
            upc             => 3,
            isrc            => 5,
            track_title     => 6,
            artist          => 7,
            album_title     => 9,
            units           => 10,
        },
);


#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;
    my $format= RPS::File::Sale::FORMAT_STREAM;
    my $mediaType = RPS::DB::Item::MediaType::kMediaTypeAudio;

    my $startDate;
    my $endDate;

	my $defaultCurrencyCode = Common::Client::Current()->Locale()->currencyFormat()->currencyCode();
	my $defaultLabel;


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

        my $foundHeader1 = 0;
        my $foundHeader2 = 0;

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

            if (! $foundHeader1)
            {
                if ('Start date' eq $line->[$offsets->{start_date}])
                {
					$foundHeader1 = 1;
                }
                next;
            }

            if (! $foundHeader2)
            {
                if ('Country' eq $line->[$offsets->{country_code}])
                {
                    $foundHeader2 = 1;
                }
                else
                {
                   $startDate    = $line->[$offsets->{start_date}];
                   $endDate      = $line->[$offsets->{end_date}];
	               $defaultLabel = $line->[$offsets->{label}] || "" if( $version == 2 );
                }
                next;
            }

			my $country             = uc($line->[$offsets->{country_code}]);
            my $currencyCode        = $defaultCurrencyCode;
            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 $label               = $defaultLabel || $line->[$offsets->{label}];
            my $f_price             = $line->[$offsets->{price}] if( $offsets->{price} );
			my $price               = $f_price / $units if( $f_price && $units );


			# Probably wise to allow for blank lines...
            #
            if ($units && $artist && ($albumTitle || $trackTitle))
            {
				my $type = $version == 2 ? 'eur' : undef;
				
				my ($dateBegin, $dateEnd);
				
				eval { ($dateBegin, $dateEnd) = $self->_getDates(date_begin => $startDate, date_end => $endDate, type => $type ) };
				
				# It seems with version 2 date formats aren't consistent.
				if( $@ ) {
					($dateBegin, $dateEnd) = $self->_getDates(date_begin => $startDate, date_end => $endDate );
				}

                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($price);
                $sale->countryCode($country);
                $sale->currencyCode($currencyCode);
                $sale->lineNum($linenum);

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

	return $linenum;
}



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