package RPS::Import::Pirames::v1;

use strict;
use warnings;

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

use parent  qw/RPS::Import::MapFaceBase RPS::Import::ImporterMixed/;

sub physical { 0 }

sub _fieldMapExtended {
    {
        digital => {
            option1 => {
                serviceID        => 'B',
                countryCode      => 'E',
                productType      => 'D',
                formatType       => 'D',
                serviceProductID => 'L',
                artistName       => 'F',
                upc              => 'I',
                albumName        => 'K',
                isrc             => 'H',
                trackName        => 'G',
                units            => 'J',
                price            => 'O',
                mediaType        => 'C',
            },
        },
    };
}

sub _unverifiedFieldMapExtended { {} }

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

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->handleError( "Unknown service: '$service'", RPS::DB::Item::SaleImportError::kErrorService )
}

sub currencyCode { 'EUR' }

sub saleDates {
    my $self = shift;

    my %quarters = (
        1 => { start => '01-01', end => '03-31' },
        2 => { start => '04-01', end => '06-30' },
        3 => { start => '07-01', end => '09-30' },
        4 => { start => '10-01', end => '12-31' },
    );

    my ($year, $qurter) = $self->filename =~ /(\d{4}).Q(\d)/i;
    my $startDate = sprintf( "%04d-%s", $year, $quarters{$qurter}{start} );
    my $endDate   = sprintf( "%04d-%s", $year, $quarters{$qurter}{end} );

    return $startDate, $endDate;
}

sub countryCode {
    my $self = shift;

    my $country = $self->_getByFieldName('countryCode') || 'IT';
    my $oCountry = Common::Country->new( search => $country );
    return $oCountry->alpha2 if $oCountry;

    $self->handleError( "Unknown country code: '$country'", RPS::DB::Item::SaleImportError::kErrorCountry );
    return $country;
}

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('productType') || '';
    if ( $type =~ /^Album Download$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    }
    if ( $type =~ /^(?:Single Download|Streaming (?:Ad Supported|Premium)|Orchard Sync)$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    }

    return $self->handleError( "Unknown product type: '$type'", RPS::DB::Item::SaleImportError::kErrorProductType );
}

sub formatType {
    my $self = shift;

    my $type = $self->_getByFieldName('formatType') || '';
    if ( $type =~ /^(?:Single|Album) Download$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    if ( $type =~ /^Streaming (?:Ad Supported|Premium)$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    if ( $type =~ /^Orchard Sync$/i ) {
        return RPS::File::Sale::FORMAT_SYNC;
    }

    return $self->handleError( "Unknown format type: '$type", RPS::DB::Item::SaleImportError::kErrorFormatType );
}

sub mediaType {
    my $self = shift;

    my $type = $self->_getByFieldName('mediaType') || '';
    if ( $type =~ /^(?:Sound Recording|Web)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    if ( $type =~ /^Music Video$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }

    return $self->handleError( "Unknown media type: '$type", RPS::DB::Item::SaleImportError::kErrorMediaType );
}

# digital
sub price     { 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;
}

# mapFace
sub _mapFaceCountryCode { 'countryCode' }
sub _mapFaceServiceID   { 'serviceID'   }
sub _mapFaceProductType { 'productType' }
sub _mapFaceFormatType  { 'formatType'  }
sub _mapFaceMediaType   { 'mediaType'   }


1;