package RPS::Import::PIAS::Combined::v3;

use strict;
use warnings;

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

use base 'RPS::Import::PIAS::Combined::v2';

## This importer version is mapped to service DSP_PIAS_LICENSING_DEAL
## but it is pretty like PIAS combined v2 with slightly different format_tytpe logic.
## Please note: sales_files are stored in the 'pias_licensing_deal_1507/001' directory.

sub _fieldMapExtended {
    {
        mixed => {
            option1 => {
                serviceID    => 'F',
                countryCode  => 'E',
                dateBegin    => 'E13',
                dateEnd      => 'E13',
                productType  => 'G',
                formatType   => 'H',
                artistName   => 'A',
                upc          => 'D',
                albumName    => 'B',
                isrc         => 'C',
                trackName    => 'B',
                units        => 'P',
                currencyCode => 'E12',
                retailPrice  => 'K',
                totalRevenue => 'Q',
                price        => 'Q',
                priceLevel   => 'H',
                mediaType    => 'G',
            },
        },
    };
}

sub _unverifiedFieldMapExtended {
    {
        mixed => {
            option1 => {
                _netUnits => 'P',
            },
        },
    };
}

sub _headerIdentifierExtended {
    {
        mixed => {
            option1 => {
                artistName => 'ARTIST',
            },
        },
    };
}


sub formatType {
    my $self = shift;

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

    if ( $type =~ /^Third Party License$/i ) {
        my $price = $self->_getByFieldName('retailPrice');
        my $units = $self->units();

        if ( $self->productType() eq RPS::File::Sale::TYPE_TRACK ){
            return ((($price / $units) < 0.15) ? RPS::File::Sale::FORMAT_STREAM : RPS::File::Sale::FORMAT_DOWNLOAD);
        }
        if ( $self->productType() eq RPS::File::Sale::TYPE_ALBUM ){
            return ((($price / $units) < 1.53) ? RPS::File::Sale::FORMAT_STREAM : RPS::File::Sale::FORMAT_DOWNLOAD);
        }
    }

    return RPS::File::Sale::FORMAT_DOWNLOAD if ($type =~ /^FULL PRICE$/i) ;

    if ( $type =~ /^(?: A\sla\scarte\sdownload\sProduct | INVOICE )$/xi ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $type =~ /^(?:
        Breakage | Cloud\sStreaming | Streaming | Official\sAd\-funded\sStreaming\sProduct |
        Radio\sstreaming\sproduct | Discount\ssubsc'\sstreaming\sproduct |
        Subsc'\s(?: Stream\sProd\s\(A\)|UGC\sproduct ) | Subscr'\sStreaming\sproduct )$/xi ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $type =~ /^(?:Subscription DL Product|Subscr' Stream prod\(teth\))$/i ) {
        return RPS::File::Sale::FORMAT_TETHERED;
    }

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

sub units {
    my $self = shift;

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

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

    return $units;
}

sub priceLevel {
    my $self = shift;

    my $level = $self->_getByFieldName('priceLevel') || '';

    if ( $level =~ /^ CREDIT\sNOTE | INVOICE $/xi ) {
        return RPS::File::Sale::PLEVEL_UNKNOWN;
    } elsif ( $level =~ /^ FULL\sPRICE $/xi ) {
        return RPS::File::Sale::PLEVEL_FULL;
    } elsif ( $level =~ /^MID PRICE$/i ) {
        return RPS::File::Sale::PLEVEL_MID;
    } elsif ( $level =~ /^Third Party License$/i ) {
        return RPS::File::Sale::PLEVEL_UNKNOWN;
    }

    $self->fail( "Unable to determine price_level from: '$level'." );
}

1;
