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

package RPS::Import::WMG::WarnerNZDigital;

use strict;

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

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

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

sub _fieldMap {
    my $self    = shift;
    my $version = $self->_version;

    my %field_map = (
        6 => {

            # detail
            artistName => 'D',
            units      => 'J',
            price      => 'K',
            serviceID  => 'E',
        },
        8 => {

            # detail
            artistName => 'C',
            units      => 'I',
            price      => 'J',
            serviceID  => 'D',
        },
    );
    return $field_map{$version};
}

sub _unverifiedFieldMap {
    my $self    = shift;
    my $version = $self->_version;

    my %field_map = (
        6 => {
            config => 'F',
            title  => 'I',
            month  => 'B',
            year   => 'A',
        },
        8 => {
            config => 'E',
            title  => 'H',
            month  => 'B',
            year   => 'A',
        },
    );
    return $field_map{$version};
}

sub _headerIdentifier { ( artistName => 'artist name' ) }

sub countryCode { 'NZ' }

sub currencyCode { 'NZD' }

sub serviceID {
    my $self = shift;

    my $service = $self->_getByFieldName('serviceID');
    my $serviceID = $self->getServiceID( lc($service) ) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };
    return $serviceID if ($serviceID);
    $self->fail("Unknown service '$service'");
}

sub productType {
    my $self = shift;
    my $config = $self->_getByFieldName('config') || return;
    if (   $config =~ /^AD Support Stream - Audio Full Len\/Album$/i
        || $config =~ /^Permanent D\/Load - Audio Full Len\/Album$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } elsif ( $config =~ /^Ad Support Stream - Audio Single$/i
        || $config =~ /^Ad Support Stream - Video Single$/i
        || $config =~ /^Elect. Temp Usage - Audio Single$/i
        || $config =~ /^Elect. Temp Usage - Video Single$/i
        || $config =~ /^Permanent D\/Load - Audio Single$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    }
    $self->fail("Unable to get product type from '$config'");
}

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

    if (   $config =~ /^AD Support Stream - Audio Full Len\/Album$/i
        || $config =~ /^Ad Support Stream - Audio Single$/i
        || $config =~ /^Ad Support Stream - Video Single$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $config =~ /^Elect. Temp Usage - Audio Single$/i
        || $config =~ /^Elect. Temp Usage - Video Single$/i ) {
        return RPS::File::Sale::FORMAT_TETHERED;
    } elsif ( $config =~ /^Permanent D\/Load - Audio Full Len\/Album$/i
        || $config =~ /^Permanent D\/Load - Audio Single$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    $self->fail("Unable to get format type from '$config'");
}

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

    if (   $config =~ /^AD Support Stream - Audio Full Len\/Album$/i
        || $config =~ /^Ad Support Stream - Audio Single$/i
        || $config =~ /^Elect. Temp Usage - Audio Single$/i
        || $config =~ /^Permanent D\/Load - Audio Full Len\/Album$/i
        || $config =~ /^Permanent D\/Load - Audio Single$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    } elsif ( $config =~ /^Ad Support Stream - Video Single$/i
        || $config =~ /^Elect. Temp Usage - Video Single$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }
    $self->fail("Unable to get media type from '$config'");
}

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

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

sub saleDates {
    my $self = shift;

    my $month = $self->_getByFieldName('month');
    my $year  = $self->_getByFieldName('year');

    my $dt = sprintf( "%04d-%02d-01", $year, $month );

    return $self->_getDates( date_begin => $dt );
}

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