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

package RPS::Import::Sony::v12;

use strict;

use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::UTF8;
use Common::Country;

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

sub _fieldMap {
    {
        # detail
        #
        dateBegin     => 'J',
        artistName    => 'B',
        upc           => 'G',
        albumName     => 'C',
        configuration => 'I',
        countryCode   => 'H',
        totalRevenue  => 'AC',
        priceLevel    => 'L',
        retailPrice   => 'M',

    };
}

sub _unverifiedFieldMap {
    { payUnits => 'Z', };
}

sub _headerIdentifier {
    ( 'upc' => 'Product number' );
}

sub currencyCode { 'USD' }

sub channel { RPS::File::Sale::CHANNEL_RETAIL }

sub priceLevel {
    my $self       = shift;
    my $priceLevel = $self->_getByFieldName('priceLevel');
    if ( $priceLevel =~ /^full$/i ) {
        return RPS::File::Sale::PLEVEL_FULL;
    } elsif ( $priceLevel =~ /^mid$/i ) {
        return RPS::File::Sale::PLEVEL_MID;
    } elsif ( $priceLevel =~ /^bud$/i ) {
        return RPS::File::Sale::PLEVEL_BUDGET;
    }
    $self->fail("Unable to determine price level from ($priceLevel)");
}

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

    my $dateBegin;
    my $dateEnd;
    if ( $startDate =~ /(\d{4}) H(\d)/ ) {
        if ( $2 == "1" ) {
            $dateBegin = "$1-01-01";
            $dateEnd   = "$1-06-30";
        } elsif ( $2 == "2" ) {
            $dateBegin = "$1-07-01";
            $dateEnd   = "$1-12-31";
        }
    }

    return ( $dateBegin, $dateEnd );
}

sub countryCode {
    my $self    = shift;
    my $country = $self->_getByFieldName('countryCode');
    if ( $country =~ /^arab emirates$/i ) {
        $country = 'United Arab Emirates';
    } elsif ( $country =~ /^rep\. korea$/i ) {
        $country = 'Korea, Republic of';
    } elsif ( $country =~ /^Slovakia Rep\.$/i ) {
        $country = 'Slovakia';
    }

    my $o = Common::Country->Get($country);
    return ( defined $o ) ? $o->alpha2() : $country;
}

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

    if ( $config =~ /^cd-lp$/i ) {
        return RPS::File::Sale::TYPE_CD;
    } elsif ( $config =~ /^v-lp$/i ) {
        return RPS::File::Sale::TYPE_LP;
    }

    $self->fail("Unable to determine config from format($config)");
}

sub configuration {
    my $self   = shift;
    my $config = $self->_getByFieldName('configuration');

    if ( $config =~ /^cd-lp$/i ) {
        return "CD";
    } elsif ( $config =~ /^v-lp$/i ) {
        return "LP";
    }

    $self->fail("Unable to determine configuration from format($config)");
}

sub sales {
    my $self  = shift;
    my $sales = $self->_getByFieldName('payUnits');

    return ( $sales > 0 ) ? $sales : 0;
}

sub salesRevenue {
    my $self         = shift;
    my $salesRevenue = $self->_getByFieldName('totalRevenue');

    return ( $salesRevenue > 0 ) ? $salesRevenue : 0;
}

sub returns {
    my $self         = shift;
    my $sales        = $self->_getByFieldName('payUnits');
    my $salesRevenue = $self->_getByFieldName('totalRevenue');

    return ( $sales < 0 && $salesRevenue < 0 ) ? abs($sales) : 0;
}

sub returnsRevenue {
    my $self         = shift;
    my $sales        = $self->_getByFieldName('payUnits');
    my $salesRevenue = $self->_getByFieldName('totalRevenue');

    return ( $sales < 0 && $salesRevenue < 0 ) ? abs($salesRevenue) : 0;
}

sub _isValidSaleRecord {
    my $self = shift;

    # The line is valid if it has sales or returns, and has an album title
    #
    return ( ( $self->sales != 0 || $self->returns != 0 ) && $self->albumName );
}

sub physical { 1 }

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