package RPS::Import::CreateSpace;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

use lib '/app/tools/common/lib';
#use Common::Util;
use Common::Util qw(normalize_date normalize_isrc);
use Common::Consts qw(%COUNTRY_CODE);

use lib '/app/tools/data_classes/lib';
use Client::Service;

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

my %field_map = (
    1 =>
    {
        date_begin => 0,
        date_end   => 1,
        country    => 2,
        service_id => 3,
        upc => 4,
        units => 5,
        price => 6,
        retail_price => 8,
        currency => 10,
        artist => 11,
        album => 12,
        label => 13,
    },
);

#public methods

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

    my $lines = $args{lines};
    my $version = $args{file}->VersionNum();
    my $fileName = $args{file}->OrigFileName();
    my $fileObj = $args{file};
    $fileObj->Physical(1);
    my @sheets = @{ $args{sheets} };

    return undef unless ($lines);

    my $linenum = 1;

    my $offsets = $field_map{$version};
    my %errors = ();

    my ($dateBegin, $dateEnd);

    my $type = RPS::File::Sale::TYPE_CD;



    foreach my $line(@$lines)
    {
        my $service_product_id = $line->[$offsets->{service_id}];
        my $artist = $line->[$offsets->{artist}];
        my $album = $line->[$offsets->{album}];
        my $units = $line->[$offsets->{units}];
        my $price = $line->[$offsets->{price}];
        my $retail_price = $line->[$offsets->{retail_price}];
        my $upc = $line->[$offsets->{upc}];
        my $currency = $line->[$offsets->{currency}];
        my $country = $line->[$offsets->{country}];
        my $label = $line->[$offsets->{label}];
        my $f_date_begin = $line->[$offsets->{date_begin}];
        my $f_date_end = $line->[$offsets->{date_end}];

        my $channel = RPS::File::Sale::CHANNEL_RETAIL;
        my $plevel  = RPS::File::Sale::PLEVEL_FULL;

        my $sales_revenue = $price * $units;

        if($service_product_id && $artist && $artist &&
        $album && $units && $price =~ /\d+/ && $upc &&
        $currency && $country && $label && $f_date_begin &&
        $f_date_end)
        {
            ($dateBegin,$dateEnd) =
            $self->_getDates(date_begin=>$f_date_begin,
            date_end=>$f_date_end, type=>'iso');

            unless ($dateBegin && $dateEnd) {
                $self->errstr("couldn't get dates from $f_date_begin, $f_date_end line: $linenum");
                print STDERR "couldn't get dates from $f_date_begin, $f_date_end line: $linenum";
                return undef;
            }

            my $sale = RPS::Import::SaleRec->new();
            $sale->productType($type);
            $sale->mediaType(1);
            $sale->serviceProductID($service_product_id);
            $sale->artistName($artist);
            $sale->albumName($album);
            $sale->sales($units);
            $sale->retailPrice($retail_price);
            $sale->wholesalePrice($price);
            $sale->salesRevenue($sales_revenue);
            $sale->totalRevenue($sales_revenue);
            $sale->upc($upc);
            $sale->currencyCode($currency);
            $sale->countryCode($country);
            $sale->labelName($label);
            $sale->priceLevel($plevel);
            $sale->channel($channel);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->lineNum($linenum);

            #$args{dumper}($sale);  ## for cmd-line testing
            $self->_insertSale(sale => $sale);
        }

        $linenum++;
    }


    return $linenum;
}

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