package RPS::Import::RedDigital::v15;

use strict;

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

use base 'RPS::Import::MapFaceBase';
use base 'RPS::Import::RedDigital2';

sub productType {
    my $self = shift;
    
    my $productType = $self->{_productType};
    
    if (!$productType) 
    {
        my $config  = $self->_getByFieldName('productType'); # CONFIGURATION
        my $channel = $self->_getByFieldName('formatType');  # DISTRIBUTION CHANNEL
        return $self->handleError("Unknown productType($config) formatType($channel))", RPS::DB::Item::SaleImportError::kErrorProductType);
    }          
    
    return $productType;
}

sub formatType {
    my $self = shift;
    
    my $formatType = $self->{_formatType};

    if (!$formatType) 
    {
        my $config  = $self->_getByFieldName('productType'); # CONFIGURATION
        my $channel = $self->_getByFieldName('formatType');  # DISTRIBUTION CHANNEL
        return $self->handleError("Unknown productType($config) formatType($channel))", RPS::DB::Item::SaleImportError::kErrorFormatType);
    }   
    
    return $formatType;
}

sub mediaType {
    my $self = shift;
    
    my $mediaType = $self->{_mediaType};

    if (!$mediaType) 
    {
        my $config  = $self->_getByFieldName('productType'); # CONFIGURATION
        my $channel = $self->_getByFieldName('formatType');  # DISTRIBUTION CHANNEL
        return $self->handleError("Unknown productType($config) formatType($channel))", RPS::DB::Item::SaleImportError::kErrorMediaType);
    }   
    
    return $mediaType;
}


sub _fieldMap {
    my $self = shift;

    my %fieldMap =
    (
        15 =>
        {
            serviceID => 'A',
            dateBegin        => 'M',
            dateEnd          => 'M',
            labelName        => 'B',
            formatType       => 'N', # DISTRIBUTION CHANNEL
            artistName       => 'C',
            isrc             => 'H',
            trackName        => 'G',
            albumName        => 'D',
            units            => 'O',
            price            => 'P',
            productType      => 'E', # CONFIGURATION
            serviceProductID => 'I',
            upc              => 'K',
            countryCode      => 'L',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap =
    (
        15 =>
        {          
            trackArtist => 'F',
        },
    );

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

sub countryCode {

    my $self = shift;
    
    my $countryCode = $self->SUPER::countryCode();

    return $countryCode;
}

sub serviceID
{
    my $self = shift;

    my $serviceID;

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

    if (!$serviceID)
    {   
        return $self->handleError("Unknown service: $serviceName", RPS::DB::Item::SaleImportError::kErrorService);
    } 
    else
    {
        return $serviceID;   
    }
}


## MapFace Stuff

sub _mapFaceServiceID {
    return ('serviceID');
}

sub _mapFaceCountryCode {
    return ('countryCode');
}

sub _mapFaceProductType {
    return ('productType', 'formatType', 'serviceID');
}

sub _mapFaceFormatType {
    return ('productType', 'formatType', 'serviceID');
}

sub _mapFaceMediaType {
    return ('productType', 'formatType', 'serviceID');
}

###
1;# Play nicely.
###
