package RPS::Import::INGrooves::v12;

use strict;

use lib '/app/tools/common/lib';
use Common::Date;
use Common::Country;

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

use Data::Dumper;

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

sub _fieldMap {
    {
        serviceID        => 'B', # RETAILER
        labelName        => 'M',
        countryCode      => 'K',
        dateBegin        => 'A',
        formatType       => 'O', # ASSET TYPE
        mediaType        => 'N', # SALES TYPE
        artistName       => 'C',
        serviceProductID => 'J',
        upc              => 'E',
        albumName        => 'D',
        trackName        => 'F',
        isrc             => 'H',
        units            => 'P',
        price            => 'S',
    }
}

sub currencyCode { 'USD' }

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

	if ($name) {
	
	    $name =~ s/,.*//;
	
	    $name = $self->_getAlternateCountryName($name) || $name;
	
	    my $country = new Common::Country( search => $name );
	
		if ($country) {
			return $country->alpha2();
		} else {
			return $name;
		}
	}
}

sub _getMappings {
    my $self = shift;
    my $retailer    = $self->_serviceName();
    #my $productType = $self->_getByFieldName( 'productType' );
    my $productType = ""; # N/A for v12
    my $assetType   = $self->_getByFieldName( 'formatType' );
    my $salesType   = $self->_getByFieldName( 'mediaType' );

    return $self->_getServiceFormat( $retailer, $salesType, $assetType, $productType );
}

sub productType {
    my $self = shift;
    #my $_productType = $self->_getByFieldName( 'productType' ); # N/A for v12

    my ( $serviceID, $formatType, $productType, $mediaType ) = $self->_getMappings();

    return $productType if( $productType );
}

sub formatType           {
    my $self = shift;
    my $assetType   = $self->_getByFieldName( 'formatType' );

    my ( $serviceID, $formatType, $productType, $mediaType ) = $self->_getMappings();

    if ($formatType)
    {
        return $formatType;
    }
    
    return $assetType;
}

sub mediaType           {
    my $self = shift;
    my $salesType   = $self->_getByFieldName( 'mediaType' );

    my ( $serviceID, $formatType, $productType, $mediaType ) = $self->_getMappings();

    return $mediaType if( $mediaType );
    
    return $salesType;
}

sub _serviceName {
    my $self = shift;
    my $service = $self->_getByFieldName( 'serviceID' ) || return;
    $service     =~ s/ (\w{3})$//;

    return $service;
}

sub serviceID           {
    my $self = shift;
    my $service = $self->_serviceName();

    my ( $serviceID, $formatType, $productType, $mediaType ) = $self->_getMappings();

    return $serviceID if( $serviceID );

    return $self->handleError("Unknown service: $service", RPS::DB::Item::SaleImportError::kErrorService);
}

sub price {
    my $price = abs( shift->SUPER::price() );
    return $price;
}

sub units {
    my $self = shift;
    my $units = $self->SUPER::units();
    my $price = $self->SUPER::price();

    if( defined($price) ) {
        return $price < 0 ? abs($units) * -1 : $units;
    } else {
        return $units;
    }

}

sub unitPrice { abs( shift->SUPER::unitPrice ) }


## MapFace Stuff

# If we want to check for mappings that were created by other compatible versions,
# that must be specified here.
sub _mapFaceCompatibleVersions {
	return (12,13,14,16,18,19,20,21);
}

sub _mapFaceServiceID {
	return ('serviceID');
}

sub _mapFaceCountryCode {
	return ('serviceID', 'countryCode');
}

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

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

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

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