package RPS::Import::Belong;

use strict;

use Date::Calc qw(Days_in_Month Add_Delta_Days Decode_Month);

use lib '/app/tools/common/lib';
use Common::Consts qw(%COUNTRY_TO_CODE);

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 %format_type = (
    Truetones   => RPS::File::Sale::FORMAT_RINGTONE,
);

my %product_type = (
    T               => RPS::File::Sale::TYPE_TRACK,
    B               => RPS::File::Sale::TYPE_ALBUM,
);

my %field_map = (
    1 => {
        format      => 0,
        title       => 1,
        price_link  => 3,
        units       => 5,
        link_price  => 0,
        price       => 4,
    },
);

#public methods

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

	my $fileObj = $args{file};
    my $version = $args{file}->VersionNum();
    my $fileName = $args{file}->OrigFileName();
    my $serviceID = $args{file}->ServiceID;
    my $sheet_count = 0;

    return undef unless (defined $args{sheets} && defined $version);

    my $sheet_names = $args{sheet_names};
    my $offsets = $field_map{$version};
    my $header_found = 0;
    my $linenum = 1;
    my %prices;

    my ($dateBegin, $dateEnd);

    foreach my $sheet(@{ $args{sheets} }) {
        my $found_header = 0;
        my $linenum = 1;
        if($sheet_names->[$sheet_count] =~ /summary/i) {
            foreach my $line (@$sheet) {
                if($line->[0] =~ /Date/) {
                    ($dateBegin,$dateEnd) = $self->_getDates($line->[1]);
                }
                if($line->[$offsets->{link_price}] ne "" && $line->[$offsets->{link_price}] !~ /total/i && $line->[$offsets->{link_price}] !~ /description/i) {
                    my $link_price = $line->[$offsets->{link_price}];
                    if($link_price =~ /bluesky/i) {
                        $prices{WEB} = $line->[$offsets->{price}];
                        $prices{WAP} = $line->[$offsets->{price}];
                    }
                    elsif($link_price =~ /prsms/i) {
                        $prices{SMS} = $line->[$offsets->{price}];
                    }
                    elsif($link_price =~ /ivr/i) {
                        $prices{IVR} = $line->[$offsets->{price}];
                    }
                }
                $linenum++;
            }
        }
        elsif($sheet_names->[$sheet_count] =~ /sale/i) {
            foreach my $line (@$sheet) {

                my $title               = $line->[$offsets->{title}];
                my $country             = "AU";
                my $format              = $format_type{$line->[$offsets->{format}]}; 
                my $prodtype            = RPS::File::Sale::TYPE_TRACK;
                my $units               = $line->[$offsets->{units}];
                my $currency            = "AUD";
                my $price_link          = uc $line->[$offsets->{price_link}];
                my $price               = $prices{$price_link};

                if ($units && $price && $title && $title !~ /Item Name/i && $title !~ /Total$/&& $line->[$offsets->{format}]) {

                    unless (defined $dateBegin && defined $dateEnd) {
                        print STDERR "Couldn't get dates from: ". $fileName . "\n";
                        $self->errstr("couldn't get dates from: ".$fileName);
                        return undef;
                    }

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

                    $sale->productType($prodtype);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->trackName($title) if($title ne "");
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

                    $self->_insertSale(sale => $sale);
                } 
                #else { print STDERR "skip sh: ".$sheet_names->[$sheet_count] . ": l: " . $linenum . " u:".$units." p:".$price." a:".$artist." t:".$title." f:".$format." d:".$line->[$field_position{date}]."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
	return $linenum;
}

#
# Private methods
#


sub _getDates
{
    my ($month,$monthName,$year);
	my $self = shift;
	my $passed_date = shift;
    if($passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/) {
        $year = $1;
        $month = $2;
    }
    elsif($passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/) {
        $year = $3;
        $month = $1;
    }
    elsif($passed_date =~ m/2(\d{3})/) {
        $year = "2".$1;
    }
    if($month eq "") {
        $monthName = "jan" if($passed_date =~ /jan/i);
        $monthName = "feb" if($passed_date =~ /feb/i);
        $monthName = "mar" if($passed_date =~ /mar/i);
        $monthName = "apr" if($passed_date =~ /apr/i);
        $monthName = "may" if($passed_date =~ /may/i);
        $monthName = "jun" if($passed_date =~ /jun/i);
        $monthName = "jul" if($passed_date =~ /jul/i);
        $monthName = "aug" if($passed_date =~ /aug/i);
        $monthName = "sep" if($passed_date =~ /sep/i);
        $monthName = "oct" if($passed_date =~ /oct/i);
        $monthName = "nov" if($passed_date =~ /nov/i);
        $monthName = "dec" if($passed_date =~ /dec/i);
        $month = Decode_Month($monthName) if($month eq "" && $monthName ne "");
    }
    #print STDERR "|->".$passed_date."<>".$monthName."<>".$month."<>".$year."<-|\n";
    my $dateBegin = sprintf("%04d-%02d-%02d", $year, $month, 1);
    my $dateEnd   = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month));
    return ($dateBegin, $dateEnd);
    return undef;
}

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