package RPS::Import::Beatport;

use strict;

use Date::Calc qw(Days_in_Month);

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

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

# map version num to the field order
my %fieldMap = (
    1 => {
        'upc'      => 0,
        'catno'    => 1,
        'isrc'     => 2,
        'vendorID' => 3,
        'label'    => 4,
        'album'    => 5,
        'track'    => 6,
        'artist'   => 7,
        'country'  => 10,
        'format'   => 13,
        'date'     => 14,
        'type'     => 15,

        #                'units'     => 16,
        'price' => 22,
    },
    4 => {
        'upc'      => 0,
        'catno'    => 1,
        'isrc'     => 2,
        'vendorID' => 3,
        'label'    => 4,
        'album'    => 5,
        'track'    => 6,
        'artist'   => 7,
        'country'  => 10,
        'format'   => 13,
        'date'     => 14,
        'type'     => 15,

        #                'units'     => 16,
        'price'     => 23,
        'sale_type' => 24,
    },
);

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

#public methods

my %product_type_map = (
    'release' => RPS::File::Sale::TYPE_ALBUM,
    'single'  => RPS::File::Sale::TYPE_TRACK
);

my %format_map = ( 'download' => RPS::File::Sale::FORMAT_DOWNLOAD );

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};

    shift @$lines;
    my $linenum = 2;
    my %errors;
    foreach my $line (@$lines) {

        my $units    = 1;
        my $vendorID = $line->[ $offsets->{vendorID} ];
        my $upc      = Common::Util::normalize_upc( $line->[ $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} ];

        my $f_country      = $line->[ $offsets->{country} ];
        my $f_product_type = $line->[ $offsets->{type} ];
        my $f_format       = $line->[ $offsets->{format} ];
        my $f_date         = $line->[ $offsets->{date} ];

        my $countryObj = Common::Country->Get($f_country) if ($f_country);
        my $country    = $countryObj->alpha2()            if ($countryObj);

        my $product_type = $product_type_map{$f_product_type};
        my $format       = $format_map{$f_format};

        my ( $dateBegin, $dateEnd );

        if ( $f_date =~ /\d{4}\D\d{2}\D\d{2}/ ) {
            ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $f_date, type => "iso" );
        } else {
            ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $f_date, type => "us" );
        }

        # Per FB15964, each line is a single unit (this implies that the price is the unit price)
        #
        my $price = $self->numeric( $line->[ $offsets->{price} ] );

        # If the line has a sales type and it's 'void', or if the price is negative, then
        # treat the line as a return.
        #
        if ( $price < 0 || ( defined $offsets->{sale_type} && $line->[ $offsets->{sale_type} ] =~ m/void/i ) ) {
            $price *= -1;
            $units *= -1;
        }

        if (   $f_product_type
            && $f_format
            && $price
            && ( $album || $track )
            && $f_country ) {

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

            unless ( $f_country && '' ne $f_country ) {
                $self->errstr("Missing country on line $linenum");
                return;
            }

            unless ($country) {
                $self->errstr( "Unknown country on line " . $linenum . " code:" . $f_country );
                return;
            }

            unless ($product_type) {
                $self->errstr("Unknown product type: $f_product_type");
                return;
            }

            unless ($format) {
                $self->errstr("Unknown format: $f_format");
                return;
            }

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

            $sale->productType($product_type);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->serviceProductID($vendorID);
            $sale->upc($upc);
            $sale->isrc($isrc);
            $sale->artistName($artist);
            $sale->albumName($album);
            $sale->trackName($track);
            $sale->labelName($label);
            $sale->units($units);
            $sale->price($price);
            $sale->countryCode($country);
            $sale->lineNum($linenum);

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

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

    return $linenum;
}

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