package RPS::Import::BeatportHistorical;

use strict;

use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Consts;

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

# map version num to the field order
my %fieldMap = (
    2 => {
        catalog_id => 0,
        album      => 1,
        track      => 2,
        isrc       => 3,
        country    => 5,
        unita      => 6,
        unitb      => 7,
        unitc      => 8,
        unitd      => 9,
        unite      => 10,
        unitf      => 11,
        price      => 12,
    },
    3 => {
        catalog_id => 1,
        isrc       => 2,
        label      => 3,
        album      => 4,
        track      => 5,
        artist     => 6,
        country    => 9,
        units      => 15,
        price      => 19,
    },
);

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 $retval   = undef;
    my $lines    = $args{lines};
    my $fileObj  = $args{file};
    my $fileName = $fileObj->OrigFileName;
    my $version  = $fileObj->VersionNum;

    return undef unless ( $lines && $version );

    my $offsets = $fieldMap{$version};
    my $linenum = 1;
    my ( %errors, $units );
    my ( $dateBegin, $dateEnd ) = $self->_getDates($fileName);
    foreach my $line (@$lines) {
        my $prodtype    = RPS::File::Sale::TYPE_TRACK;
        my $format      = RPS::File::Sale::FORMAT_DOWNLOAD;
        my $upc         = Common::Util::normalize_upc( $line->[ $offsets->{upc} ] ) if ( defined $offsets->{upc} );
        my $isrc        = Common::Util::normalize_isrc( $line->[ $offsets->{isrc} ] );
        my $artist      = $line->[ $offsets->{artist} ];
        my $album       = $line->[ $offsets->{album} ];
        my $track       = $line->[ $offsets->{track} ];
        my $label       = $line->[ $offsets->{label} ] if ( defined $offsets->{label} );
        my $catalog_id  = $line->[ $offsets->{catalog_id} ];
        my $country     = $line->[ $offsets->{country} ];
        my $price       = $line->[ $offsets->{price} ];
        my $countryCode = $Common::Consts::COUNTRY_CODE{ lc($country) };
        $units =
          ( $line->[ $offsets->{unita} ] +
              $line->[ $offsets->{unitb} ] +
              $line->[ $offsets->{unitc} ] +
              $line->[ $offsets->{unitd} ] +
              $line->[ $offsets->{unite} ] +
              $line->[ $offsets->{unitf} ] )
          if ( $version == 2 );
        $units = ( $line->[ $offsets->{units} ] + 1 ) if ( $version == 3 );
        $price /= $units if ( $units != 0 );

        if ( $prodtype && $format && $price && ( $album || $track ) && $track !~ /(title|track)/i ) {
            unless ( $dateBegin && $dateEnd ) {
                $self->errstr( "couldn't get dates from " . $fileName );
                return undef;
            }
            unless ( defined $countryCode || exists $errors{$country} ) {
                print STDERR "unknown country: $country\n";
                $self->warning();
                $errors{$country} = 1;
            }

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

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->serviceProductID($catalog_id);
            $sale->upc($upc) if ( defined $offsets->{upc} );
            $sale->isrc($isrc);
            $sale->artistName($artist);
            $sale->albumName($album);
            $sale->trackName($track) if ( $prodtype eq RPS::File::Sale::TYPE_TRACK );
            $sale->labelName($label) if ( defined $offsets->{label} );
            $sale->units($units);
            $sale->price($price);
            $sale->countryCode($countryCode);
            $sale->lineNum($linenum);

            $self->_insertSale( sale => $sale );
        }

        #else { print STDERR "$linenum - pt: $prodtype , ft: $format , p: $price , a: $album , c: $countryCode\n"; }
        $linenum++;
    }

    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my $self = shift;
    my $date = shift;
    my ( $dateBegin, $dateEnd );
    $date =~ m/(\d{4})_Quarter_(\d)/;
    my $year = $1;
    my $qtr  = $2;
    if ( $year ne "" && $qtr ne "" ) {
        if ( $qtr == 1 ) {
            $dateBegin = $year . "-01-01";
            $dateEnd = $year . "-03-" . Days_in_Month( $year, 3 );
        } elsif ( $qtr == 2 ) {
            $dateBegin = $year . "-04-01";
            $dateEnd = $year . "-06-" . Days_in_Month( $year, 6 );
        } elsif ( $qtr == 3 ) {
            $dateBegin = $year . "-07-01";
            $dateEnd = $year . "-09-" . Days_in_Month( $year, 9 );
        } elsif ( $qtr == 4 ) {
            $dateBegin = $year . "-10-01";
            $dateEnd = $year . "-12-" . Days_in_Month( $year, 12 );
        }
        return ( $dateBegin, $dateEnd );
    }
    return undef;
}

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