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

package RPS::Import::WMG::WarnerUSDigital;

use strict;

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

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

use base 'RPS::Import::Importer';

sub _fieldMap {
    {
        # detail
        serviceID   => 'N',
        countryCode => 'G',
        dateBegin   => 'O',
        dateEnd     => 'O',
        productType => 'H',
        formatType  => 'J',
        artistName  => 'F',
        upc         => 'B',
        albumName   => 'E',
        isrc        => 'B',
        trackName   => 'Q',
        units       => 'K',
        price       => 'P',
    };
}

sub _headerIdentifier { ( artistName => 'Artist Name' ) }

sub currencyCode { 'USD' }

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

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

sub trackName {
    my $self = shift;
    my $trackName;

    if ( $self->productType eq RPS::File::Sale::TYPE_TRACK ) {
        $trackName = $self->_getByFieldName('trackName');
        if ( !$trackName ) {
            $trackName = $self->_getByFieldName('albumName');
        }
    }
    return $trackName;
}

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

sub serviceID {
    my $self    = shift;
    my $service = $self->_getByFieldName('serviceID');
    $service =~ s/ \(Download\)//;
    $service =~ s/ \(Streaming\)//;
    my $serviceID = $self->getServiceID( lc($service) ) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };
    return $serviceID if ($serviceID);
    $self->fail("Unknown service '$service'");
}

sub countryCode {
    my $self = shift;

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

    my %countryMap = (
        "argentina satellite"           => "Argentina",
        "central europe"                => "Germany",
        "columbia"                      => "Colombia",
        "greece wm"                     => "Greece",
        "denmark wea"                   => "Denmark",
        "eastern europe"                => "Poland",
        "mton kft (hungary)"            => "Hungary",
        "new zealand wm"                => "New Zealand",
        "n. ireland"                    => "United Kingdom",
        "supraphon (czech rep)"         => "Czech Republic",
        "serengeti records (ghana)"     => "Ghana",
        "serengeti records (mauritius)" => "Mauritius",
        "wm taiwan"                     => "Taiwan",
        "ivory coast"                   => "Côte d'Ivoire",
        "farol musica (portugal)"       => "Portugal",
    );

    $countryName = $countryMap{ lc $countryName } || $countryName;

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

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

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

}

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

    # Follow the sign of the revenue.
    if ( $price > 0 ) {
        $units = abs($units);
    } elsif ( $price < 0 ) {
        $units = abs($units) * -1;
    }

    return $units;
}

sub productType {
    my $self        = shift;
    my $productType = lc( $self->_getByFieldName('productType') ) || return;
    my $formatType  = lc( $self->_getByFieldName('formatType') ) || return;

    my ( $_productType, $_formatType, $_mediaType ) = $self->_getMappings();

    $self->fail("Could not determine product type from $productType / $formatType") if ( !$_productType );

    return $_productType;
}

sub formatType {
    my $self = shift;

    my $productType = lc( $self->_getByFieldName('productType') ) || return;
    my $formatType  = lc( $self->_getByFieldName('formatType') )  || return;

    my ( $_productType, $_formatType, $_mediaType ) = $self->_getMappings();

    $self->fail("Could not determine format type from $productType / $formatType") if ( !$_formatType );

    return $_formatType;
}

sub mediaType {
    my $self = shift;

    my $productType = lc( $self->_getByFieldName('productType') ) || return;
    my $formatType  = lc( $self->_getByFieldName('formatType') )  || return;

    my ( $_productType, $_formatType, $_mediaType ) = $self->_getMappings();

    $self->fail("Could not determine media type from $productType / $formatType") if ( !$_mediaType );

    return $_mediaType;
}

my %gProductFormatMap = (
    'cloud sales' => {
        'stream' => {
            product_type => RPS::File::Sale::TYPE_TRACK,
            format       => RPS::File::Sale::FORMAT_STREAM,
            media        => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
    },
    'digital sales' => {
        'digital album' => {
            product_type => RPS::File::Sale::TYPE_ALBUM,
            format       => RPS::File::Sale::FORMAT_DOWNLOAD,
            media        => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
        'digital download' => {
            product_type => RPS::File::Sale::TYPE_TRACK,
            format       => RPS::File::Sale::FORMAT_DOWNLOAD,
            media        => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
        'stream' => {
            product_type => RPS::File::Sale::TYPE_TRACK,
            format       => RPS::File::Sale::FORMAT_STREAM,
            media        => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
        'video stream' => {
            product_type => RPS::File::Sale::TYPE_TRACK,
            format       => RPS::File::Sale::FORMAT_STREAM,
            media        => RPS::DB::Item::MediaType::kMediaTypeVideo,
        },
    },
    'promotional' => {
        'stream' => {
            product_type => RPS::File::Sale::TYPE_TRACK,
            format       => RPS::File::Sale::FORMAT_STREAM,
            media        => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
        'video stream' => {
            product_type => RPS::File::Sale::TYPE_TRACK,
            format       => RPS::File::Sale::FORMAT_STREAM,
            media        => RPS::DB::Item::MediaType::kMediaTypeVideo,
        },
    },
    'tethered' => {
        'digital download' => {
            product_type => RPS::File::Sale::TYPE_ALBUM,
            format       => RPS::File::Sale::FORMAT_TETHERED,
            media        => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
    },
);

sub _getMappings {
    my $self        = shift;
    my $productType = lc( $self->_getByFieldName('productType') ) || return;
    my $formatType  = lc( $self->_getByFieldName('formatType') ) || return;

    my $_productType;
    my $_formatType;
    my $_mediaType;

    foreach my $_pType ( keys %gProductFormatMap ) {
        if ( $productType =~ /^$_pType$/i ) {
            foreach my $_fType ( keys %{ $gProductFormatMap{$_pType} } ) {
                if ( $formatType =~ /^$_fType$/i ) {
                    $_productType = $gProductFormatMap{$_pType}{$_fType}{product_type};
                    $_formatType  = $gProductFormatMap{$_pType}{$_fType}{format};
                    $_mediaType   = $gProductFormatMap{$_pType}{$_fType}{media};
                    last;
                }
            }
        }
    }
    return ( $_productType, $_formatType, $_mediaType );
}

sub _processSheet {
    my $self     = shift;
    my $sheet    = shift;
    my $sheetnum = shift;

    return $self->SUPER::_processSheet( $sheet, $sheetnum ) if ( $self->{sheetname} !~ /^pivot/i );
}

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