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

package RPS::Import::BelieveDigital::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        => 'F',
        serviceID        => 'C',
        countryCode      => 'D',
        dateBegin        => 'B',
        dateEnd          => 'B',
        formatType       => 'M',
        artistName       => 'G',
        serviceProductID => 'L',
        upc              => 'J',
        albumName        => 'H',
        isrc             => 'K',
        trackName        => 'I',
        units            => 'N',

        #price         => 'W',
    };
}

sub _unverifiedFieldMap {
    { revenue => 'S', };
}

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

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

sub currencyCode { 'EUR' }

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

    if ( $country eq 'chili' ) {
        $country = 'chile';
    } elsif ( $country eq 'cote d ivoire' ) {
        $country = 'ci';
    } elsif ( $country eq 'bosnia and herzefovi' ) {
        $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';
    } elsif ( $country eq 'world' && lc( $self->_getByFieldName('serviceID') ) eq 'youtube' ) {
        $country = 'us';
    }

    my $c = Common::Country->Get($country);

    $self->fail("Unable to determine country code from '$country'") unless ( $c && $c->alpha2 );

    return $c->alpha2;
}

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

    # If there's a track name, it's a track.
    if ($track) {
        return RPS::File::Sale::TYPE_TRACK;

        # Otherwise it's an album.
    } else {
        return RPS::File::Sale::TYPE_ALBUM;
    }
}

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 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;
}

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

    if ( $price < 0 ) {
        $price *= -1;
    }

    return $price;
}

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

    if ( $price < 0 && $units > 0 ) {
        $units *= -1;

    }

    return $units;
}

# For some reason we have negative price and a positive units.  We need to accept
# this value, so we disable the validation.
sub _validatePrice {
    my ( $self, $saleRec ) = @_;
    my $price = $saleRec->price || return;

    # 0 | 0.0 | .00 |0.
    if ( $price !~ /^-?(?:(?:\d+)?\.\d+|\d+(?:\.)?(?:\d+)?)$/ ) {
        die Import::ValidationError->new( $saleRec, "Price $price is in an incorrect format" );
    }
}

sub unitPrice {
    my $self      = shift;
    my $netPrice  = $self->_formatPrice( $self->price() );
    my $unitPrice = $netPrice / $self->units() if ( $self->units() );

    if ( $unitPrice < 0 ) {
        $unitPrice *= -1;
    }

    return $unitPrice;
}

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