package RPS::Import::DMXmin;

use strict;


use Date::Calc qw(Days_in_Month);

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

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

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

# detail records
use constant FIELD_LABEL  => 1;
use constant FIELD_TRACK  => 2;
use constant FIELD_ARTIST => 3;
use constant FIELD_PRICE  => 4;

#
# Private methods
#

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

    my $lines = $args{lines};
    my $fileObj = $args{file};
    my $fileName = $args{file}->OrigFileName();
    #my $fileName = $args{OrigFileName}; # cmd-line testing
    #my $version = $args{version}; ## for cmd-line testing
    my $last_label;
    
    return undef unless ($lines);
    my ($dateBegin, $dateEnd) = _getDates($fileName);

    unless ($dateBegin && $dateEnd) {
        $self->errstr("couldn't get dates from $fileName");
        #return undef;
    }

    my $lineNum = 1;

    my ($label, $artist, $track);

    foreach my $line(@$lines) {
        $label  = $line->[FIELD_LABEL];
        $label  = $last_label if ($line->[FIELD_LABEL] ne "" && $last_label ne "");
        $artist = $line->[FIELD_ARTIST];
        $track  = $line->[FIELD_TRACK];
        my ($price) = $line->[FIELD_PRICE] =~ /^(\d+[\.]?\d*)/;
        my $units = $price/6;
        $price = 6;

        if ($price && $artist && $track && $units) {
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType(RPS::File::Sale::TYPE_TRACK);
            $sale->formatType(RPS::File::Sale::FORMAT_BACKGROUNDMUSIC);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->labelName($label);
            $sale->artistName($artist);
            $sale->trackName($track);
            $sale->units($units);
            $sale->price($price);
            $sale->lineNum($lineNum);
            $sale->countryCode('US');

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

    return $lineNum;
}

sub _getDates {         
    my $date = shift;   
                        
    return undef unless ($date =~ m/^.*DMX[ _](\d{4})(\w).*$/);
    my ($year, $qtr) = ($1 ,$2);
                 
    my ($dateBegin, $dateEnd);      
                        
    if ($qtr eq "A") {    
        $dateBegin = $year."-01-01";
        $dateEnd   = $year."-03-".Days_in_Month($year, 3);
    } elsif ($qtr eq "B") {
        $dateBegin = $year."-04-01";
        $dateEnd   = $year."-06-".Days_in_Month($year, 6);
    } elsif ($qtr eq "C") {
        $dateBegin = $year."-07-01";
        $dateEnd   = $year."-09-".Days_in_Month($year, 9);
    } elsif ($qtr eq "D") {
        $dateBegin = $year."-10-01";
        $dateEnd   = $year."-12-".Days_in_Month($year, 12);
    } else {            
        return undef;   
    }                   
                 
    return ($dateBegin, $dateEnd);
}            

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