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

package RPS::Import::WMG::WarnerUKDigital;

use strict;

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

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

sub _fieldMap {
    {
        dateBegin        => 'A',  # stm_recdate

        artistName       => 'AC', # con_name
        serviceProductID => 'C',  # stm_catno

        albumName        => 'AD', # cat_title
        trackName        => 'AD', # cat_title
        labelName        => 'AG', # com_name

        productType      => 'AB', # pak_desc
        formatType       => 'AE', # pri_desc

        units            => 'H',  # sales
        price            => 'M',  # stm_royalty
        countryCode      => 'AA', # ter_location
    }
}

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

sub currencyCode { 'GBP'; }

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

sub saleDates {
    my $self = shift;

    my $dateBegin;
    my $dateEnd;

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

    if( $startDate )
    {
        ($dateBegin,$dateEnd) = $self->_getDates( date_begin => $startDate );
    }

    return ($dateBegin, $dateEnd);
}

sub countryCode {
    my $self = shift;

    my $countryCode = $self->_getByFieldName( 'countryCode' );

    my %countryMap = (
        "central europe"             => "Germany",
        "eastern europe"             => "Poland",
        "napster permanent (uk)"     => "UK",
        "play.com (uk)"              => "UK",
        "apple icloud"               => "UK",
        "denmark wea"                => "Denmark",
        "napster streaming (uk)"     => "UK",
        "qriocity music sony"        => "France",
        "asia warner (beijing) ent." => "China",
        "new zealand wm"             => "New Zealand",
        "wm taiwan"                  => "Taiwan",
        "7 digital (uk)"             => "UK",
        "amazon digital uk"          => "UK",
        "nokia comes with music"     => "UK",
        "you tube"                   => "UK",
        "spotify ad supported"       => "SE",
        "spotify premium"            => "SE",
        "we7"                        => "UK",
        "virgin music dx3"           => "UK",
        "vodaphone bundles"          => "UK",
        "bskyb"                      => "UK",
        "dx3 (samsung)"              => "UK",
        "omnifone"                   => "UK",
        "hmv (7 digital)"            => "UK",
        "k2 media"                   => "UK",
        "playnow-arena.com"          => "UK",
        "hip digital"                => "UK",
        "vodaphone ayce"             => "UK",
    );

    my $countryName = $countryMap{lc $countryCode};

    # If we don't have a direct mapping for the country, try and
    # make an intelligent guess.
    #
    $countryName = "UK"     if( !$countryName && ($countryCode =~ /(?:\s|^)uk(?:\s|$)/i || $countryCode =~ /\(uk\)/i) );
    $countryName = $1       if( !$countryName && ($countryCode =~ /wm (\w+)$/i) );
    $countryName = "Mexico" if( !$countryName && ($countryCode =~ /(?:\s|^)mexico(?:\s|$)/i) );
    $countryName = "Argentina" if( !$countryName && ($countryCode =~ /(?:\s|^)argentina(?:\s|$)/i) );

    # Fall-back; maybe the country code is really a country instead
    # of a service name with country information in it.
    #
    $countryName = $countryCode if( !$countryName );

    my $o = Common::Country->Get( $countryName );

    if( $o )
    {
        return $o->alpha2();
    }

    $self->fail("Unable to determine country from '$countryName'");

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

    if( $pak_desc =~ /Internet Album - Electronic/i )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    }
    elsif( $pak_desc =~ /Internet Single - Electronic/i ||
           $pak_desc =~ /Ringtune\/Realtone\/Mastertone\/Cart- Phone Content/i  ||
           $pak_desc =~ /Ringback - Phone Content/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    }

    $self->fail("Unable to determine product from pak_desc($pak_desc)");
}

sub formatType  {
    my $self = shift;
    my $pri_desc = $self->_getByFieldName( 'formatType' );
    my $pak_desc = $self->_getByFieldName( 'productType' );

    if( $pri_desc =~ /^Download Full Price$/i )
    {
        if( $pak_desc =~ /^Ringback - Phone Content$/i )
        {
            return RPS::File::Sale::FORMAT_RINGTONE;
        }
        else
        {
            return RPS::File::Sale::FORMAT_DOWNLOAD;
        }
    }
    elsif( $pri_desc =~ /^Stream Full Price$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    $self->fail("Unable to determine format from pri_desc($pri_desc) pak_desc($pak_desc)");
}

sub albumName {
    my $self = shift;
    my $name = $self->_getByFieldName( 'albumName' );
    return $name if( $self->productType eq RPS::File::Sale::TYPE_ALBUM );
}

sub trackName {
    my $self = shift;
    my $name = $self->_getByFieldName( 'trackName' );
    return $name if( $self->productType eq RPS::File::Sale::TYPE_TRACK );
}

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