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

package RPS::Import::HRA::v1;

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::UTF8;

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

# Orange has three sheets that need to be processed, fortunately
# the columns of interest on those sheets are all in same location.
#
sub _fieldMap {
    {
        labelName    => 'S',
        dateBegin    => 'C',
        dateEnd      => 'D',
        productType  => 'K',
        formatType   => 'I',
        artistName   => 'Q',
        upc          => 'N',
        isrc         => 'O',
        trackName    => 'R',
        albumName    => 'R',
        price        => 'AD',
        units        => 'L',
        currencyCode => 'X',
        countryCode  => 'H',
    };
}

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

sub saleDates {
    my $self = shift;

    my $dateBegin;
    my $dateEnd;

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

    $startDate = join( "/", $1, $2, $3 ) if ( $startDate =~ /^(\d{1,2})\.(\d{1,2})\.(\d{4})$/ );
    $endDate   = join( "/", $1, $2, $3 ) if ( $endDate =~ /^(\d{1,2})\.(\d{1,2})\.(\d{4})$/ );

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

    return ( $dateBegin, $dateEnd );
}

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType') || return;

    if ( $type =~ /^bundle$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } else {
        $self->fail("Could not parse product type from '$type'");
    }
}

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

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

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