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

package RPS::Import::IRIS::v3;

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     => 'C',
        serviceID     => 'B',
        countryCode   => 'AB',
        dateBegin     => 'W',
        dateEnd       => 'X',
        productType   => 'J',
        formatType    => 'M',
        upc           => 'E',
        albumName     => 'D',
        isrc          => 'H',
        trackName     => 'G',
        units         => 'O',
        price         => 'V',
        free          => 'V',
	}
}

sub _unverifiedFieldMap {
    {
        albumArtist   => 'F',
        trackArtist   => 'I',
    }
}


my %formatTypes = (
    'download'              => RPS::File::Sale::FORMAT_DOWNLOAD,
    'ota'                   => RPS::File::Sale::FORMAT_DOWNLOAD,
    'stream'                => RPS::File::Sale::FORMAT_STREAM,
    'other'                 => RPS::File::Sale::FORMAT_STREAM,
    'tethered_download'     => RPS::File::Sale::FORMAT_TETHERED,
    'play'                  => RPS::File::Sale::FORMAT_STREAM,
    'ringtone'              => RPS::File::Sale::FORMAT_RINGTONE,
    'ringback'              => RPS::File::Sale::FORMAT_RINGTONE,
    'realtone'              => RPS::File::Sale::FORMAT_RINGTONE,
);


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

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

sub currencyCode { 'USD' }

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

	if( $value && $value eq 'ALBUM' ) {
		return RPS::File::Sale::TYPE_ALBUM;

	} elsif ( $value && $value eq 'TRACK' ) {
		return RPS::File::Sale::TYPE_TRACK;
	} else {
        $self->fail( "Unknown product type '$value'" ); 	    
	}
}

sub formatType {
	my $self = shift;
	my $value = $self->_getByFieldName( 'formatType' );
	
    my $formatType = $formatTypes{lc($value)};
    
    $self->fail( "Unknown format type '$value'" ) unless $formatType;
    
    my $price = $self->_getByFieldName( 'price' );
    my $units = $self->_getByFieldName( 'units' );
    my $unitPrice = $price / $units;
    
    if ($formatType eq RPS::File::Sale::FORMAT_RINGTONE && $unitPrice < .25)
    {
        $formatType = RPS::File::Sale::FORMAT_STREAM;
    } elsif (lc($value) eq 'ota' && $unitPrice < .10 && $self->productType() eq RPS::File::Sale::TYPE_TRACK) {
        $formatType = RPS::File::Sale::FORMAT_STREAM;
    } elsif (lc($value) eq 'ota' && $unitPrice < 1 && $self->productType() eq RPS::File::Sale::TYPE_ALBUM) {
        $formatType = RPS::File::Sale::FORMAT_STREAM;
    }       
    
    return $formatType; 
}


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

    if ( $service )
    {
        my $serviceClean = $service;
        $serviceClean =~ s/ \(\w{2}\)//;
        $serviceID = $self->getServiceID( $serviceClean ) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $serviceClean };
    }

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

    return $serviceID;
}


sub artistName {
    my $self = shift;
    my $trackArtist = $self->_getByFieldName('trackArtist');
    my $albumArtist = $self->_getByFieldName('albumArtist');

    return $self->productType eq RPS::File::Sale::TYPE_TRACK ? $trackArtist : $albumArtist;
}


sub free {
	my $self = shift;

	if ( $self->price == 0 ) {
		return 1;
	} else {
		return 0;
    }
}


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