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

package RPS::Import::BelieveDigital::v11;

use strict;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_isrc);
use Date::Calc qw(Days_in_Month);

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

sub _fieldMap {
    {
        labelName        => 'E',
        serviceID        => 'C',
        countryCode      => 'D',
        dateBegin        => 'B',
        formatType       => 'M',
        mediaType        => 'L',
        artistName       => 'F',
        serviceProductID => 'K',
        upc              => 'I',
        albumName        => 'G',
        isrc             => 'J',
        trackName        => 'H',
        units            => 'O',
        price            => 'V',
        currencyCode     => 'P',
    }
}

sub _unverifiedFieldMap {
    {
        subscriptionType => 'N',
    }
}

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

sub countryCode {
    my $self = shift;
    my $country = lc($self->_getByFieldName('countryCode'));

    my %countryMap = (
        'chili'         => 'chile',
        'congo  the democratic republic of the' => 'congo, the democratic republic of the',
        'cote d ivoire' => 'ci',
        'bosnia and herzefovi' => 'bosnia and herzegovina',
        'bosnia and herzegovi' => 'bosnia and herzegovina',
        'iran, islamic republ' => 'iran, islamic republic of',
        'iran  islamic republ'  => 'ir',
        'korea  republic of'   => 'kr',
        'lao people s democratic republic' => 'lao people\'s democratic republic',
        'macedonia  the former yugoslav republic of'  => 'mk',
        'moldova  republic of' => 'md',
        'palestine' => 'palestinian territory, occupied',
        'palestinian territory  occupied' => 'palestinian territory, occupied',
        "r+\x{00ac}union"      => 'ru',
        'taiwan  province of china'   => 'tw',
        'taiwan,province of china'   => 'tw',
        'tanzania  united republic of'  => 'tz',
        'timorleste'           => 'timor-leste',
        'virgin islands  u.s.' => 'virgin islands, u.s.',
    );

    my $_country = $countryMap{ lc $country } || $country;

    $_country = 'ci' if( $_country =~ /c(.*)te d ivoire$/i );

    if( $_country )
    {
        my $c = Common::Country->Get( $_country );
        return $c->alpha2 if( $c );
    }
    return $_country; # Return original name and let MapFace deal with it
}

sub saleDates {
    my $self = shift;
    my $startDate = $self->_getDateBegin();
    my $endDate   = $self->_getDateEnd();
    return $self->_getDates( date_begin => $startDate, date_end => $endDate );
}

sub isrc {
    my $self = shift;
    my $isrc = $self->_getByFieldName( 'isrc' );
    if ($isrc) {
        $isrc = Common::Util::normalize_isrc($isrc);
    }
    return $isrc;
}

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

    if( $trackName && !$isrc )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    }
    elsif( $trackName && $isrc )
    {
        return RPS::File::Sale::TYPE_TRACK;
    }
    return $self->handleError("Trackname not set, can't determine product type", RPS::DB::Item::SaleImportError::kErrorProductType );
}

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

    if( $value =~ /^download$/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif ( $value =~ /^stream$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    return $self->handleError("Unknown format: $value", RPS::DB::Item::SaleImportError::kErrorFormatType );
}

sub mediaType {
    my $self = shift;
    my $value = $self->_getByFieldName('mediaType');
    if($value =~ /^music (?:compilation|release)$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    elsif($value =~ /^(?:entertainment|music) video$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }
    return $self->handleError("Unknown media type: $value", RPS::DB::Item::SaleImportError::kErrorMediaType );
}

sub free {
    my $self = shift;
    my $price = $self->_getByFieldName('price');
    my $sType = $self->_getByFieldName('subscriptionType');
    return 1 if( $sType =~ /(free|promo)/i && 0 == $price );
}

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

sub units {
    my $self = shift;
    my $price = $self->_getByFieldName( 'price' );
    my $units = $self->_getByFieldName( 'units' );

    if ($price < 0 && $units > 0)
    {
        $units *= -1;
    }
    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 };
    }
    
    if (!$serviceID)
    {
        return $self->handleError("Unknown serviceID $service", RPS::DB::Item::SaleImportError::kErrorService );
    }

    return $serviceID;
}

#####    MapFace    #####
#

sub _mapFaceCompatibleVersions {
    return( 4,5,6,7,8,9,11,12 );
}

sub _mapFaceServiceID {
    return('serviceID');
}

sub _mapFaceCountryCode {
    return('countryCode');
}

sub _mapFaceProductType {
    return('productType');
}

sub _mapFaceFormatType {
    return('formatType');
}

sub _mapFaceMediaType {
    return('mediaType');
}

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