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

package RPS::Import::Livewire::v1;

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::Import::SaleRec;
use RPS::DB::Item::MediaType;
use RPS::File::Sale;

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

sub _fieldMap {
    {
        service      => 10,
        track_artist => 1,
        track_name   => 2,
        isrc         => 3,
        upc          => 4,
        type         => 5,
        retail_price => 7,
        country      => 9,
        currency     => 9,
        net_price    => 11,
    };
}

sub _productTypeMap {
    {
        'song' => {
            productType => RPS::File::Sale::TYPE_TRACK,
            formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
            mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
    };
}

sub _country {
    my $self = shift;
    my $country = shift || return;

    $country =~ /^(\w{2})/;
    return $1;
}

sub _currency {
    return 'GBP';

    # We don't use the currency in the import file.
    my $self = shift;
    my $currency = shift || return;

    $currency =~ /(\w{3})$/;
    return $1;
}

sub _getDate {
    my $self  = shift;
    my $sheet = shift;

    return ( $self->{_startDate}, $self->{_endDate} ) if ( $self->{_startDate} );

    my %months = qw( January   01
      February  02
      March     03
      April     04
      May       05
      June      06
      July      07
      August    08
      September 09
      October   10
      November  11
      December  12 );

    my ( $startDate, $endDate );

    my $f_date = $sheet->[1]->[1];
    if ( $f_date =~ /(\w+) (\d{1,2}), (\d{4}) to (\w+) (\d{1,2}), (\d{4})/ ) {
        my $startMonth = $1;
        my $startDay   = $2;
        my $startYear  = $3;
        my $endMonth   = $4;
        my $endDay     = $5;
        my $endYear    = $6;

        $startDate = sprintf( "%02d-%02d-%04d", $months{$startMonth}, $startDay, $startYear );
        $endDate   = sprintf( "%02d-%02d-%04d", $months{$endMonth},   $endDay,   $endYear );

        $self->{_startDate} = $startDate;
        $self->{_endDate}   = $endDate;
    }

    return ( $startDate, $endDate );
}

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