package RPS::Import::MusicToday::v2;

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use RPS::Import::ServiceMap;

use parent 'RPS::Import::ImporterMixed';

sub physical { 2 }

sub _fieldMapExtended {
    {
        mixed => {
            option1 => {
                labelName        => 'A',
                countryCode      => 'B',
                productType      => 'J',
                formatType       => 'J',
                clientProductID  => 'L',
                artistName       => 'K',
                upc              => 'M',
                albumName        => 'N',
                units            => 'Q',
                channel          => 'O',
                retail           => 'P',
                totalRevenue     => 'R',
                price            => 'R',
                mediaType        => 'J',
            },
        },
    };
}

sub _unverifiedFieldMapExtended {
    {
        mixed => {
            option1 => {
                _saleYear  => 'C',
                _saleMonth => 'D',
                _netUnits  => 'Q',
            },
        },
    };
}

sub _headerIdentifierExtended {
    {
        mixed => {
            option1 => {
                upc => 'upc-ean',
            },
        },
    };
}

sub serviceID    { Client::Service::DSP_MUSICTODAY }
sub priceLevel   { RPS::File::Sale::PLEVEL_FULL }
sub currencyCode { 'USD' }

sub countryCode {
    my $self = shift;

    my $name = $self->_getByFieldName('countryCode') || '';
    my $oCountry = Common::Country->Get($name);

    return $oCountry->alpha2() if $oCountry;
}

sub channel {
    my $self = shift;

    my $channel = $self->_getByFieldName('channel') || '';
    return RPS::File::Sale::CHANNEL_RETAIL    if $channel =~ /^retail$/i;
    return RPS::File::Sale::CHANNEL_MILITARY  if $channel =~ /^military$/i;
    return RPS::File::Sale::CHANNEL_CLUB      if $channel =~ /^club$/i;
    return RPS::File::Sale::CHANNEL_MAILORDER if $channel =~ /^mail order$/i;
    return RPS::File::Sale::CHANNEL_DIRECT    if $channel =~ /^direct$/i;

    $self->fail("Unable to determine channel from: '$channel'.");
}

sub saleDates {
    my $self = shift;

    my $saleYear  = $self->_getByFieldName('_saleYear') || '';
    my $saleMonth = $self->_getByFieldName('_saleMonth') || '';

    unless ( $saleYear =~ /^(\d{4})$/ && $saleMonth =~ /^(\d{1,2})$/ ) {
        $self->fail( "Unable to determine date from: '$saleYear' '$saleMonth'." );
    }

    my $dateBegin = sprintf "%04d-%02d-%02d", $saleYear, $saleMonth, 1;
    return $self->_getDates( date_begin => $dateBegin );
}

sub productType {
    my $self = shift;

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

    # physical
    return RPS::File::Sale::TYPE_CD   if $type =~ /^CD(?: Box Set)?$/i;
    return RPS::File::Sale::TYPE_LP   if $type =~ /^LP$/i;
    return RPS::File::Sale::TYPE_DVD  if $type =~ /^DVD$/i;
    return RPS::File::Sale::TYPE_CASS if $type =~ /^Cassette$/i;

    # digital
    return RPS::File::Sale::TYPE_ALBUM if $type =~ /^Digital Download (?:MP3|FLAC) Format$/i;

    $self->fail("Unable to determine product type from '$type'.");
}

sub formatType {
    my $self = shift;

    my $type = $self->_getByFieldName('formatType') || '';
    return RPS::File::Sale::FORMAT_DOWNLOAD if $type =~ /^Digital Download (?:MP3|FLAC) Format$/i;

    $self->fail("Unable to determine format type from '$type'.");
}

sub mediaType {
    my $self = shift;

    my $type = $self->_getByFieldName('mediaType') || '';
    if ( $type =~ /^(?:Cassette|LP|CD(?: Box Set)?|Digital Download (?:MP3|FLAC) Format)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }

    if ( $type =~ /^DVD$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }

    $self->fail("Unable to determine media type from '$type'.");
}

# digital
sub price     { abs( shift->_getByFieldName('price') || 0 ) }
sub unitPrice { abs shift->SUPER::unitPrice }
sub units {
    my $self = shift;

    my $units = $self->_getByFieldName('units') || 0;
    my $price = $self->_getByFieldName('price') || 0;

    if ( $price > 0 ) {
        $units = $units ? abs($units) : 1;
    } elsif ( $price < 0 ) {
       $units = $units ? -1 * abs($units) : -1;
    }

    return $units;
}

# physical
sub netUnits           { $_[0]->_getByFieldName('_netUnits') || 0 }
sub totalRevenue       { $_[0]->_getByFieldName('totalRevenue') || 0 }
sub sales              { $_[0]->netUnits     > 0 ? $_[0]->netUnits         : 0 }
sub salesRevenue       { $_[0]->totalRevenue > 0 ? $_[0]->totalRevenue     : 0 }
sub returns            { $_[0]->netUnits     < 0 ? abs $_[0]->netUnits     : 0 }
sub returnsRevenue     { $_[0]->totalRevenue < 0 ? abs $_[0]->totalRevenue : 0 }

sub _isValidSaleRecord {
    my $self = shift;

    return if $self->labelName =~ /Summary/i;

    return $self->countryCode && $self->labelName && defined $self->price;
}


1;
