package RPS::Import::Symphonic::v1;

use strict;
use warnings;

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use RPS::Import::ServiceMap;
use Date::Calc qw(Decode_Month Days_in_Month);

use parent 'RPS::Import::ImporterMixed';

sub physical { 0 }

sub _fieldMapExtended {
    {
        digital => {
            option1 => {
                labelName       => 'B',
                serviceID       => 'L',
                countryCode     => 'N',
                dateBegin       => 'M',
                productType     => 'J',
                formatType      => 'O',
                clientProductID => 'G',
                artistName      => 'K',
                upc             => 'F',
                albumName       => 'C',
                isrc            => 'J',
                trackName       => 'H',
                units           => 'R',
                currencyCode    => 'S1',
                price           => 'S',
            }
        }
    };
}

sub _unverifiedFieldMapExtended {{}}

sub _headerIdentifierExtended {
    {
        digital => {
            option1 => { isrc => 'ISRC Code' },
        },
    };
}


my %months = (
    'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6,
    'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12,
);

sub saleDates {
    my $self = shift;

    my $dt = $self->_getByFieldName('dateBegin') || '';
    my ($startDate, $endDate);

    # MMM-YY
    if ( my ($month, $year) = $dt =~ /^(\w{3,9})[ -](\d{2,4})$/ ) {
        $month = $months{ lc( substr($month, 0, 3) ) };
        $year = "20$year" if length($year) == 2;

        $startDate = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        $endDate = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
	}
    # YY-MMM
    elsif ( ($year, $month) = $dt =~ /^(\d{2,4})[ -](\w{3,9})$/ ) {
        $month = $months{ lc( substr($month, 0, 3) ) };
        $year = "20$year" if length($year) == 2;

        $startDate = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        $endDate = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) );
    }

    unless ( $startDate && $endDate ) {
        $self->fail( "Unable to determine sale dates from: $dt" );
    }

    return ( $startDate, $endDate );
}

sub countryCode {
    my $self = shift;

    my $name = $self->_getByFieldName('countryCode') || '';
    return 'TL' if $name =~ /^TMP$/i;
    return 'JP' if $name =~ /^JPY$/i;

    my $country = Common::Country->Get($name) if $name;
    return $country->alpha2 if $country;

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

sub serviceID {
    my $self = shift;

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

    return $self->fail( "Unable to determine service from: '$service'" );
}

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('productType') || '';
    if ( $type ) {
        return RPS::File::Sale::TYPE_TRACK;
    }

    $self->fail( "Unable to determine product type from: '$type'" );
}

sub formatType {
    my $self = shift;

    my $type = $self->_getByFieldName('formatType') || '';

    return RPS::File::Sale::FORMAT_DOWNLOAD    if $type =~ /^Download$/i;
    return RPS::File::Sale::FORMAT_STREAM      if $type =~ /^(?:Streaming|UGC|Background Music|Audio Tier)$/i;
    return RPS::File::Sale::FORMAT_PERFORMANCE if $type=~ /^Webcasting$/i;

    $self->fail( "Unable to determine format type from: '$type'" );
}

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

sub currencyCode {
    my $self = shift;

    my $code = $self->_getByFieldName('currencyCode') || '';
    return 'USD' if $code =~ /\$US/i;

    $self->fail( "Unable to determine currency code from: '$code'" );
}

sub price     { sprintf "%.8f", abs( shift->_getByFieldName('price') || 0 ) }
sub unitPrice { abs shift->SUPER::unitPrice }
sub units {
    my $self = shift;

    my $units = $self->_getByFieldName('units') || 0;
    my $price = $self->_getByFieldName('price') || 0;

    if ( $price > 0 ) {
        $units = $units ? abs($units) : 1;
    } elsif ( $price < 0 ) {
       $units = $units ? -1 * abs($units) : -1;
    }

    return $units;
}

sub _isValidSaleRecord {
    my $self = shift;

    return 1 if $self->units * 1 || $self->price * 1;
}


1;