package RPS::Import::Mobivillage;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);
use Spreadsheet::ParseExcel;

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

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

my %field_map = (
    1 => {
        date                => 0,
        service_product_id   => 2,
        title               => 3,
        artist              => 4,
        label               => 8,
        isrc                => 9,
        country             => 11,
        units               => 13,
        price               => 17,
    },
    2 => {
        date                => 0,
        service_product_id   => 2,
        title               => 3,
        artist              => 4,
        label               => 8,
        isrc                => 9,
        country             => 11,
        units               => 13,
        price               => 18,
    },
    3 => {
        date                => 0,
        title               => 2,
        artist              => 3,
        label               => 7,
        isrc                => 8,
        country             => 10,
        units               => 12,
        price               => 16,
    },
    4 => {
        date                => 0,
        service_product_id   => 4,
        title               => 5,
        artist              => 6,
        label               => 8,
        isrc                => 9,
        country             => 11,
        units               => 14,
        price               => 18,
    },
);

my %service_map = (

);

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

#public methods

sub _importLines
{
	my $self = shift;
	my %args = @_;
    my $fileObj = $args{file};
    my $version = $args{file}->VersionNum();
    my $fileName = $args{file}->OrigFileName();
    my $offsets = $field_map{$version};
    my $sheet_count = 0;
    my $currency = "EUR";
    my $linenum;
    return undef unless (defined $args{sheets} && defined $version);

    my $sheet_names = $args{sheet_names};

    foreach my $sheet(@{ $args{sheets} }) {
        if($sheet_names->[$sheet_count] !~ /summary/i) {
            $linenum = 1;
	        foreach my $line (@$sheet) {

                my $isrc                = $line->[$offsets->{isrc}];
                my $title               = $line->[$offsets->{title}];
                my $artist              = $line->[$offsets->{artist}];
                my $price               = $line->[$offsets->{price}];
                my $units               = $line->[$offsets->{units}];
                my $label               = $line->[$offsets->{label}];
                my $service_product_id   = $line->[$offsets->{service_product_id}]if (defined $offsets->{service_product_id});
                my $product_type           = RPS::File::Sale::TYPE_TRACK;
                my $format                 = RPS::File::Sale::FORMAT_RINGTONE;
                $price                  /= $units if($units>1);

			    if ($units && $price && $title && $units ne "Downloads" && $line->[$offsets->{country}] ne "Country") {
                    my $country;
                    $country     = "FR" if($line->[$offsets->{country}] =~ /france/i);
                    $country     = "FR" if($line->[$offsets->{country}] eq "FR");
                    $country     = "BE" if($line->[$offsets->{country}] =~ /belgique/i);
                    $country     = "BE" if($line->[$offsets->{country}] eq "BE");
                    $country     = "FR" if($line->[$offsets->{country}] =~ /caraibes/i);
                    $country     = "RE" if($line->[$offsets->{country}] =~ /union/i);
                    unless($country) {
                        print STDERR "Unknown country: " . $line->[$offsets->{country}]."\n";
                        $self->errstr("Unknown country: ".$line->[$offsets->{country}]);
                        return undef;
                    }

                    my ($dateBegin,$dateEnd) = _getDates($line->[$offsets->{date}]);
                    unless ($dateBegin && $dateEnd) {
                        print STDERR "Couldn't get dates from ".$line->[$offsets->{date}]."\n";
                        $self->errstr("couldn't get dates from ".$line->[$offsets->{date}]);
                        return undef;
                    }

			        my $sale = RPS::Import::SaleRec->new();

				    $sale->productType($product_type);
				    $sale->formatType($format);
				    $sale->dateBegin($dateBegin);
				    $sale->dateEnd($dateEnd);
                    $sale->currencyCode($currency);
                    $sale->countryCode($country);
                    $sale->serviceProductID($service_product_id) if(defined $offsets->{service_product_id});
				    $sale->artistName($artist);
				    $sale->trackName($title);
                    $sale->isrc($isrc);
				    $sale->units($units);
				    $sale->price($price);
                    $sale->labelName($label);
				    $sale->lineNum($linenum);
				    $self->_insertSale(sale => $sale);
			    }
                #else { print STDERR "skip s:".$sheet_names->[$sheet_count]." l:".$linenum." u: $units p: $price a: $artist t: $title\n"; }
                $linenum++;
            }
		}
        $sheet_count++;
	}

	return $linenum;
}

#
# Private methods
#

sub _getDates {
    my $passed_date = shift;
    my ($month,$monthName,$year,$qtr,$dateBegin,$dateEnd);

    if($passed_date =~ m/(\d+)\D(\d{4})/) {
        $year = $2;
        $month = $1;
    }
    elsif($passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/) {
        $year = $3;
        $month = $1;
    }
    elsif($passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/) {
        $year = $1;
        $month = $2;
    }
    if($month ne "" & $year ne "") {
        $dateBegin = $year."-".$month."-01";
        $dateEnd = $year."-".$month."-".sprintf("%02d",Days_in_Month($year,$month));
        return ($dateBegin,$dateEnd);
    }
    else {
        return undef;
    }
}

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