#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::Import::BelieveDigital::v1;

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tool/common/lib';
use Common::Country;

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

sub _fieldMap {
    {
        labelName     => 'D',
        serviceID     => 'B',
        countryCode   => 'C',
        dateBegin     => 'A',
        dateEnd       => 'A',
        formatType    => 'I',
        artistName    => 'E',
        upc           => 'G',
        albumName     => 'F',
        isrc          => 'G',
        trackName     => 'F',
        units         => 'K',
        price         => 'N',
	}
}

sub _headerIdentifier { ( labelName => 'Label' ) }

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

sub currencyCode { 'EUR' }

sub countryCode {
	my $self = shift;
	my $country = $self->_getByFieldName( 'countryCode' );
	
	# Someone must have been hungry when they created this sales file...
	if ($country eq 'CHILI')
	{
	    $country = 'CHILE';
	}
	elsif ($country eq 'COTE D IVOIRE')
	{
	    $country = 'CI';
	}	
	elsif ($country eq 'BOSNIA AND HERZEGOVI')
	{
	    $country = 'BOSNIA AND HERZEGOVINA';
	}	
	elsif ($country eq 'IRAN, ISLAMIC REPUBL')
	{
	    $country = 'IRAN, ISLAMIC REPUBLIC OF';
	}		
	elsif ($country eq 'TIMORLESTE')
	{
	    $country = 'TIMOR-LESTE';
	}		
	elsif ($country eq 'LAO PEOPLE S DEMOCRATIC REPUBLIC')
	{
	    $country = 'LAO PEOPLE\'S DEMOCRATIC REPUBLIC';
	}	
	
	my $c = Common::Country->Get( $country );
	
    $self->fail( "Unable to determin country code from '$country'" ) unless( $c && $c->alpha2 );

    return $c->alpha2;
}

sub productType {
	my $self = shift;
	my $value = $self->_getByFieldName( 'upc' );

	# If all digits assume it's a upc == album
	if( $value && $value =~ /^\d+$/ ) {
		return RPS::File::Sale::TYPE_ALBUM;

	# Otherwise assume it's an ISRC == track
	} else {
		return RPS::File::Sale::TYPE_TRACK;
	}
}

sub formatType {
	my $self = shift;
	my $value = $self->_getByFieldName( 'formatType' );
	
	if( $value eq 'FULL' ) {
		return RPS::File::Sale::FORMAT_DOWNLOAD;
	} elsif ( $value eq 'STREAM' ) {
		return RPS::File::Sale::FORMAT_STREAM;
	}
}

sub upc {
	my $self = shift;
	
	if ($self->productType() eq RPS::File::Sale::TYPE_ALBUM)
	{
	    return $self->_getByFieldName( 'upc' );   
	}    
	
	return undef;
}

sub isrc {
	my $self = shift;
	
	if ($self->productType() eq RPS::File::Sale::TYPE_TRACK)
	{
	    my $isrc = $self->_getByFieldName( 'isrc' );
	    # Removing dashes from ISRC
	    $isrc =~ s/-//g;    
	    return $isrc;   
	}    
	
	return undef;
}

sub albumName {
	my $self = shift;
	
	if ($self->productType() eq RPS::File::Sale::TYPE_ALBUM)
	{
	    return $self->_getByFieldName( 'albumName' );   
	}    
	
	return undef;
}

sub trackName {
	my $self = shift;
	
	if ($self->productType() eq RPS::File::Sale::TYPE_TRACK)
	{
	    return $self->_getByFieldName( 'trackName' );   
	}    
	
	return undef;
}

sub price {
	my $self = shift;
	
	my $price = $self->_getByFieldName( 'price' );
	
	# The price is formatted euro-style.
	
	$price =~ s/,/\./;
	
	return $price;
}

sub units {
	my $self = shift;
	
	my $units = $self->_getByFieldName( 'units' );
	
	# The units are formatted euro-style.
	
	$units =~ s/\./,/;
	
	return $units;
}

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

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

    $self->fail( "Unknown service '$service'" ) unless( $serviceID );

    return $serviceID;
}

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