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

package RPS::Import::WMG::WarnerAUPhysical;

use strict;

use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Date;
use Common::Date::Text;

use lib '/app/tools/rps/lib';
use RPS::Import::ServiceMap;

use base 'RPS::Import::Importer';

sub _fieldMap {
    my $self = shift;
    my $version = $self->_version;

    my %_field_map = (
        4 => {
            # header
            dateBegin        => 'B5',

            # detail
            artistName       => 'A',
            albumName        => 'B',
            clientProductID  => 'C',
            wholesalePrice   => 'Y',
            retailPrice      => 'E',
            totalRevenue     => 'X',
        },
        10 => {
            # header
            dateBegin        => 'B5',
            labelName        => 'A1',

            # detail
            artistName       => 'A',
            albumName        => 'B',
            clientProductID  => 'C',
            totalRevenue     => 'U',
            wholesalePrice   => 'F',
            retailPrice      => 'E',
        },
    );
    return $_field_map{$version};
}

sub _unverifiedFieldMap {
    my $self = shift;
    my $version = $self->_version;

    my %_field_map = (
        4 => {
            config           => 'D',
            netQuantity      => 'J',
            netSales         => 'X',
        },
        10 => {
            config           => 'D',
            netQuantity      => 'J',
            netSales         => 'U',
        },
    );
    return $_field_map{$version};
}

sub _headerIdentifier { ( artistName => 'Artist' ) }

sub countryCode { 'AU' }

sub currencyCode { 'AUD' }

sub physical { 1 }

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


my %productLevelMap = (
    '1CD BUD' => {
        product_type => RPS::File::Sale::TYPE_CD,
        price_tier   => RPS::File::Sale::PLEVEL_BUDGET,
    },
    '1CD FULL' => {
        product_type => RPS::File::Sale::TYPE_CD,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    '1CD MID' => {
        product_type => RPS::File::Sale::TYPE_CD,
        price_tier   => RPS::File::Sale::PLEVEL_MID,
    },
    '2CD' => {
        product_type => RPS::File::Sale::TYPE_DBL_CD,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    '2CD MID' => {
        product_type => RPS::File::Sale::TYPE_DBL_CD,
        price_tier   => RPS::File::Sale::PLEVEL_MID,
    },
    '2CD FULL' => {
        product_type => RPS::File::Sale::TYPE_DBL_CD,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    '3CD FULL' => {
        product_type => RPS::File::Sale::TYPE_DBL_CD,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'BPCD' => {
        product_type => RPS::File::Sale::TYPE_CD,
        price_tier   => RPS::File::Sale::PLEVEL_BUDGET,
    },
    'CD/BLU/LP' => {
        product_type => RPS::File::Sale::TYPE_CD,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'CD/DVD' => {
        product_type => RPS::File::Sale::TYPE_DVD_CD_SET,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'CD/DVD MID' => {
        product_type => RPS::File::Sale::TYPE_DVD_CD_SET,
        price_tier   => RPS::File::Sale::PLEVEL_MID,
    },
    'CD/DVD FUL' => {
        product_type => RPS::File::Sale::TYPE_DVD_CD_SET,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'CD SETS' => {
        product_type => RPS::File::Sale::TYPE_CD,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'CDSET BUD' => {
        product_type => RPS::File::Sale::TYPE_CD,
        price_tier   => RPS::File::Sale::PLEVEL_BUDGET,
    },
    'DVD/CD' => {
        product_type => RPS::File::Sale::TYPE_DVD_CD_SET,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'DVDWM FULL' => {
        product_type => RPS::File::Sale::TYPE_DVD,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'EPCD (A)' => {
        product_type => RPS::File::Sale::TYPE_EP,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'EPCD (S)' => {
        product_type => RPS::File::Sale::TYPE_EP,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'FPCD' => {
        product_type => RPS::File::Sale::TYPE_CD,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'FPLP' => {
        product_type => RPS::File::Sale::TYPE_LP,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'LFCD' => {
        product_type => RPS::File::Sale::TYPE_CD,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'LPSETS' => {
        product_type => RPS::File::Sale::TYPE_LP,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'MPCD' => {
        product_type => RPS::File::Sale::TYPE_CD,
        price_tier   => RPS::File::Sale::PLEVEL_MID,
    },
    'MUSDVDWM' => {
        product_type => RPS::File::Sale::TYPE_DVD,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    'VINYL/DVD' => {
        product_type => RPS::File::Sale::TYPE_DVD,
        price_tier   => RPS::File::Sale::PLEVEL_FULL,
    },
    '12SING' => {
        product_type => RPS::File::Sale::TYPE_LP5,
        price_tier   => RPS::File::Sale::PLEVEL_UNKNOWN,
    },
    '2LP' => {
        product_type => RPS::File::Sale::TYPE_LP,
        price_tier   => RPS::File::Sale::PLEVEL_UNKNOWN,
    },
    'CDSING' => {
        product_type => RPS::File::Sale::TYPE_CD_SIN,
        price_tier   => RPS::File::Sale::PLEVEL_UNKNOWN,
    },
    'LPSET' => {
        product_type => RPS::File::Sale::TYPE_LP,
        price_tier   => RPS::File::Sale::PLEVEL_UNKNOWN,
    },
);

sub _processLine {
    my $self = shift;

    my $config = $self->_getByFieldName('config');

    $self->{_productType} = '';
    $self->{_priceTier}   = '';

    $self->{_productType} = $productLevelMap{uc $config}{product_type};
    $self->{_priceTier}   = $productLevelMap{uc $config}{price_tier};

    return $self->SUPER::_processLine(@_);
}

sub labelName {
    my $self = shift;
    if( exists $self->_fieldMap->{'labelName'} )
    {
        my $name = $self->_getByFieldName('labelName');

        # Strip the decorator
        #
        $name =~ s/ pty limited//i;
        return $name;
    }
}

sub priceLevel {
    return shift->{_priceTier};
}

sub productType {
    return shift->{_productType};
}

sub months {
    my $self = shift;
    my $m = lc($self->{_month});

    my %month_map = (
            jan => 1,
            feb => 2,
            mar => 3,
            apr => 4,
            may => 5,
            jun => 6,
            jul => 7,
            aug => 8,
            sep => 9,
            oct => 10,
            nov => 11,
            dec => 12,
    );
    return $month_map{$m};
}

sub saleDates {
    my $self  = shift;
    my $version = $self->_version;

    if( !$self->{_dateBegin} && !$self->{_dateEnd} )
    {
        my $dt  = $self->_getByFieldName('dateBegin');

        # Version 10 should be imported with quarterly dates
        if( $version == 10 ) {

            # get the start of the quarter and prune off the month and date
            $dt =~ m/^([a-z][a-z][a-z])\-(\d{4}) to/i;
            $self->{_month} = $1;
            $self->{_year} = $2;
            
            # convert text month to numerical
            my $tmp = $self->months();
            $self->{_month} = $tmp;

            ( $self->{_dateBegin}, $self->{_dateEnd} ) = 
                $self->_getDates( date_begin => $self->quarterBegin(),
                                  date_end => $self->quarterEnd());

        # version 4 uses the standard monthly date
        } else {
            ( $self->{_dateBegin}, $self->{_dateEnd} ) = 
                $self->_getDates( date_begin => $dt );
        }
    }
    return( $self->{_dateBegin}, $self->{_dateEnd} );
}

sub quarterBegin {
    my $self = shift;
    my ($quarter, $month);

    $quarter = int($self->{_month} / 3) + 1;
    $month   = $quarter * 3 - 2;

    return sprintf( "%04d-%02d-%02d", $self->{_year}, $month, 1 );
}

sub quarterEnd {
    my $self = shift;
    my ($quarter, $month, $days); 

    $quarter = int($self->{_month} / 3) + 1;
    $month   = $quarter * 3;
    $days    = Date::Simple::days_in_month( $self->{_year}, $month );

    return sprintf( "%04d-%02d-%02d", $self->{_year}, $month, $days );
}


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

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

sub returns {
    my $self = shift;
    my $sales   = $self->_getByFieldName('netQuantity');
    my $revenue  = $self->_getByFieldName('netSales');
    return ($revenue < 0 ) ? abs($sales) : 0; # Follow the sign of the revenue
}

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

sub _processSheet {
    my $self     = shift;
    my $sheet    = shift;
    my $sheetnum = shift;

    return $self->SUPER::_processSheet( $sheet, $sheetnum ) if( $self->{sheetname} =~ /^physical/i );
}

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 );
}

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