package RPS::Import::Cargo::v4;

use strict;
use warnings;

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

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

sub physical { 2 }

sub _fieldMapExtended {
    {
        digital => {
            option1 => {
                serviceID   => 'J',
                countryCode => 'I',
                productType => 'D',
                formatType  => 'C',
                upc         => 'A',
                albumName   => 'F',
                isrc        => 'B',
                trackName   => 'H',
                units       => 'K',
                price       => 'L',
            },
         },
    };
}

sub _unverifiedFieldMapExtended {
    {
        digital => {
            option1 => {
                _albumArtist => 'E',
                _trackArtist => 'G',
            },
        }
    };
}

sub _headerIdentifierExtended {
    {
        digital => {
            option1 => {
                upc => 'BARCODE',
            },
        },
    };
}


sub currencyCode { 'EUR' }
sub mediaType    { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub serviceID {
    my $self = shift;

    my $service = lc( $self->_getByFieldName('serviceID') || '' );

    if ( $service =~ /^google play music/i ) {
        return Client::Service::DSP_GOOGLE;
    } elsif ( $service =~ /^itunes streaming/i ) {
        return Client::Service::DSP_ITUNES;
    } elsif ( $service =~ /^juke/i ) {
        return Client::Service::DSP_JUKE;
    } elsif ( $service =~ /^youtube official music/i ) {
        return Client::Service::DSP_YOUTUBE;
    }

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

    $self->fail("Unknown service: $service");
}

sub countryCode {
    my $self = shift;

    my $name = $self->_getByFieldName('countryCode') || '';
    my $oCountry = Common::Country->Get($name);
    return $oCountry ? $oCountry->alpha2 : undef ;
}

sub saleDates {
    my $self = shift;

    my $filename = $self->filename;
    if ( $filename =~ /Cargo Digital ([a-z]{3,9} \d{4}).xlsx?$/i ) {
        return $self->_getDates( date_begin => $1 );
    }

    return;
}

sub productType {
    my $self = shift;

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

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

sub formatType {
    my $self = shift;

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

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

sub artistName {
    my $self = shift;

    my $trackArtist = $self->_getByFieldName('_trackArtist') || '';
    my $albumArtist = $self->_getByFieldName('_albumArtist') || '';

    return $self->productType eq RPS::File::Sale::TYPE_TRACK
        ? $trackArtist
        : $albumArtist;
}

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

    my $units = abs( $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;
}


1;