package RPS::Import::INGrooves::v17;

use strict;

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

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

use Data::Dumper;

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

sub _fieldMap {
    {
        serviceID        => 'D',
        countryCode      => 'U',
        dateBegin        => 'B',
        dateEnd          => 'C',
        artistName       => 'E',
        serviceProductID => 'K',
        upc              => 'G',
        albumName        => 'F',
        trackName        => 'H',
        isrc             => 'I',
        units            => 'R',
        price            => 'T',
    }
}

sub _unverifiedFieldMap {
    {
        assetType        => 'P',
        salesType        => 'O',
    }
}

sub currencyCode { 'USD' }

sub productType {
    my $self = shift;
    my $assetType = $self->_getByFieldName( 'assetType' );
    if( $assetType =~ /^(album|album upgrade)$/i )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    }
    elsif( $assetType =~ /^(ringback|ringtone|track|track upgrade|video)$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    }
    return $assetType;
}

sub formatType {
    my $self = shift;
    my $salesType = $self->_getByFieldName( 'salesType' );
    if( $salesType =~ /^(album|track) downloads$/i  ||
        $salesType =~ /^video(s)?$/i ||
        $salesType =~ /^NULL$/i ) # Note: the Importer class will force to stream if the price is low enough
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif( $salesType =~ /^ringtones$/i )
    {
        return RPS::File::Sale::FORMAT_RINGTONE;
    }
    elsif( $salesType =~ /^streams$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif( $salesType =~ /^tethered download$/i )
    {
        return RPS::File::Sale::FORMAT_TETHERED;
    }

    return $salesType;
}

sub mediaType {
    my $self = shift;
    my $assetType = $self->_getByFieldName( 'assetType' );
    if( $assetType =~ /^(album|album upgrade)$/i ||
        $assetType =~ /^(ringback|ringtone|track|track upgrade|video)$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    elsif( $assetType =~ /^video$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }
    
    return $assetType;
}

sub serviceID {
    my $self = shift;
    my $service = $self->_getByFieldName( 'serviceID' ) || return;
    my $serviceID = $self->getServiceID( $service ) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };
    
    return $serviceID if( $serviceID );
    
    return $self->handleError("Unknown service: $service", RPS::DB::Item::SaleImportError::kErrorService);
}

sub countryCode {
    my $self = shift;
    my $name = $self->_getByFieldName('countryCode');
    my $serviceID = $self->serviceID();
  
   	if ($name) {
	
	    # Note: any fields in a CSV sales file with a comma surrounded by spaces will
	    # have the spaces stripped out when the file is read (see _read_text_file in
	    # RPS::Sale::File).  Non-CSV files don't have this behavior, so we repeat the
	    # "comma code" here and the keys in the countryMap table reflect this.
	    #
	    $name =~ s/\s*,\s*/,/g;  # Force non-CSV files to not have whitespace around the comma(s)
	
	    my %countryMap = (
	        'spain,spanish state' => 'spain',
	        'argentina,argentine republic' => 'argentina',
	        'australia,commonwealth of' => 'australia',
	        'austria,republic of' => 'austria',
	        'belgium,kingdom of' => 'belgium',
	        'brazil,federative republic of' => 'brazil',
	        'chile,republic of' => 'chile',
	        'colombia,republic of' => 'colombia',
	        'costa rica,republic of' => 'costa rica',
	        'denmark,kingdom of' => 'denmark',
	        'egypt,arab republic of' => 'egypt',
	        'finland,republic of' => 'finland',
	        'france,french republic' => 'france',
	        'ghana,republic of' => 'ghana',
	        'greece,hellenic republic' => 'greece',
	        'hong kong,special administrative region of china' => 'hong kong',
	        'hungary,hungarian people\'s republic' => 'hungary',
	        'india,republic of' => 'india',
	        'israel,state of' => 'israel',
	        'italy,italian republic' => 'italy',
	        'kenya,republic of' => 'kenya',
	        'korea,republic of' => 'korea, republic of', # valid, but _needs_ the space to be recognized by Country
	        'mexico,united mexican states' => 'mexico',
	        'nigeria,federal republic of' => 'nigeria',
	        'norway,kingdom of' => 'norway',
	        'peru,republic of' => 'peru',
	        'philippines,republic of the' => 'philippines',
	        'poland,polish people\'s republic' => 'poland',
	        'portugal,portuguese republic' => 'portugal',
	        'singapore,republic of' => 'singapore',
	        'south africa,republic of' => 'south africa',
	        'spain,spanish state' => 'spain',
	        'sweden,kingdom of' => 'sweden',
	        'switzerland,swiss confederation' => 'switzerland',
	        'taiwan,province of china' => 'taiwan, province of china', # valid, but _needs_ the space to be recognized by Country
	        'trinidad and tobago,republic of' => 'trinidad and tobago',
	        'uganda,republic of' => 'uganda',
	    );
	    $name = "US" if( $serviceID == Client::Service::DSP_YOUTUBE && $name =~ /^territory unspecified$/i ); # FB2109
	
	    $name = $countryMap{lc $name} if( exists $countryMap{lc $name} );
	
	    my $country = new Common::Country( search => $name );
		if ($country) {
			return $country->alpha2();
		}
			
		return $name;
	}
}

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

sub _mapFaceServiceID {
	return ('serviceID');
}

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

sub _mapFaceProductType {
	return ('assetType');
}

sub _mapFaceFormatType {
	return ('salesType');
}

sub _mapFaceMediaType {
	return ('assetType');
}

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