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

use strict;
use warnings;

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

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

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

sub _fieldMap {
    my $self      = shift;
    my %field_map = (
        1 => {
            countryCode => 'R',
            dateBegin   => 'B',
            dateEnd     => 'B',
            productType => 'Q',
            formatType  => 'N',
            artistName  => 'G',
            upc         => 'D',
            isrc        => 'C',

            #albumName     => 'E',
            trackName    => 'E',
            units        => 'H',
            price        => 'K',
            currencyCode => 'J',
        },
        2 => {
            labelName   => 'V',
            countryCode => 'R',
            dateBegin   => 'E2',
            dateEnd     => 'F2',
            productType => 'Q',
            formatType  => 'N',
            artistName  => 'G',
            upc         => 'D',
            isrc        => 'C',

            #albumName     => 'E',
            trackName    => 'E',
            units        => 'H',
            price        => 'K',
            currencyCode => 'J',
        }
    );
    return $field_map{ $self->_version };
}

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

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

sub units {
    my $self = shift;
    my $u    = $self->_getByFieldName('units');
    my $a    = $self->_getByFieldName('adjustments');
    if ( $a && $a != 0 ) {
        $self->fail("Unexpected adjustments found");
    }
    $u =~ s/(\d+).*/$1/g;
    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');
    my $dateEnd   = $self->_getByFieldName('dateEnd');
    return $self->_getDates( date_begin => $dateBegin, date_end => $dateEnd );
}

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
        || $format =~ /^PremiumAccessStream$/i
        || $format =~ /^PremiumIntroStream$/i
        || $format =~ /^BrandedProgramStream$/i
        || $format =~ /^PinnedTrackStream$/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.
###
