#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2013 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package RPS::Import::Merlin::v38;

use strict;
use warnings;

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

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

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

sub _headerIdentifier { ( upc => 'UPC' ) }
sub mediaType         { RPS::DB::Item::MediaType::kMediaTypeAudio; }
sub serviceID         { Client::Service::DSP_KKBOX_HONG_KING_LTD; }

sub _processHeader {
    my $self = shift;

    unless ( $self->{_currencyCode} ) {
        my $st = $self->_getByFieldName('currencyCode');
        if ( $st =~ /^(\w{3}) Payable$/i ) {
            $self->{_currencyCode} = $1;
        }
    }
}

sub currencyCode { shift->{_currencyCode} }

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');
    if ( $type =~ /^track$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    }
    $self->fail("Could not determine type from $type");
}

sub formatType {
    my $self = shift;
    my $type = $self->_getByFieldName('formatType');
    if (   $type =~ /^Streaming - (Monthly|Daily|Weekly)$/i
        || $type =~ /^Streaming - Monthly\/Weekly\/Daily$/i
        || $type =~ /^Streaming - (?:Family|Student|Sponsor$)$/i
        || $type =~ /^Sponsor$/i
        || $type =~ /^HMV Music - Monthly$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $type =~ /^download$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    $self->fail("Could not determine format from $type");
}

sub price     { abs( shift->_getByFieldName('price') || 0 ) }
sub unitPrice { abs shift->SUPER::unitPrice }
sub units {
    my $self = shift;

    my $units = $self->_getByFieldName('units') || 0;
    my $price = $self->_getByFieldName('price') || 0;

    if ( $price > 0 ) {
        $units = $units ? abs($units) : 1;
    } elsif ( $price < 0 ) {
       $units = $units ? -1 * abs($units) : -1;
    }

    return $units;
}


1;
