package RPS::Import::PocketGroup2;

use strict;


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

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Consts;
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;

# map version num to the field order
my %fieldMap =
(
	2 =>
	{
		'artist'   => 1,
		'title'    => 2,
		'date'     => 3,
		'units'    => 8,
		'price'    => 14,
		'isrc'     => 15,
	},
);

my %gSheetMap = 
(
    "True Tones" =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_RINGTONE,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    "Ringback Tones" =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_RINGBACK,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
#    "Wallpapers" =>
#    {
#        productType => RPS::File::Sale::TYPE_ALBUM,
#        formatType => RPS::File::Sale::FORMAT_GRAPHIC,
#    },
    "Videos" =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeVideo,
    },
    "Full Tracks" =>
    {
        productType => RPS::File::Sale::TYPE_TRACK,
        formatType => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType => RPS::DB::Item::MediaType::kMediaTypeAudio,
    }
);

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

#public methods

sub _importLines
{
	my $self = shift;
	my %args = @_;

    my $sheets = $args{sheets};
    my $sheet_names = $args{sheet_names};

	my $fileObj = $args{file};
	my $version = $fileObj->VersionNum;
    ##my $version = $args{version}; # for cmd-line testing

    return 0 unless ($sheets);

	my $offsets = $fieldMap{$version};

    for (my $sheet_i = 0; $sheet_i < @$sheets; $sheet_i++) {
        my $sheet = $sheets->[$sheet_i];
        my $sheet_name = $sheet_names->[$sheet_i];

        my $linenum = 1;
        Common::Log::Print("SHEET: $sheet_name");
        my $formatMap = $gSheetMap{$sheet_name};
        if (! $formatMap)
        {
            Common::Log::Print(" skipping this sheet");
            next;
        }

        my $header_found = 0;
        foreach my $line (@$sheet) {
            if ($header_found == 0) {
                $header_found = 1 if ($line->[$offsets->{artist}] eq "Artist");
                $linenum++;
                Common::Log::Print("found header on line $linenum");
                next;
            }
    
            my $artist   = $line->[$offsets->{artist}];
            my $isrc     = $line->[$offsets->{isrc}];
            my $title    = $line->[$offsets->{title}];
            my $currency = "GBP";
            my $country  = "GB";
    
            unless ($artist || $isrc || $title) {
                Common::Log::Print("skipping line $linenum, had artist $artist isrc $isrc title $title");
                $linenum++;
                next;
            }
    
            # JPK - Keeping the overloaded getDates for now.
            # I would like to use the 'new' method in the Importer class, but
            # there are some changes I'd like to see (namely the ability to pass
            # in a _list_ of date formats to try, rather than just 1 or none).
            #
	        my ($dateBegin, $dateEnd) = $self->_getDates($line->[$offsets->{date}]);
    
            my $prodtype = $formatMap->{productType};
            my $format = $formatMap->{formatType};
            my $mediaType = $formatMap->{mediaType};

    
            my ($units)	= $line->[$offsets->{units}] =~ m/^(-?\d+)$/;
            my ($price)	= $line->[$offsets->{price}] =~ m/^(\d*\.?\d*)$/;
    
            $price = ($units != 0)?($price/$units):0;
    
            if ($units && $price && ($title || $artist || $isrc))
            {
                my $sale = RPS::Import::SaleRec->new();
    
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->isrc($isrc);
                $sale->artistName($artist);
                $sale->albumName($title) if ($prodtype eq RPS::File::Sale::TYPE_ALBUM);
                $sale->trackName($title) if ($prodtype eq RPS::File::Sale::TYPE_TRACK);
                $sale->formatType($format);
                $sale->productType($prodtype);
                $sale->mediaType($mediaType);
                $sale->units($units);
                $sale->price($price);
                $sale->countryCode($country);
                $sale->currencyCode($currency);
                $sale->lineNum($linenum);
    
                ##$args{dumper}($sale); ## for cmd-line testing
                $self->_insertSale(sale => $sale);
            } 
            $linenum++;
        }
    }

	return 1;
}

#
# Private methods
#


sub _getDates
{
	my $self = shift;
	my $date = shift;  # end date of period

    my ($dateBegin, $dateEnd);
    if ($date =~ m/^(\d{4})-(\d\d)-\d\d$/) {
        my $year  = $1;
        my $month = $2;
        $dateBegin = sprintf("%04d-%02d-%02d", $year, $month, 1);
        $dateEnd   = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month));
        return ($dateBegin, $dateEnd);
    } elsif ($date =~ m/^(\w{3})-(\w{3})(\d\d)$/) {
        my $m1 = $1;
        my $m2 = $2;
        my $yr = $3 + 2000;
        $dateBegin = sprintf("%04d-%02d-%02d", $yr, Decode_Month($m1), 1);
        $dateEnd   = sprintf("%04d-%02d-%02d", $yr, Decode_Month($m2), Days_in_Month($yr, Decode_Month($m2)));
        return ($dateBegin, $dateEnd);
    } elsif ($date =~ m/^(\w{3})-(\d\d)$/) {
        my $m1 = $1;
        my $yr = $2 + 2000;
        $dateBegin = sprintf("%04d-%02d-%02d", $yr, Decode_Month($m1), 1);
        $dateEnd   = sprintf("%04d-%02d-%02d", $yr, Decode_Month($m1), Days_in_Month($yr, Decode_Month($m1)));
        return ($dateBegin, $dateEnd);
    }

    return undef;
}

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