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

package RPS::Import::DripFM;

use strict;

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

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

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

sub _fieldMap {
    {
        labelName     => 'I',
        countryCode   => 'P',
        dateBegin     => 'W',
        dateEnd       => 'X',
        formatType    => 'M',
        serviceProductID => 'J',
        artistName    => 'E',
        upc           => 'K',
        isrc          => 'L',
        albumName     => 'F',
        trackName     => 'D',
        trackNum      => 'C',
        units         => 'O',
    }
}


sub _headerIdentifier { (labelName => 'creative_name') }


sub currencyCode { 'USD' }


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


sub saleDates {
    my $self = shift;
    my $dateBegin = $self->_getByFieldName('dateBegin');
    my $dateEnd = $self->_getByFieldName('dateEnd');
    return $self->_getDates(date_begin => $dateBegin, date_end => $dateEnd);
}


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


sub formatType {
    my $self = shift;
    my $format = $self->_getByFieldName('formatType');
    if ($format =~ /^(OnDemandStream|PreviewStream)$/i)
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif ($format =~ /^PermanentDownload$/i)
    {
        return RPS::File::Sale::FORMAT_VPD;
    }
    else
    {
        $self->fail("Unable to determine format type from '$format'");
    }
}


sub countryCode {
    my $self  = shift;
    my $country = $self->_getByFieldName('countryCode');
    $country = "US" if(!defined $country || '' eq $country);
    my $obj = Common::Country->Get($country);
    return $obj ? $obj->alpha2 : undef;
}

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