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

package RPS::Import::Finetunes;

use strict;

use Date::Calc qw(Days_in_Month Add_Delta_Days Decode_Month);
use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::Import::SaleRec;
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 Client::Service;

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

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

my %fieldMap = (
    1 => {
        label          => 19,
        service        => 7,
        distributor    => 0,
        country_code   => 8,
        product_type   => 11,
        product_format => 9,
        artist         => 17,
        upc            => 14,
        title          => 18,
        isrc           => 15,
        units          => 12,
        currency_code  => 31,
        net_price      => 29,
        start_date     => 5,
    },
);

my %gServiceMap = (
    'POPJOY'        => Client::Service::DSP_POPJOY,
    'TIGAVISION'    => Client::Service::DSP_TIGAVISION,
    'DJSHOP'        => Client::Service::DSP_DJSHOP,
    'TONSPION'      => Client::Service::DSP_TONSPION,
    'DOOLOAD'       => Client::Service::DSP_DOOLOAD,
    'SPEX'          => Client::Service::DSP_SPEX,
    'MZEE'          => Client::Service::DSP_MZEE,
    'CLUBLOAD'      => Client::Service::DSP_CLUBLOAD,
    'CLUBLOADFR'    => Client::Service::DSP_CLUBLOADFR,
    'SOUNDQUAKE'    => Client::Service::DSP_SOUNDQUAKE,
    'HARDYTV'       => Client::Service::DSP_HARDYTV,
    'CLUBSMART'     => Client::Service::DSP_CLUBSMART,
    'DEUTSCHLOAD'   => Client::Service::DSP_DEUTSCHLOAD,
    'SCS'           => Client::Service::DSP_SCS,
    'PUNKLOAD'      => Client::Service::DSP_PUNKLOAD,
    'FINETUNES.NET' => Client::Service::DSP_FINETUNES,
    'DANCEALLDAY'   => Client::Service::DSP_PUNKLOAD,
    'BEGGARS'       => Client::Service::DSP_BEGGARS,
);

my %gProductTypeMap = (
    'track' => {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'bundle' => {
        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];

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

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

            my $country      = uc( $line->[ $offsets->{country_code} ] );
            my $currencyCode = uc( $line->[ $offsets->{currency_code} ] );

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

            my $service   = $line->[ $offsets->{service} ];
            my $serviceID = $gServiceMap{ uc($service) };

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

            my $trackTitle;
            my $albumTitle;

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

            my $upc       = $line->[ $offsets->{upc} ];
            my $isrc      = $line->[ $offsets->{isrc} ];
            my $artist    = $line->[ $offsets->{artist} ];
            my $units     = $line->[ $offsets->{units} ];
            my $netPrice  = $line->[ $offsets->{net_price} ];
            my $label     = $line->[ $offsets->{label} ];
            my $startDate = $line->[ $offsets->{start_date} ];

            # Probably wise to allow for blank lines...
            #
            if ( $units && $artist && ( $albumTitle || $trackTitle ) ) {
                unless ($serviceID) {
                    $self->errstr("Unknown service: $service");
                    return undef;
                }

                $startDate =~ s/\./\//g if ($startDate);
                my ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $startDate, type => 'eur' );

                my $unitPrice = $netPrice / $units;

                my $sale = RPS::Import::SaleRec->new();

                $sale->serviceID($serviceID);
                $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->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.
###
