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

package RPS::Import::JBHifiMusic::v2;

use strict;

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

use lib '/app/tools/rps/lib';
use base 'RPS::Import::Importer';

sub _fieldMap {
    {
        countryCode      => 'B',
        dateBegin        => 'C',
        dateEnd          => 'D',
        productType      => 'F',
        formatType       => 'E',
        artistName       => 'K',
        upc              => 'J',
        albumName        => 'L',
        isrc             => 'G',
        trackName        => 'I',
        units            => 'M',
        price            => 'P',
    }
}

sub _headerIdentifier { ( trackName => 'Track Title' ) }

sub currencyCode { 'USD'; }
sub mediaType { RPS::DB::Item::MediaType::kMediaTypeAudio; }

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

    return normalize_upc($upc);
}

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

    return normalize_isrc($isrc);
}

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

sub saleDates {
    my $self = shift;

    my $dateBegin;
    my $dateEnd;

    my $startDate = $self->_getDateBegin();
    my $endDate   = $self->_getDateEnd();

    if( $startDate && $endDate )
    {
        ($dateBegin,$dateEnd) = $self->_getDates( date_begin => $startDate, date_end => $endDate, type => 'eur' );
    }

    return ($dateBegin, $dateEnd);
}

sub formatType  {
    my $self = shift;
    my $fmt = $self->_getByFieldName( 'formatType' );
    if( $fmt =~ /^ps$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    $self->fail("Unable to determine format from '$fmt'");
}

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