#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (c) 2013-2014 RoyaltyShare, Inc. All Rights Reserved
#---------------------------------------------------------------

package RPS::Import::Sony::EROS;

use strict;

use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Assert;
use Data::Dumper;

use lib '/app/tools/data_classes/lib';
use Client::Service;

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

sub _fieldMap {}

# This format has no delimiters, so it all comes in as one big field.
#  We'll just substr the crap out it.
sub _unverifiedFieldMap {
    {
        line => 'A',
    }
}

sub _headerIdentifier { ( line => qr/\w{3}-\w{3}  \w\d{3}\w{2} \w\d{27}\w{2}\.{210}/ ) }

sub mediaType   { RPS::DB::Item::MediaType::kMediaTypeAudio}
sub productType { RPS::File::Sale::TYPE_ALBUM }

my %service_map = (
    'P001' => Client::Service::DSP_ITUNES,
    'P038' => Client::Service::DSP_ITUNES,
    'P003' => Client::Service::DSP_REAL,
    'P073' => Client::Service::DSP_VIRGINDIGITAL,
    'P297' => Client::Service::DSP_LINE_CORPORATION,
    'P338' => Client::Service::DSP_TMOBILE,
    'P568' => Client::Service::DSP_MTVN,
    'P630' => Client::Service::DSP_HUTCHINSON,
    'P816' => Client::Service::DSP_7DIGITAL,
    'P906' => Client::Service::DSP_MICROSOFT_GROOVE,
    'P934' => Client::Service::DSP_YOUTUBE,
    'PAA5' => Client::Service::DSP_GOOGLE,
    'PAR6' => Client::Service::DSP_MUSIC_QUBED,
    'PAS9' => Client::Service::DSP_MONDIA_MEDIA,
    'PAY5' => Client::Service::DSP_RIM,
    'PBJ8' => Client::Service::DSP_IODA,
    'PBK5' => Client::Service::DSP_DEEZER,
    'PBR9' => Client::Service::DSP_PCMTECHNOLOGIES,
    'PBT7' => Client::Service::DSP_RARA,
    'PC07' => Client::Service::DSP_CLASSICAL_COM,
    'PC79' => Client::Service::DSP_MYSPACE,
    'PCI2' => Client::Service::DSP_SAINSBURYS,
    'PCO4' => Client::Service::DSP_7DIGITAL,
    'PCO9' => Client::Service::DSP_7DIGITAL,
    'PCS0' => Client::Service::DSP_HDTRACKS,
    'PCX5' => Client::Service::DSP_SONY,
    'PCX6' => Client::Service::DSP_7DIGITAL,
    'PDD7' => Client::Service::DSP_BLOOM_FM,
    'PDI6' => Client::Service::DSP_ORCHARD,
    'PDJ9' => Client::Service::DSP_TRAILER_PARK,
    'PDP5' => Client::Service::DSP_AMAZON,
    'PDT0' => Client::Service::DSP_FRESHPLANET,
    'PDY9' => Client::Service::DSP_IMI_MOBILE,
    'PE34' => Client::Service::DSP_VIDZONEDIGITAL,
    'PED5' => Client::Service::DSP_NAXOS_DIGITAL,
    'PEI0' => Client::Service::DSP_SAMSUNG,
    'PEO0' => Client::Service::DSP_GOOGLE,
    'PEU1' => Client::Service::DSP_SPOTIFY,
    'PEV5' => Client::Service::DSP_QOBUZ,
    'PF90' => Client::Service::DSP_YAHOO,
    'PFE7' => Client::Service::DSP_7DIGITAL_PURE,
    'PFS3' => Client::Service::DSP_YOUTUBE_RED,
    'PFU8' => Client::Service::DSP_RDIO,
    'PG46' => Client::Service::DSP_LASTFM,
    'PGL3' => Client::Service::DSP_SONY,
    'PGP1' => Client::Service::DSP_MEDIANET,
    'PH08' => Client::Service::DSP_BLINKBOX_MUSIC,
    'PH99' => Client::Service::DSP_BT_VISION,
    'PHG4' => Client::Service::DSP_SONY,
    'PHN8' => Client::Service::DSP_TIDAL,
    'PI60' => Client::Service::DSP_MUZU_TV,
    'PIF9' => Client::Service::DSP_NEUROTICMEDIA,
    'PIU0' => Client::Service::DSP_SONY,
    'PIZ3' => Client::Service::DSP_NUGS,
    'PJ16' => Client::Service::DSP_QTRAX,
    'PJ20' => Client::Service::DSP_AMAZON,
    'PJA7' => Client::Service::DSP_HIGH_RESAUDIO,
    'PJG1' => Client::Service::DSP_PRESTO_CLASSICAL,
    'PK11' => Client::Service::DSP_SPOTIFY,
    'PKE4' => Client::Service::DSP_APPLE_MUSIC,
    'PKN5' => Client::Service::DSP_GLOBAL_RADIO,
    'PKQ8' => Client::Service::DSP_JUNO,
    'PLA1' => Client::Service::DSP_MUSIC_QUBED,
    'PLX4' => Client::Service::DSP_QOBUZ,
    'PM12' => Client::Service::DSP_RAKUTEN,
    'PM19' => Client::Service::DSP_ITUNES,
    'PM36' => Client::Service::DSP_SONY_ERICSSON,
    'PM43' => Client::Service::DSP_LIBRARYIDEAS,
    'PP02' => Client::Service::DSP_DADA,
    'PP24' => Client::Service::DSP_VEVO,
    'PS18' => Client::Service::DSP_RDIO,
    'PT33' => Client::Service::DSP_SONY,
    'PT64' => Client::Service::DSP_ITUNES,
    'PV54' => Client::Service::DSP_CATCH_MEDIA,
    'PV77' => Client::Service::DSP_VIDZONEDIGITAL,
    'PW89' => Client::Service::DSP_VIRGINDIGITAL,
    'PY68' => Client::Service::DSP_MUSIC_UNLIMITED,
    'PZ05' => Client::Service::DSP_BEATPORT,
    'Q174' => Client::Service::DSP_AMAZON,
    'PMY1' => Client::Service::DSP_GOOGLE,
);

my %format_map = (
    'CDS' => RPS::File::Sale::FORMAT_STREAM,
    'CDT' => RPS::File::Sale::FORMAT_STREAM,
    'CSD' => RPS::File::Sale::FORMAT_TETHERED,
    'DAS' => RPS::File::Sale::FORMAT_STREAM,
    'DBA' => RPS::File::Sale::FORMAT_STREAM,    
    'DBC' => RPS::File::Sale::FORMAT_TETHERED,
    'DBP' => RPS::File::Sale::FORMAT_STREAM, 
    'DCD' => RPS::File::Sale::FORMAT_TETHERED,
    'DDD' => RPS::File::Sale::FORMAT_DOWNLOAD,
    'DEP' => RPS::File::Sale::FORMAT_STREAM,
    'DES' => RPS::File::Sale::FORMAT_STREAM,   
    'DLK' => RPS::File::Sale::FORMAT_DOWNLOAD, 
    'DLS' => RPS::File::Sale::FORMAT_STREAM,    
    'DOL' => RPS::File::Sale::FORMAT_DOWNLOAD,
    'DOR' => RPS::File::Sale::FORMAT_RINGTONE,
    'DPS' => RPS::File::Sale::FORMAT_STREAM,
    'DSA' => RPS::File::Sale::FORMAT_STREAM,
    'DSD' => RPS::File::Sale::FORMAT_STREAM,
    'DSP' => RPS::File::Sale::FORMAT_STREAM,    
);

sub serviceID {
    my $self = shift;
    my $service = substr($self->_getByFieldName('line'), 250, 4);

    my $serviceID = $service_map{$service};

    $self->fail( "Unable to determine service id from '$service'" ) unless( $serviceID );

    return $serviceID;
}

sub countryCode {
    my $self = shift;
    my $code = substr($self->_getByFieldName('line'), 61, 2);
    
    return $code;
}

sub saleDates {
    my $self = shift;
    my $startDate = substr($self->_getByFieldName('line'), 171, 8);

    my $dateBegin;
    my $dateEnd;
    if( $startDate =~ /^(\d{4})(\d{2})/ ) {
        $dateBegin = sprintf("%04d-%02d-%02d", $1, $2, 1 );
        $dateEnd   = sprintf("%04d-%02d-%02d", $1, $2, Days_in_Month($1, $2) );
    }
    return ( $dateBegin, $dateEnd );
}

sub formatType {
    my $self = shift;
    my $type = substr($self->_getByFieldName('line'), 18, 3);

    my $formatType = $format_map{uc($type)};

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

    return $formatType;
}

sub clientProductID {
    my $self = shift;
    my $id = substr($self->_getByFieldName('line'), 1, 14);
    
    return $id;
}

sub catalogID {
    my $self = shift;
    my $id = substr($self->_getByFieldName('line'), 1, 14);
    
    return $id;
}

sub artistName {
    my $self = shift;
    my $name = substr($self->_getByFieldName('line'), 21, 20);
    
    return $name;
}

sub albumName {
    my $self = shift;
    my $name = substr($self->_getByFieldName('line'), 41, 20);
    
    return $name;
}

sub units {
    my $self = shift;
    my $units = substr($self->_getByFieldName('line'), 65, 8);
    my $sign = substr($self->_getByFieldName('line'), 64, 1);
    
    if ($sign eq '-') {
        $units = $sign . $units;
    }
    
    return $units;
}

sub currencyCode {
    my $self = shift;
    my $code = substr($self->_getByFieldName('line'), 74, 3);
    
    return $code;
}

sub unitPrice {
    my $self = shift;
    my $price = substr($self->_getByFieldName('line'), 100, 10);
    
    if ($price =~ /\./) {
        return $price;
    } else {
        $price = substr($price, 0, 8) . "." . substr($price, 8, 2);
        return $price;
    }
}

sub free {
    my $self = shift;
    my $free = substr($self->_getByFieldName('line'), 15, 3);

    if ($free =~ /FGD/i || $free =~ /PRO/i) {
        return 1;
    }
    return 0;
}

sub price {
    my $self = shift;
    my $price = substr($self->_getByFieldName('line'), 225, 10);
    
    if ($price =~ /\./) {
        return $price;
    } else {
        $price = substr($price, 0, 8) . "." . substr($price, 8, 2);
        return $price;
    }
}

sub _isValidSaleRecord {
	my $self = shift;	
	my $footer = substr($self->_getByFieldName('line'), 0, 1);

    # Checking for the footer.
    return undef if( $footer eq 'Z' );

    return $self->SUPER::_isValidSaleRecord(); 
}

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