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

package RPS::Import::BelieveDigital::v7;

use strict;

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

sub _fieldMap {
    my $self    = shift;
    my $version = $self->_version;

    my %field_map = (
        7 => {
            labelName    => 'C',
            serviceID    => 'W',
            countryCode  => 'O',
            currencyCode => 'K',
            dateBegin    => 'Q',
            formatType   => 'X',    # TYPE
            artistName   => 'B',
            upc          => 'L',
            albumName    => 'D',
            isrc         => 'H',
            trackName    => 'G',
            units        => 'R',
            price        => 'V',
        },
        9 => {
            labelName    => 'A',
            serviceID    => 'AB',
            countryCode  => 'R',
            currencyCode => 'N',
            dateBegin    => 'S',
            formatType   => 'F',    # TYPE
            artistName   => 'C',
            upc          => 'D',
            albumName    => 'G',
            isrc         => 'K',
            trackName    => 'J',
            units        => 'T',
            price        => 'X',
        },
    );
    return $field_map{$version};
}

sub saleDates {
    my $self = shift;

    my $dateBegin;
    my $dateEnd;

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

    if ( $startDate =~ /^\d{5}$/ ) {
        $startDate = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $startDate );
    }

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

    return ( $dateBegin, $dateEnd );
}

sub _cleanField {
    my $self = shift;
    my $f    = shift;
    $f =~ s/-//g;
    return $f;
}

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

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

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

    if ( $track && $isrc ) {
        return RPS::File::Sale::TYPE_TRACK;
    } else {
        return RPS::File::Sale::TYPE_ALBUM;
    }
    $self->fail("Unable to determine product type");
}

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