package RPS::Import::Skype;

use strict;


use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_upc normalize_isrc);

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

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

my %field_map = (
    1 => {
        date            => 0,
        svc_prod_id     => 1,
        track           => 2,
        retail          => 6,
        currency        => 7,
        client_prod_id  => 9,
        isrc            => 10,
        artist          => 13,
        units           => 14,
    },
    2 => {
        date            => 0,
        svc_prod_id     => 1,
        track           => 2,
        retail          => 6,
        currency        => 7,
        client_prod_id  => 9,
        isrc            => 10,
        artist          => 14,
        units           => 16,
    },
    3 => {
        date            => 0,
        svc_prod_id     => 1,
        track           => 2,
        retail          => 6,
        currency        => 7,
        client_prod_id  => 9,
        isrc            => 10,
        artist          => 13,
        units           => 15,
    },
);
# version 4 is the same as 2 with 1 of the unused columns named differently
$field_map{4} = \%{ $field_map{2} };

# public methods

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

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

    my $sheetNum = _getUSsheet($args{sheet_names});
    unless ($sheetNum)
    {
        $self->errstr("can't locate tab");
        return undef;
    }
    my $lines = $args{sheets}->[$sheetNum];
    return 0 unless ($lines);

    my $linenum = 1;
    my $prodtype = RPS::File::Sale::TYPE_TRACK;
    my $format = RPS::File::Sale::FORMAT_RINGTONE; # was FORMAT_MASTERTONE (FB1324)
    my $mediaType = RPS::DB::Item::MediaType::kMediaTypeAudio;

    my $priceType = '';

    if ($self->{clientID} == RPS::Import::Importer::WMG) # warner
    {
        my $fmt = 'mastertone';
        # check for warner specific stuff
        require RPS::Import::WMG::Util;

        my ($billTo, $sellTo, $priceType) = RPS::Import::WMG::Util::getWirelessInfo($self->{fileServiceID}, $fmt);
        unless ($billTo && $sellTo)
        {
            $self->warning();
            print STDERR "unknown billto/sellto mapping: $self->{fileServiceID}\n";
        }

        unless ($priceType)
        {
            $self->warning();
            print STDERR "unknown format mapping: $fmt\n";
        }

        $fileObj->Atomic(1);
        $fileObj->Save();
    }

    foreach my $line(@$lines)
    {
        next unless ($line->[$offsets->{date}] =~ /^\d+/);
        my ($dateBegin, $dateEnd) = _getDates($line->[$offsets->{date}]);
        unless ($dateBegin && $dateEnd)
        {
            $self->errstr("couldn't get dates");
            print STDERR "fileName: $line->[$offsets->{date}]\n";
            return undef;
        }

        my $svc_prod_id    = $line->[$offsets->{svc_prod_id}];
        my $client_prod_id = $line->[$offsets->{client_prod_id}];
        my $isrc           = normalize_isrc($line->[$offsets->{isrc}]);
        my $artist         = $line->[$offsets->{artist}];
        my $track          = $line->[$offsets->{track}];
        my ($units)        = $line->[$offsets->{units}] =~ /^(\d+)$/;
        my $retail         = $line->[$offsets->{retail}];
        #my $currency       = $line->[$offsets->{currency}];

        if ($units && $retail && $artist && $track) {
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->mediaType($mediaType);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->serviceProductID($svc_prod_id);
            $sale->clientProductID($client_prod_id);
            $sale->isrc($isrc);
            $sale->artistName($artist);
            $sale->trackName($track);
            $sale->units($units);
            $sale->retail($retail);
            #$sale->currencyCode($currency);
            $sale->lineNum($linenum);
            $sale->priceType($priceType);

            #$args{dumper}($sale);  ## for cmd-line testing
            $self->_insertSale(sale => $sale);
        }# else { print STDERR "skip: u: $units r: $retail a: $artist t: $track ($linenum)\n"; }
        $linenum++;
    }
    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my $date = shift;

    $date =~ s/\s.*//;
    my ($year, $month, $day) = split /\W+/, $date;
    return unless ($year =~ /^\d+$/ && $month =~ /^\d+$/);

    return (
        sprintf("%d-%02d-%02d", $year, $month, $day),
        sprintf("%d-%02d-%02d", $year, $month, $day)
    );

    #return (
        #sprintf("%d-%02d-01", $year, $month),
        #sprintf("%d-%02d-%d", $year, $month, Days_in_Month($year, $month))
    #);
}

sub _getUSsheet
{
    my $sheets = shift;
    my $i = 0;

    map {
        return $i if (lc($sheets->[$i]) eq 'us');
        $i++;
    } @$sheets;

    return undef;
}

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