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

package RPS::Import::Sony::v20;

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/rps/lib';
use RPS::Import::ServiceMap;
use base 'RPS::Import::Importer';

sub _fieldMap {
    {
        dateBegin  => 'A',
        dateEnd    => 'A',
        artistName => 'D',
        serviceID  => 'C',
        units      => 'K',
        price      => 'L',
    };
}

sub _unverifiedFieldMap {
    {
        config    => 'F',
        trans     => 'J',
        title     => 'E',
        prodGroup => 'G',
    };
}

sub _headerIdentifier { ( 'title' => 'title' ) }

sub countryCode {
    my $self = shift;
    return $self->{_countryCode};    # From filename; see _preProcess
}

sub currencyCode {
    my $self = shift;
    return $self->{_currencyCode};    # From filename; see _preProcess
}

sub saleDates {
    my $self      = shift;
    my $startDate = $self->_getByFieldName('dateBegin');

    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 serviceID {
    my $self    = shift;
    my $service = $self->_getByFieldName('serviceID');

    $self->fail("Service name not defined") unless ($service);

    my $serviceID = $self->getServiceID( lc($service) ) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };

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

    return $serviceID;
}

sub productType {
    my $self = shift;
    my $config = $self->_getByFieldName('config') || return;

    if (   $config =~ /^digital audio bundle$/i
        || $config =~ /^digital audio extended play$/i
        || $config =~ /^digital audio longplay$/i
        || $config =~ /^digital (combo|video) longform$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } elsif ( $config =~ /^digital\s+(audio|video) track$/i
        || $config =~ /^mastertone$/i
        || $config =~ /^ringback tone$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    } else {
        $self->fail("Unable to determine product type from config($config)");
    }
}

sub formatType {
    my $self  = shift;
    my $trans = $self->_getByFieldName('trans');

    if ( $trans =~ /^(stream|play|broadcast)$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $trans =~ /^download tethered$/i ) {
        return RPS::File::Sale::FORMAT_TETHERED;
    } elsif ( $trans =~ /^download permanent$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } else {
        $self->fail("Unable to determine format type from trans($trans)");
    }
}

sub mediaType {
    my $self      = shift;
    my $prodGroup = $self->_getByFieldName('prodGroup');

    if ( $prodGroup =~ /^(single|album|mobile tone)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    } elsif ( $prodGroup =~ /^video$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    } else {
        $self->fail("Unable to determine media type from config($prodGroup)");
    }
}

sub _preProcess {
    my $self     = shift;
    my %args     = @_;
    my $fileName = $args{file}->OrigFileName();

    my $currencyCode;
    my $countryCode;

    if ( $fileName =~ /AUS/i ) {
        $countryCode  = "AU";
        $currencyCode = "AUD";
    } else {
        $self->fail("Unable to determine country/currency code from filename");
    }

    $self->{_currencyCode} = $currencyCode;
    $self->{_countryCode}  = $countryCode;
}

sub units {
    my $self  = shift;
    my $units = $self->_getByFieldName('units') || return;
    my $price = $self->_getByFieldName('price') || return;

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

sub albumName {
    my $self = shift;
    my $name = $self->_getByFieldName('title');

    if ( $self->productType() eq RPS::File::Sale::TYPE_ALBUM ) {
        return $name;
    } else {
        return '';
    }
}

sub trackName {
    my $self = shift;
    my $name = $self->_getByFieldName('title');

    if ( $self->productType() eq RPS::File::Sale::TYPE_TRACK ) {
        return $name;
    } else {
        return '';
    }
}

sub _isValidSaleRecord {
    my $self = shift;

    # The line is valid if it has units and an album/track title
    #
    return ( ( $self->units() != 0 || ( $self->units == 0 && $self->price != 0 ) ) && ( $self->albumName() || $self->trackName() ) );
}

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