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

use strict;

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

sub _fieldMap {
    {
        labelName       => 'F',
        dateBegin       => 'C',
        countryCode     => 'V',
        clientProductID => 'H',
        currencyCode    => 'U',
        productType     => 'G',
        artistName      => 'D',
        isrc            => 'J',
        upc             => 'I',
        trackName       => 'E',
        albumName       => 'E',
        units           => 'P',
        price           => 'Q',
    },
    ;
}

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

sub formatType { RPS::File::Sale::FORMAT_DOWNLOAD }
sub mediaType  { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');
    if ( $type =~ /^(mp3|flac|wav) track$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    } elsif ( $type =~ /^(mp3|flac|wav) release$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    }
    $self->fail("Unknown product type '$type'");
}

sub saleDates {
    my $self = shift;

    return ( $self->{_dateBegin}, $self->{_dateEnd} ) if ( $self->{_dateBegin} );

    my $dt = $self->_getByFieldName('dateBegin');

    my ( $startDate, $endDate ) = $self->_getDates( date_begin => $dt );

    if ( $startDate && $endDate ) {
        ( $self->{_dateBegin}, $self->{_dateEnd} ) = ( $startDate, $endDate );
    } else {
        $self->fail("Could not date from '$dt'");
    }
}

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

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

sub isrc {
    my $self = shift;
    return undef if ( $self->productType eq RPS::File::Sale::TYPE_ALBUM );
    my $isrc = $self->_getByFieldName('isrc');
    if ($isrc) {
        return Common::Util::normalize_isrc($isrc);
    }
}

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