package RPS::Import::Plastichead::v1;

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use parent 'RPS::Import::ImporterMixed';

use Date::Calc qw(Add_Delta_Days Days_in_Month);

sub physical { 1 }

sub _fieldMapExtended {
    {
        physical => {
            option1 => {
                countryCode     => 'E',
                dateBegin       => 'A1',
                productType     => 'A',
                clientProductID => 'A',
                upc             => 'B',
                artistName      => 'C',
                albumName       => 'D',
                retailPrice     => 'H',
                sales           => 'F',
                returns         => 'G',
                totalRevenue    => 'J',
                mediaType       => 'A',
            },
         },
    };
}
sub _unverifiedFieldMapExtended {
    {
        physical => {
            option1 => {
                _DistFee  => 'E'
            }
        }
    }
};


sub _headerIdentifierExtended {
    {
        physical => {
            option1 => {
                upc => 'Barcode',
            },
        },
    };
}

sub channel      { RPS::File::Sale::CHANNEL_RETAIL }
sub serviceID    { Client::Service::DSP_PLASTICHEAD }
sub priceLevel   { RPS::File::Sale::PLEVEL_UNKNOWN }
sub currencyCode { 'GBP' }

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

    $self->SUPER::_preProcess(%args);

    foreach my $line ( @{ $args{lines} } ) {
        $self->{line} = $line;

        my $fee = $line->[4];
        if ( $fee && $fee =~ /^(\d+)% Distro \(Non Export\)$/ ) {
            $self->{_distFee} = $1;
        }
    }
}

sub countryCode {
    my $self = shift;

    my $code = $self->_getByFieldName("countryCode");
    $code = 'EU' if ($code eq 'EXPORT');

    return $code;
}

my %months      = (
    'JANUARY'   => '01',
    'FEBRUARY'  => '02',
    'MARCH'     => '03',
    'APRIL'     => '04',
    'MAY'       => '05',
    'JUNE'      => '06',
    'JULY'      => '07',
    'AUGUST'    => '08',
    'SEPTEMBER' => '09',
    'OCTOBER'   => '10',
    'NOVEMBER'  => '11',
    'DECEMBER'  => '12',
);

sub saleDates {
    my $self = shift;

    my $dt = $self->_getByFieldName('dateBegin');
    my $startDate;
    my $endDate;

    if ( $dt =~ /^(\w+) (\d{4})$/ ) {   # MMM-YYYY
        my $year  = $2;
        my $month = $months{uc($1)};
        $startDate = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        $endDate = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
    }
    return ( $startDate, $endDate );
}

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('productType') || '';

    if ($type =~ /(\w\w)$/) {
        return RPS::File::Sale::TYPE_CD  if ($1 eq 'CD');
        return RPS::File::Sale::TYPE_LP  if ($1 eq 'LP');
        return RPS::File::Sale::TYPE_DVD if ($1 eq 'VD');
    }

    $self->fail("Can't determine product_type from: '$type'.");
}


sub mediaType {
    my $self = shift;

    return $self->productType eq RPS::File::Sale::TYPE_DVD
        ? RPS::DB::Item::MediaType::kMediaTypeVideo
        : RPS::DB::Item::MediaType::kMediaTypeAudio;
}

sub totalRevenue {
    my $self = shift;

    my $revenue = $self->_getByFieldName("totalRevenue") || 0;
    my $country = $self->_getByFieldName("countryCode");

    if (($country eq "UK") && $self->{_distFee}) {
        my $distFee = $self->{_distFee};
        $distFee = ( 100 - $distFee ) * .01;
        $revenue *= $distFee;
    }

    return $revenue;
}

sub salesRevenue   { $_[0]->totalRevenue > 0 ? $_[0]->totalRevenue     : 0 }
sub returnsRevenue { $_[0]->totalRevenue < 0 ? abs $_[0]->totalRevenue : 0 }

sub _isValidSaleRecord {
    return ($_[0]->_getByFieldName('productType') && $_[0]->_getByFieldName('artistName') &&
           ( $_[0]->_getByFieldName('sales') || $_[0]->_getByFieldName('returns') ) );
}

1;
