package RPS::Import::ColorsStudio::v1;

use strict;
use warnings;

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

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

sub physical { 0 }

sub _fieldMapExtended {
    {
        digital => {
            option1 => {
                serviceID    => 'G',
                countryCode  => 'I',
                dateBegin    => 'A',
                dateEnd      => 'A',
                productType  => 'B',
                formatType   => 'F',
                artistName   => 'C',
                upc          => 'H',
                albumName    => 'E',
                isrc         => 'H',
                trackName    => 'D',
                units        => 'J',
                currencyCode => 'M',
                price        => 'Y',
            },
        },
    };
}

sub _unverifiedFieldMapExtended { {} }

sub _headerIdentifierExtended {
    {
        digital => {
            option1 => {
                productType => 'Type',
            },
        },
    };
}

sub mediaType   { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub serviceID {
    my $self = shift;

    my $service = lc( $self->_getByFieldName('serviceID') || '' );
    my $serviceID = $self->getServiceID($service) || $RPS::Import::ServiceMap::SERVICE_ALIASES{$service};
    return $serviceID if $serviceID;

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

sub isrc {
    my $self = shift;

    my $isrc = $self->_getByFieldName("isrc");

    return $isrc if $self->productType() eq RPS::File::Sale::TYPE_TRACK;
}

sub upc {
    my $self = shift;

    my $upc = $self->_getByFieldName("upc");

    return $upc if $self->productType() eq RPS::File::Sale::TYPE_ALBUM;
}

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('productType') || '';
    return RPS::File::Sale::TYPE_TRACK if $type =~ /^Track$/i;
    return RPS::File::Sale::TYPE_ALBUM if $type =~ /^Release$/i;
}

sub countryCode {
    my $self = shift;

    my $country = $self->_getByFieldName('countryCode') || '';
    return 'ZZ' if $country =~ /^93\/.+$/i;
    return 'VE' if $country =~ /^Venezuela/i;
    return 'KR' if $country =~ /^Korea \(Republic of\)/i;
    return 'UK' if $country =~ /^United Kingdom of/i;

    my $oCountry = Common::Country->Get($country) if $country;
    return $oCountry->alpha2 if $oCountry;

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

sub formatType {
    my $self = shift;

    my $type = $self->_getByFieldName('formatType') || '';
    return RPS::File::Sale::FORMAT_DOWNLOAD if $type =~ /^Download$/i;
    return RPS::File::Sale::FORMAT_STREAM   if $type =~ /^Streaming$/i;

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

sub price     { sprintf "%.8f", abs( shift->_getByFieldName('price') || 0 ) }
sub unitPrice { abs shift->SUPER::unitPrice }
sub units {
    my $self = shift;

    my $units = $self->_getByFieldName('units') || 0;
    $units = sprintf("%.0f", $units);

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

sub _isValidSaleRecord {
    my $self = shift;

    return $self->albumName && ($self->units || $self->price);
}

1;
