package RPS::Import::AmazonCreateSpace;

#Created from specs in Fog Bugz Case 9654

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 %CODE_TO_COUNTRY);

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

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

my %field_map = (
8 => {
        date               => 0,
        service_product_id => 1,
        upc                => 3,
        album              => 5,
        artist             => 6,
        channel            => 7,
        retail_price       => 8,
        sales              => 10,
        sales_revenue      => 11,
    },

);

#public methods

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

#    my $fileName = $args{file}->OrigFileName();

#    if($fileName =~ /digital/){
#        return _importDigital($self, @_);
#    }
#    elsif($fileName =~ /physical/){
#        return _importPhysical($self, @_);
#    }
#    else{
#        $self->errstr("Could not determine product type from filename: $fileName");
#        print STDERR "Could not determine product type from filename: $fileName\n";
#        return undef;
#    }

    #my %args = @_;

    #return undef unless ($lines);

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

    #my $linenum = 1;

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

#}

my %channel_map =
("Amazon US Retail" => [RPS::File::Sale::CHANNEL_RETAIL, "US", "USD"],
 "Free Order (US)"  => [RPS::File::Sale::CHANNEL_RETAIL, "US", "USD"],);


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};
    my @sheets = @{ $args{sheets} };
    my $sheet_names = $args{sheet_names};
    my $sheet_count = 0;
    my $offsets = $field_map{$version};

    return undef unless ($lines);

    my $linenum = 1;

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

    #my $currency_code = "USD";
    #my $country_code  = "US";
    my $product_type = RPS::File::Sale::TYPE_CD;
    my $media_type   = RPS::DB::Item::MediaType::kMediaTypeAudio;
    #my $channel     = RPS::File::Sale::CHANNEL_RETAIL;
    my $price_level  = RPS::File::Sale::PLEVEL_FULL;


    foreach my $line (@$lines) {
        my $f_date = $line->[$offsets->{date}];
        my $service_product_id = $line->[$offsets->{service_product_id}];
        my $upc = $line->[$offsets->{upc}];
        my $album = $line->[$offsets->{album}];
        my $artist = $line->[$offsets->{artist}];
        my $f_channel = $line->[$offsets->{channel}];
        my $sales = $line->[$offsets->{sales}];
        my $sales_revenue = $line->[$offsets->{sales_revenue}];
        my $retail_price = $line->[$offsets->{retail_price}];

        my $total_revenue = $sales_revenue;

        my ($dateBegin, $dateEnd);

        my $channel;
        my $country_code;
        my $currency_code;
        my $free;

        if($f_date =~ /^\d{4}\D\d{2}\D\d{2}$/){
            ($dateBegin, $dateEnd) =
            $self->_getDates(date_begin=>$f_date, type=>"iso");
        }

        if($channel_map{$f_channel}){
            ($channel, $country_code, $currency_code) =
            @{$channel_map{$f_channel}};
        }

        if($f_channel =~ /free/i){
            $free = 1;
        }

        if($f_date && $f_channel && $service_product_id && $upc &&
        $album && $artist &&
        $sales =~ /\d+/ && $sales_revenue =~ /\d+/){

            unless($dateBegin && $dateEnd){
                $self->errstr("Could not get dates from $f_date at line number: $linenum");

                print STDERR "Could not get dates from $f_date at line number: $linenum\n";
                return undef;
            }

            unless($channel){
                $self->errstr("Could not get channel from $f_channel at line number: $linenum");

                print STDERR "Could not get channel from $f_channel at line number: $linenum\n";
                return undef;
            }

            unless($country_code){
                $self->errstr("Could not get country code from $f_channel at line number: $linenum");

                print STDERR "Could not get country code from $f_channel at line number: $linenum\n";
                return undef;
            }

            unless($currency_code){
                $self->errstr("Could not get currency code from $f_channel at line number: $linenum");

                print STDERR "Could not get currency code from $f_channel at line number: $linenum\n";
                return undef;
            }

            my $sale = RPS::Import::SaleRec->new();
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->serviceProductID($service_product_id);
            $sale->upc($upc);
            $sale->albumName($album);
            $sale->artistName($artist);

            $sale->countryCode($country_code);
            $sale->currencyCode($currency_code);
            $sale->productType($product_type);
            $sale->mediaType($media_type);

            $sale->channel($channel);
            $sale->priceLevel($price_level);
            $sale->retailPrice($retail_price);
            $sale->sales($sales);
            $sale->salesRevenue($sales_revenue);
            $sale->totalRevenue($total_revenue);
            $sale->free($free) if($free);
            $sale->lineNum($linenum);
            $self->_insertSale(sale => $sale);
        }
        #else{print STDERR
        #"linenum: $linenum Svc prod ID: $serviceProductID title: ".
        #"$title sales: $sales total revenue: $totalRevenue\n"}


        $linenum++;
    }

    $fileObj->Physical(1);
    $fileObj->Save();

    return $linenum;
}




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