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

package RPS::Import::BelieveDigital::v10;

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 base 'RPS::Import::Importer';

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

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

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

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

    $self->fail("Unknown country: $_country");
}

sub saleDates {
    my $self = shift;

    my $dateBegin;
    my $dateEnd;

    my $startDate = $self->_getDateBegin();
    my $endDate   = $self->_getDateEnd();

    if( $startDate =~ /(\w{3}) (\d{4})/ )
    {
        my %months = (Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6,
                      Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12);
        my($month, $year) = $startDate =~ /(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w* (\d{4})/i;
        $month = $months{$month};

        $startDate = sprintf("%04d-%02d-01", $year, $month);
        $endDate = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month));
    }

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

    $self->fail("Trackname not set, can't determine product type");
    return undef;
}

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

        if( $value =~ /full/i || $value =~ /download/i ) {
                return RPS::File::Sale::FORMAT_DOWNLOAD;
        } elsif ( $value =~ /stream/i ) {
                return RPS::File::Sale::FORMAT_STREAM;
        }
    $self->fail("Unknown format: $value");
}

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

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

    return $price;
}

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

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

    }

    if ($price != 0 && $units == 0)
    {
      $self->fail( "Revenue $price with no units" )
    }

    return $units;
}

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