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

package RPS::Import::MusicQubed::v1;

use strict;

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

use lib '/app/tools/rps/lib';
use RPS::Import::ServiceMap;

use base 'RPS::Import::Importer';

sub _fieldMap {
    {
        dateBegin    => 'C',
        dateEnd      => 'D',
        formatType   => 'E',
        productType  => 'H',
        artistName   => 'J',
        isrc         => 'I',
        trackName    => 'K',
        price        => 'P',
        units        => 'L',
        countryCode  => 'S',
        currencyCode => 'V',
    };
}

sub _noHeader { 1 }

sub _unverifiedFieldMap {
    { altCountryCode => 'T', };
}

sub _isValidSaleRecord {
    my $self      = shift;
    my $units     = $self->_getByFieldName('units');
    my $trackName = $self->_getByFieldName('trackName');

    return ( $units && $trackName ) ? 1 : undef;
}

sub countryCode {
    my $self       = shift;
    my $country    = $self->_getByFieldName('countryCode');
    my $altCountry = $self->_getByFieldName('altCountryCode');

    return ( defined $country ) ? $country : $altCountry;

}

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

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

    if ( $type =~ /^stream$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $type =~ /^download$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }

    $self->fail("Unable to determine formatType from '$type'");

}

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

    if ( $type =~ /^electronic single$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    }

    $self->fail("Unable to determine productType from '$type'");

}

sub saleDates {
    my $self = shift;

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

    my ( $startDate, $endDate ) = $self->_getDates( date_begin => $dateBegin, date_end => $dateEnd );

    return ( $startDate, $endDate );

}

sub _preProcess {
    my $self = shift;

    my %args = @_;

    Log->info("Starting _preProcess");

    if ( $self->{sheetname} =~ /^(substriptions|a la carte)$/i ) {
        return $self->SUPER::_preProcess(%args);
    }
}

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