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

package RPS::Import::EssentialDistribution::v1;

use strict;
use utf8;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Log;

use lib '/app/tools/rps/lib';
use RPS::File::Sale;

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

sub _fieldMap {
    {
        albumName    => 'G',
        artistName   => 'F',
        countryCode  => 'B',
        dateBegin    => 'C',
        labelName    => 'E',
        productType  => 'J',
        sales        => 'K',
        salesRevenue => 'N',
        upc          => 'I',
    };
}

sub _headerIdentifier { ( productType => 'Format' ) }

sub priceLevel { RPS::File::Sale::PLEVEL_UNKNOWN; }

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

sub currencyCode { 'GBP' }

sub countryCode {
    my $self       = shift;
    my $ccode      = uc $self->_getByFieldName('countryCode');
    my %countryMap = (
        'INLAND'               => 'DE',
        'INLAND FREMD'         => 'DE',
        'AUSLAND'              => 'NL',
        'NATRAB: INLAND'       => 'DE',
        'PROMO: INLAND'        => 'DE',
        'RÜCK LABEL: INLAND'  => 'DE',
        'RÜCK LABEL: SCHROTT' => 'DE',
    );
    return ( exists $countryMap{$ccode} ) ? $countryMap{$ccode} : $ccode;
}

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin');

    # the date is being returned in the "days since 1/1/1900" format, so we need
    # to convert it.  We'll check if it's a 5 digit number and greater than the
    # number of days since 1/1/1900 on 1/1/2006, which is 38716
    if ( $date =~ /^\d{5}$/ and $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }

    return $date

}

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

    my $productType = "";

    if ( $type =~ /^(cd|2cd)$/i ) {
        $productType = RPS::File::Sale::TYPE_CD;
    } elsif ( $type =~ /^(cd2)$/i ) {
        $productType = RPS::File::Sale::TYPE_DBL_CD;
    } elsif ( $type =~ /^dvd$/i ) {
        $productType = RPS::File::Sale::TYPE_DVD;
    } elsif ( $type =~ /^(lp|vinyl)$/i ) {
        $productType = RPS::File::Sale::TYPE_LP;
    } elsif ( $type =~ /^cd.*\+.*dvd$/i ) {
        $productType = RPS::File::Sale::TYPE_DVD_CD_SET;
    } elsif ( $type =~ /^(?:7 inch|10'')$/i ) {
        $productType = RPS::File::Sale::TYPE_LP5;
    } else {
        $self->fail("Could not parse product type from '$type'");
    }
    return $productType;
}

sub albumName {
    my $self      = shift;
    my $albumName = $self->_getByFieldName('albumName');
    return $albumName;
}

sub sales {
    my $self  = shift;
    my $units = $self->_getByFieldName('sales');
    return ( $units > 0 ) ? $units : 0;
}

sub salesRevenue {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('salesRevenue');
    return ( $revenue > 0 ) ? $revenue : 0;
}

sub returns {
    my $self  = shift;
    my $units = $self->_getByFieldName('sales');
    return ( $units < 0 ) ? ( $units * -1 ) : 0;
}

sub returnsRevenue {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('salesRevenue');
    return ( $revenue < 0 ) ? ( $revenue * -1 ) : 0;
}

sub totalRevenue {
    my $self = shift;
    return $self->salesRevenue - $self->returnsRevenue;
}

sub _isValidSaleRecord {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');

    # Skip book and merchandise lines
    #
    return if ( $type =~ /^(book|merchandise)$/i );

    # This is a physical sale, so if we have revenue and a track
    # or album name then record is good
    #
    return ( defined( $self->totalRevenue ) && ( $self->trackName || $self->albumName ) );

    return $self->SUPER::_isValidSaleRecord;
}

sub _preProcess {
    my ( $self, %args ) = @_;
    $self->SUPER::_preProcess(%args);
    my $fileObj = $args{file};
    $fileObj->Physical(1);
}

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