package RPS::Import::SiriusXM;

use strict;

use Data::Dumper;
use lib '/app/tools/common/lib';
use Common::Util qw(normalize_upc normalize_isrc);
use Date::Calc qw(Days_in_Month);

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

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

sub _headerIdentifier { ( isrc => 'ISRC' ) }

sub _fieldMap {
    my $self = shift;
    
    my %fieldMap =
    (
        1 => {
            labelName        => 'B',
            serviceID        => 'N',
            countryCode      => 'M',
            dateBegin        => 'K',
            dateEnd          => 'L',
            formatType       => 'O',
            clientProductID  => 'C',
            artistName       => 'E',
            upc              => 'P',
            albumName        => 'F',
            isrc             => 'G',
            trackName        => 'D',
            units            => 'H',
            price            => 'J',
        },
    );         
        
    return $fieldMap{$self->_version()};
}

sub mediaType    { RPS::DB::Item::MediaType::kMediaTypeAudio }
sub productType  { RPS::File::Sale::TYPE_TRACK }
sub currencyCode { 'USD' }

sub serviceID {
    my $self = shift;
	my $service = $self->_getByFieldName( 'serviceID' );
	
    my $serviceID = $self->getServiceID(lc ($service)) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };	

    return $serviceID if( $serviceID );

    $self->fail( "Unknown service: $service" );
}

sub countryCode {
    my $self = shift;
    
    my $name = $self->_getByFieldName( 'countryCode' );
    my $country = Common::Country->Get( $name );
	if ($country) {
		return $country->alpha2();
	} else {
		return $name;
	}        
}

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

    if( $type =~ /^Non-Webcast$/i ) {
        return RPS::File::Sale::FORMAT_PERFORMANCE;
    } 

    $self->fail( "Could not parse format type from '$type'" );
}

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