package RPS::Import::Cargo::v3;

use strict;
use warnings;

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        3 => {
            labelName    => 'E',
            countryCode  => 'D',
            dateBegin    => 'A',
            dateEnd      => 'B',
            productType  => 'J',
            formatType   => 'K',
            artistName   => 'F',
            upc          => 'I',
            albumName    => 'G',
            isrc         => 'J',
            trackName    => 'H',
            units        => 'L',
            currencyCode => 'M',
            price        => 'N',
        },
    );

    return $fieldMap{ $self->_version };
}

sub _headerIdentifier { ( upc => 'UPC' ) }

sub saleDates {
    my $self = shift;
    my $startDate = $self->_getDateBegin();
    my $endDate   = $self->_getDateEnd();
    return $self->_getDates( date_begin => $startDate, date_end => $endDate );
}

sub productType {
    my $self = shift;
    my $isrc = $self->_getByFieldName('isrc');
    return $isrc ? RPS::File::Sale::TYPE_TRACK : RPS::File::Sale::TYPE_ALBUM;
}

sub countryCode {
    my $self    = shift;

    my $country = $self->_getByFieldName('countryCode');
    my $oCountry = Common::Country->new( search => $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');
    if ( $type =~ /^download$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif ( $type =~ /^stream$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    else {
        $self->fail("Could not parse fortmat type from '$type'");
    }
}

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

sub units {
    return shift->_getByFieldName('units') || 0;
}

sub isrc {
    my $self = shift;

    my $isrc = $self->_getByFieldName('isrc');
    $isrc =~ s/-//g if $isrc;

    return $isrc;
}


1;