package RPS::Import::INGrooves::v8;

use strict;

use lib '/app/tools/common/lib';
use Common::Date;
use Common::Country;

use lib '/app/tools/rps/lib';
use base 'RPS::Import::MapFaceBase';
use base 'RPS::Import::INGroovesBase';

use Data::Dumper;

sub _headerIdentifier { ( upc => 'UPC/EAN' ) }

sub _fieldMap {
    {
        serviceID        => 'B',
        labelName        => 'U',
        countryCode      => 'M',
        dateBegin        => 'A',
        dateEnd          => 'D',
        productType      => 'R',
        formatType       => 'V',
        artistName       => 'H',
        serviceProductID => 'K',
        upc              => 'J',
        albumName        => 'G',
        trackName        => 'E',
        isrc             => 'I',
        units            => 'N',
        price            => 'P',
    };
}

sub _unverifiedFieldMap {
    { assetType => 'Q', };
}

sub currencyCode { 'USD' }

sub countryCode {
    my $self = shift;
    my $name = $self->SUPER::countryCode();

    if ($name) {

        $name =~ s/,.*//;

        $name = $self->_getAlternateCountryName($name) || $name;

        my $country = new Common::Country( search => $name );

        if ($country) {
            return $country->alpha2();
        }

        return $name;
    }
}

sub productType {
    my $self        = shift;
    my $consumption = $self->_getByFieldName('formatType');             # col V ("CONSUMPTION")
    my $assetType   = $self->_getByFieldName('assetType') || return;    # col Q ("ASSET TYPE")
    my $service     = $self->_serviceName();

    if ( !$consumption ) {

        # if CONSUMPTION (col V) is blank, use product format mappings
        #
        my ( $serviceID, $consumption, $productType, $mediaType ) = $self->_getServiceFormat( $service, $assetType );
        return $productType if ($productType);
    } elsif ( $assetType =~ /track/i || $assetType =~ /video/i || $assetType =~ /ringtone/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    } elsif ( $assetType =~ /album/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    }

    return $self->_getByFieldName('productType');
}

sub formatType {
    my $self = shift;

    my $consumption = $self->_getByFieldName('formatType');             # col V ("CONSUMPTION")
    my $assetType   = $self->_getByFieldName('assetType') || return;    # col Q ("ASSET TYPE")
    my $service     = $self->_serviceName();

    if ( !$consumption ) {

        # if CONSUMPTION (col V) is blank, use product format mappings
        #
        my ( $serviceID, $formatType, $productType, $mediaType ) = $self->_getServiceFormat( $service, $assetType );
        return $formatType if ($formatType);
    } elsif ( $consumption =~ /^(download|burn)$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $consumption =~ /^(portable|stream|client|radio|kiosk)$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $consumption =~ /^jukebox$/i ) {
        return RPS::File::Sale::FORMAT_JUKEBOX;
    } elsif ( $consumption =~ /^ringtone$/i ) {
        return RPS::File::Sale::FORMAT_RINGTONE;
    } elsif ( $consumption =~ /^tethered download$/i ) {
        return RPS::File::Sale::FORMAT_TETHERED;
    }

    return $consumption;
}

sub mediaType {
    my $self = shift;

    my $productType = $self->_getByFieldName('productType');            # col R ("PRODUCT TYPE")
    my $assetType   = $self->_getByFieldName('assetType') || return;    # col Q ("ASSET TYPE")
    my $service     = $self->_serviceName();

    if ( !$productType ) {

        # if PRODUCT TYPE (col R) is blank, use product format mappings
        #
        my ( $serviceID, $formatID, $productType, $mediaType ) = $self->_getServiceFormat( $service, $assetType );
        if ($mediaType) {
            return $mediaType;
        } else {

            #$self->fail("Cannot determine media type for service '$service' using asset type '$assetType'");
            return $assetType;
        }

    }
    if (   $productType =~ /^(audio album|master service tone\/ ringback tone|track)$/i
        || $productType =~ /^(high quality audio album)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    } elsif ( $productType =~ /^(music video|video)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    } elsif ( lc($productType) eq 'audio video bundle' ) {
        if   ( lc($assetType) eq 'video' ) { return RPS::DB::Item::MediaType::kMediaTypeVideo }
        else                               { return RPS::DB::Item::MediaType::kMediaTypeAudio }
    }

    return $productType;
}

sub _serviceName {
    my $self = shift;
    my $service = $self->_getByFieldName('serviceID') || return;
    $service =~ s/ (\w{3})$//;

    return $service;
}

sub serviceID {
    my $self    = shift;
    my $type    = $self->_getByFieldName('assetType');
    my $service = $self->_serviceName();

    if ( $type && $service ) {
        my ( $serviceID, $formatID, $productType, $mediaType ) = $self->_getServiceFormat( $service, $type );
        return $serviceID if ($serviceID);
    }

    return $self->handleError( "Unknown service: $service", RPS::DB::Item::SaleImportError::kErrorService );
}

sub price {
    my $price = abs( shift->SUPER::price() );
    return $price;
}

sub units {
    my $self  = shift;
    my $units = $self->SUPER::units();
    my $price = $self->SUPER::price();

    if ( defined($price) ) {
        return $price < 0 ? abs($units) * -1 : $units;
    } else {
        return $units;
    }

}

sub unitPrice { abs( shift->SUPER::unitPrice ) }

## MapFace Stuff

sub _mapFaceServiceID {
    return ('serviceID');
}

sub _mapFaceCountryCode {
    return ('countryCode');
}

sub _mapFaceProductType {
    return ( 'formatType', 'assetType', 'serviceID' );
}

sub _mapFaceFormatType {
    return ( 'formatType', 'assetType', 'serviceID' );
}

sub _mapFaceMediaType {
    return ( 'productType', 'assetType', 'serviceID' );
}

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