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

package RPS::Import::iMusica::v1;

use strict;

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

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

sub _headerIdentifier { ( trackName => 'Track' ) }

sub _fieldMap {
    {
        labelName     => 'O',
        dateBegin     => 'E',
        formatType    => 'C',
        artistName    => 'K',
		upc           => 'N',
        albumName     => 'M',
        isrc          => 'L',
        trackName     => 'I',
        units         => 'R',
		currencyCode  => 'W',
        price         => 'X',
		mediaType     => 'C'
	}
}

sub _unverifiedFieldMap {
	{
		unitPrice     => 'V',
	}
}

sub countryCode { 'BR' }
sub productType { RPS::File::Sale::TYPE_TRACK }

sub _isValidSaleRecord {
    my $self = shift
	;
	return unless( $self->labelName );
	
	return $self->SUPER::_isValidSaleRecord();
}

sub formatType {
	my $self = shift || die;
	my $formatType = $self->_getByFieldName( 'formatType' ) || return;
	
	if( $formatType =~ /udio/i ) {
	    return RPS::File::Sale::FORMAT_STREAM;
	} else {
		$self->fail( "Unable to determine format type from '$formatType'" );
	}
}

sub mediaType { 
	my $self = shift || die;
	my $type = $self->_getByFieldName( 'mediaType' ) || return;
	
	if( $type =~ /udio/i ) {
	    return RPS::DB::Item::MediaType::kMediaTypeAudio;
	} else {
		$self->fail( "Unable to determine media type from '$type'" );
	}
}

sub units {
	my $self = shift;
	my $units = $self->_getByFieldName( 'units' ) || return;
	
	# It looks like out Excel parser is confused returning the format instead of
	# the value.  If it does then then let's derive the units.
	if( lc($units) eq 'general' ) {
		my $unitPrice = $self->_getByFieldName( 'unitPrice' );
		$self->fail( "Unknown unit price" ) unless( $unitPrice );
		
		return $self->price / $unitPrice;
	}
	
	return $units;
}


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