package RPS::Import::AlexanderStreet;

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 %fieldMap = (
        1 => {
            album       => 1,
            track       => 2,
            label       => 3,
            catalog     => 4,
            units       => 5,
            price       => 6,
        },
);

#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 $offsets = $fieldMap{$version};
    my $sheet_names = $args{sheet_names};
    my $header_found = 0;
    my $linenum = 1;
    my ($dateBegin, $dateEnd) = $self->_getDates($fileName);

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

            foreach my $line (@$sheet) {
                my $free                = 0;
                my $country             = "US";
                my $currency            = "USD";
                my $track               = $line->[$offsets->{track}];
                my $album               = $line->[$offsets->{album}];
                my $format              = RPS::File::Sale::FORMAT_STREAM;
                my $product             = RPS::File::Sale::TYPE_TRACK;
                my $units               = $line->[$offsets->{units}];
                my $price               = $line->[$offsets->{price}];
                my $label               = $line->[$offsets->{label}];
                my $catalog             = $line->[$offsets->{catalog}];
                $price                  /= $units if($units!=0);
                $free                   = 1 if($price==0);

                if ($units && ($track || $album) && $track !~ /Track Title/i) {

                    unless (defined $dateBegin && defined $dateEnd) {
                        print STDERR "Couldn't get dates from: ". $fileName . "\n";
                        $self->errstr("couldn't get dates from: ".$fileName);
                        return undef;
                    }
                    unless ($format ne "") {
                        print STDERR "Couldn't get format for: ". $line->[$offsets->{format}] . "(".$linenum.")\n";
                        $self->errstr("Couldn't get format for: ". $line->[$offsets->{format}] . "(".$linenum.")");
                        return undef;
                    }
                    unless ($product =~ /(A|T)/) {
                        print STDERR "Couldn't get get product for: ". $line->[$offsets->{product}] . "(".$linenum.")\n";
                        $self->errstr("Couldn't get get product for: ". $line->[$offsets->{product}] . "(".$linenum.")");
                        return undef;
                    }

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

                    $sale->mediaType(2) if($format =~ /video/i);
                    $sale->productType($product);
                    $sale->serviceProductID($catalog);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->labelName($label);
                    $sale->trackName($track);
                    $sale->albumName($album);
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->free($free);
                    $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:".$track." f:".$format."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
	return $linenum;
}

#
# Private methods
#


sub _getDates
{
    my ($smonth,$syear,$sday,$emonth,$eyear,$eday);
	my $self = shift;
	my $passed_date = shift;
    #print STDERR "|->".$passed_date."<-|\n";
    if($passed_date =~ m/_(\d{4})-(\d+)-(\d+)_to_(\d{4})-(\d+)-(\d+)/) {
        $syear = $1;
        $smonth = $2;
        $sday = $3;
        $eyear = $4;
        $emonth = $5;
        $eday = $6;
    }
    if($syear && $eyear && $smonth && $emonth && $sday && $eday) {
        return ($syear."-".$smonth."-".$sday,$eyear."-".$emonth."-".$eday);
    }
    return undef;
}

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