package RPS::Import::AlphaPup::v2;

use strict;

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

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

sub physical { 0 }

sub _fieldMapExtended {
    {
        digital => {
            option1 => {
                labelName    => 'A',
                countryCode  => 'M',
                dateBegin    => 'C',
                productType  => 'J',
                artistName   => 'H',
                upc          => 'D',
                albumName    => 'F',
                isrc         => 'G',
                trackName    => 'I',
                units        => 'O',
                currencyCode => 'U1',
                price        => 'U',
            }
        }
    };
}

sub _unverifiedFieldMapExtended {
    {
        digital => {
            option1 => {
                _dsp             => 'A',
                _albumArtist     => 'E',
                _consumtionType  => 'L',
                _consumtionClass => 'K',
            },
        },
    };
}

sub _headerIdentifierExtended {
    {
        digital => {
            option1 => { upc => 'UPC' },
        },
    };
}

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

my %formatMediaTypes = (
    'stream' => {
        ondemandstream => {
            format_type => RPS::File::Sale::FORMAT_STREAM,
            media_type  => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
        premiumaccessstream => {
            format_type => RPS::File::Sale::FORMAT_STREAM,
            media_type  => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
        limitedinteractivestream => {
            format_type => RPS::File::Sale::FORMAT_STREAM,
            media_type  => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
        noninteractivestream => {
            format_type => RPS::File::Sale::FORMAT_PERFORMANCE  ,
            media_type  => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
        format_type => RPS::File::Sale::FORMAT_STREAM,
        media_type  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'download' => {
        format_type => RPS::File::Sale::FORMAT_DOWNLOAD,
        media_type  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'contentid' => {
        format_type => RPS::File::Sale::FORMAT_STREAM,
        media_type  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'locker' => {
        format_type => RPS::File::Sale::FORMAT_STREAM,
        media_type  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
);

sub saleDates {
    my $self = shift;

    my $date = lc( $self->_getByFieldName('dateBegin') || '' );

    # Nov-2019
    if ( my ($m, $y) = $date =~ /^([a-z]{3})[_-](\d{4})$/i ) {
        return $self->_getDates( date_begin => sprintf( "%04d-%02d-%02d", $y, $months{$m}, 1 ) );
    }
    # 2019_11
    elsif ( ($y, $m) = $date =~ /^(\d{4})_(\d{1,2})$/i ) {
        return $self->_getDates( date_begin => sprintf( "%04d-%02d-%02d", $y, $m, 1 ) );
    }
    # 11/1/2019
    elsif ( ($m, $y) = $date =~ /^(?:\d{1,2})\/(\d{1,2})\/(\d{4})$/i ) {
        return $self->_getDates( date_begin => sprintf( "%04d-%02d-%02d", $y, $m, 1 ) );
    }

    $self->fail( "Unable to determine dates of a sale from: '$date'" );
}

sub productType {
    my $self = shift;

    my $conf = lc( $self->_getByFieldName('productType') || '' );
    my $track = lc( $self->_getByFieldName('trackName') || '' );

    return RPS::File::Sale::TYPE_TRACK if ($conf eq "track" || ($conf eq "album" && $track));
    return RPS::File::Sale::TYPE_ALBUM if ($conf eq "album" && !$track);

    $self->fail("Unable to determine productType from: '$conf/$track'");
}

sub formatType {
    my $self = shift;

    my $dsp = lc( $self->_getByFieldName('_dsp') || '' );
    my $consumtionType = lc( $self->_getByFieldName('_consumtionType') || '' ) =~ s/\s+//gr;
    my $consumtionClass = lc( $self->_getByFieldName('_consumtionClass') || '' ) =~ s/\s+//gr;

    if ( $dsp =~ /^Pandora$/i && exists($formatMediaTypes{$consumtionClass}{$consumtionType})) {
        return $formatMediaTypes{$consumtionClass}{$consumtionType}{format_type};
    }
    elsif ( exists $formatMediaTypes{$consumtionClass} ) {
        return $formatMediaTypes{$consumtionClass}{format_type};
    }

    $self->fail("Unbale to determine format type from '$consumtionClass/$dsp/$consumtionType'");
}

sub mediaType {
    my $self = shift;

    my $dsp = lc( $self->_getByFieldName('_dsp') || '' );
    my $consumtionType = lc( $self->_getByFieldName('_consumtionType') || '' ) =~ s/\s+//gr;
    my $consumtionClass = lc( $self->_getByFieldName('_consumtionClass') || '' ) =~ s/\s+//gr;

    if ( $dsp =~ /^Pandora$/i && exists($formatMediaTypes{$consumtionClass}{$consumtionType})) {
        return $formatMediaTypes{$consumtionClass}{$consumtionType}{media_type};
    }
    elsif ( exists $formatMediaTypes{$consumtionClass} ) {
        return $formatMediaTypes{$consumtionClass}{media_type};
    }

    $self->fail("Unbale to determine media type from: '$consumtionClass/$dsp/$consumtionType'");
}

sub artistName {
    my $self = shift;

    my $track_artist = $self->_getByFieldName("artistName") || "";
    my $album_artist = $self->_getByFieldName("_albumArtist") || "";

    return $track_artist if $self->productType() eq 'T';
    return $album_artist if $self->productType() eq 'A';
}

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;
}

sub currencyCode {
    my $self = shift;

    my $code = $self->_getByFieldName('currencyCode') || '';
    if ( $code =~ /^Net (\w{3})$/i ) {
        return $1;
    }

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

1;
