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

package RPS::Import::eClassical::v1;

use strict;

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

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

sub _fieldMap {
    {
        labelName        => 'E',
        dateBegin        => 'C',
        productType      => 'K',
        formatType       => 'M',
        artistName       => 'G',
        upc              => 'I',
        isrc             => 'J',
        trackName        => 'F',
        albumName        => 'F',
        price            => 'Y',
        units            => 'N',
        currencyCode     => 'S',
        countryCode      => 'B',
    }
}

sub _headerIdentifier { ( upc => 'Code BArre' ) }

sub saleDates {
    my $self = shift;

    my $dateBegin;
    my $dateEnd;

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

    if( $startDate )
    {
        ($dateBegin,$dateEnd) = $self->_getDates( date_begin => $startDate );
    }

    return ($dateBegin, $dateEnd);
}

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName( 'productType' ) || return;
    if( $type =~ /^a$/i )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    } 
    elsif( $type =~ /^t$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    } 
    else
    {
        $self->fail( "Could not parse product type from '$type'" );
    }
}

sub formatType  {
    my $self = shift;
    my $type = $self->_getByFieldName( 'formatType' ) || return;
    if( $type =~ /^download$/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif( $type =~ /^highdef$/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOADPREMIUM;
    }
    else
    {
        $self->fail( "Could not parse format type from '$type'" );
    }
}

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

    my %countryMap = (
        'norway (svalbard and jan mayen islands)' => 'NO',
    );
    $name = $countryMap{ lc $name } if( exists $countryMap{lc $name} );

    my $o = Common::Country->Get( $name );
    $self->fail( "Unknown country '$name'" ) if( !defined $o );
    return (defined $o) ? $o->alpha2() : "";
}

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

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

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

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