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

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use lib '/app/tools/data_classes/lib';

use Client::Service;
use RPS::Import::ServiceMap;

use base qw/RPS::Import::MapFaceBase RPS::Import::Importer/;

sub _fieldMap {
    {
        dateBegin    => 'A',
        countryCode  => 'U',
        currencyCode => 'S',
        serviceID    => 'T',
        artistName   => 'P',
        isrc         => 'N',
        upc          => 'I',
        trackName    => 'K',
        albumName    => 'H',
        units        => 'G',
        price        => 'R',
    },
    ;
}

sub _headerIdentifier { upc => 'album_upc' }

sub productType { RPS::File::Sale::TYPE_TRACK }
sub formatType  { RPS::File::Sale::FORMAT_STREAM }
sub mediaType   { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub serviceID {
    my $self = shift;

    my $name = $self->_getByFieldName('serviceID') || '';
    my $serviceID = $self->getServiceID( lc($name) ) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $name };
    if ($name =~ /^carrier billing$/i){
        $serviceID = Client::Service::DSP_TIDAL;
    }
    return $serviceID if $serviceID;

    $self->handleError( "Unknown service: '$name'", RPS::DB::Item::SaleImportError::kErrorService );
}

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 countryCode {
    my $self = shift;

    my $name = $self->_getByFieldName('countryCode') || '';
    my $country = Common::Country->Get($name);
    return $country->alpha2() if $country;

    $self->fail("Unable to determine countryCode from: $name");
}

sub upc {
    my $self = shift;

    my $upc = $self->_getByFieldName('upc') || 0;
    $upc =~ s/Unknown/0/i;

    return (($upc =~ /^\d+$/) ? sprintf "%s", $upc * 1 : undef )
}

sub _mapFaceServiceID   { 'serviceID' }
sub _mapFaceCountryCode {}
sub _mapFaceProductType {}
sub _mapFaceFormatType  {}
sub _mapFaceMediaType   {}


1;
