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

package RPS::Import::IndieMobile;

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 RPS::Import::Importer;
use base 'RPS::Import::Importer';

my %fieldMap = (
        1 => {
            date            => 3,
            label           => 4,
            service         => 5,
            country         => 6,
            type            => 10,
            artist          => 8,
            track_name      => 9,
            isrc            => 7,
            unit_price      => 12,
            units           => 11,
            net_price       => 16,
        },
);

my %gProductTypeMap = (
    'realtone' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_RINGTONE,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'truetone' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_RINGTONE,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'full album download' =>
    {
        productType => RPS::File::Sale::TYPE_ALBUM,
        formatType => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'full track download' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'dig pre-order' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'sms pre-order' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'ringback tone' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_RINGBACK,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'streaming audio' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_STREAM,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'streaming audio subscription' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_STREAM,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'video download' =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeVideo,
    },
    'video ringtone' =>
    {
        # !!! really?
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_RINGTONE, # was FORMAT_VIDEORINGER (FB1324)
        mediaType => RPS::DB::Item::MediaType::kMediaTypeVideo,
    },

);

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

    # Data is on the sheet named 'AllData'
    #
    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 ('Date of sales' eq $line->[$offsets->{date}])
                {
                    Common::Log::Print("found header on line $linenum");
                    $foundHeader = 1;
                }
                next;
            }
            

            my $country = $Common::Consts::COUNTRY_CODE{lc($line->[$offsets->{country}])};

            my $currencyCode = 'GBP';

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

            my $mapping = $gProductTypeMap{ lc($line->[$offsets->{type}]) };
            if ($mapping)
            {
                $productType = $mapping->{productType};
                $format = $mapping->{formatType};
                $mediaType = $mapping->{mediaType};
            }

            my $trackTitle;
            my $albumTitle;

            # No album sales in this file (yet)
            #
            $trackTitle = $line->[$offsets->{track_name}];

            my $upc = $line->[$offsets->{upc}];
            my $isrc = $line->[$offsets->{isrc}];
            my $artist = $line->[$offsets->{artist}];
            my $catalogID = $line->[$offsets->{catalog_id}];

            my $units = $line->[$offsets->{units}];
            my $netPrice = $line->[$offsets->{net_price}];

            my $label = $line->[$offsets->{label}];


            # Probably wise to allow for blank lines...
            #
            if ($units && $artist && $country && ($albumTitle || $trackTitle))
            {
                my ($dateBegin, $dateEnd) = $self->_getDates(date_begin => $line->[$offsets->{date}], type => 'numerical_year_first');

                die "Error on line $linenum - Could not determine product type from '".$line->[$offsets->{type}]."'" unless $productType;

                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($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.
###
