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

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Util qw(normalize_date normalize_upc);

use lib '/app/tools/rps/lib';

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

sub _fieldMap {
    my $self = shift;
    my %field_map = (
        42 => {
            labelName     => 'W',
            countryCode   => 'R',
            dateBegin     => 'B',
            productType   => 'Q',
            formatType    => 'N',
            artistName    => 'G',
            upc           => 'D',
            isrc          => 'C',
            trackName     => 'E',
            units         => 'H',
            price         => 'K',
            currencyCode  => 'J',
        }
    );
    return $field_map{$self->_version};
}


sub _unverifiedFieldMap {
    my $self = shift;
    my %field_map = (
        42 => {
            adjustments   => 'I',
        }
    );
    return $field_map{$self->_version};
}


sub _headerIdentifier { ( upc => 'UPC' ) }


sub serviceID { Client::Service::DSP_PANDORA; }


sub units {
    my $self = shift;
    my $u = $self->_getByFieldName('units');
    my $a = $self->_getByFieldName('adjustments');
    if( $a && $a != 0 )
    {
        $self->fail("Unexpected adjustments found");
    }
    return $u;
}


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


sub upc {
    my $self = shift;
    my $upc = $self->_getByFieldName('upc');
    return Common::Util::normalize_upc($upc);
}


sub saleDates {
    my $self = shift;
    my $dateBegin = $self->_getByFieldName('dateBegin');
    return $self->_getDates(date_begin => $dateBegin);
}


sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');
    if ($type =~ /^trackrelease$/i)
    {
        return RPS::File::Sale::TYPE_TRACK;
    }
    $self->fail("Unknown product type '$type'");
}


sub formatType {
    my $self = shift;
    my $format = $self->_getByFieldName('formatType');
    if ($format =~ /^OnDemandStream$/i ||
        $format =~ /^LimitedInteractiveStream$/i)
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif ($format =~ /^NonInteractiveStream$/i )
    {
        return RPS::File::Sale::FORMAT_PERFORMANCE;
    }
    else
    {
        $self->fail("Unable to determine format type from '$format'");
    }
}


sub countryCode {
    my $self  = shift;
    my $country = $self->_getByFieldName('countryCode');
    my $obj = Common::Country->Get($country);
    return $obj ? $obj->alpha2 : undef;
}

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