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

package RPS::Import::Sony::v17;

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::Sony::v11';

sub _fieldMap {
    {
        # header
        #
        labelName        => 'A6',
        dateBegin        => 'E3',
        dateEnd          => 'G3',

        # detail
        #
        countryCode      => 'I',
        configuration    => 'D',
        artistName       => 'C',
        serviceProductID => 'A',
        upc              => 'F',
        albumName        => 'B',
        priceLevel       => 'K',
        currencyCode     => 'R',
        wholesalePrice   => 'L',
        sales            => 'M',
        salesRevenue     => 'R',
        returns          => 'M',
        returnsRevenue   => 'R',
        totalRevenue     => 'R',
    }
}

sub _unverifiedFieldMap {
    {
        altDateBegin     => 'K8',
        altDateEnd       => 'L8',
    }
}

sub _headerIdentifier {
    ( 'upc' => 'Barcode (UPC)' )
}


sub _isValidSaleRecord {
	my $self = shift;

	return ( ($self->returns != 0 || $self->sales != 0) && ($self->trackName || $self->albumName) );
}

sub channel      { RPS::File::Sale::CHANNEL_RETAIL }
sub physical     { 1 }
sub currencyCode { 'AUD' } # this code is a custom cell format in the spreadsheet, so we'll hard code
                           # it here


# Normally these are defined in RPS::Import::Importer, but our parent class (v11)
# overrides those in a way that we don't want...
#
sub albumName  { shift->RPS::Import::Importer::albumName(); }
sub upc        { shift->RPS::Import::Importer::upc(); }
sub isrc       { shift->RPS::Import::Importer::isrc(); }
sub mediaType  { shift->RPS::Import::Importer::mediaType(); }
sub formatType { shift->RPS::Import::Importer::formatType(); }


# _processSheet is used to process a tab from the spreadsheet.
# Here, we're extracting the sale date and label information
# from the cells called out in the template.
#
sub _processSheet {
    my $self     = shift;
    my $sheet    = shift;
    my $sheetnum = shift;

    if( $self->{sheetname} !~ /^ISRC-Barcode Data$/i && $self->{sheetname} !~ /^Label Copy$/i)
    {
        # Get the date and label information from the header
        #
        foreach my $line(@$sheet)
        {
            $self->{line} = $line;


            my $labelName = $self->_getByFieldName('labelName');
            $self->{_labelName} = $labelName;

            my $startDate = $self->_getByFieldName('dateBegin');
            my $endDate   = $self->_getByFieldName('dateEnd');

            if( !$startDate && !$endDate )
            {
                $startDate = $self->_getByFieldName('altDateBegin');
                $endDate   = $self->_getByFieldName('altDateEnd');
            }
            my($dateBegin, $dateEnd) = $self->_getDates( date_begin => $startDate, date_end => $endDate );

            $self->{_startDate} = $dateBegin;
            $self->{_endDate}   = $dateEnd;


            last;
        }

        # Now process the detail lines
        #
        return $self->RPS::Import::Importer::_processSheet( $sheet, $sheetnum );
    }
}


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

    if( $config =~ /^cd longplay$/i || $config =~ /^enhanced cd longplay$/i )
    {
        return RPS::File::Sale::TYPE_CD;
    }
    elsif( $config =~ /^vinyl longplay 33 1\/3$/i )
    {
        return RPS::File::Sale::TYPE_LP;
    }
    elsif( $config =~ /^dvd video longplay$/i )
    {
        return RPS::File::Sale::TYPE_DVD;
    }

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


sub countryCode {
    my $self = shift;
    my $country = $self->RPS::Import::Importer::countryCode();

    $self->fail( "No country specified" )
        unless( $country && length($country) );

    my $countryObj = new Common::Country( search => $country );
    return $countryObj->alpha2() if( $countryObj );

    $self->fail( "Unable to determine country code from '$country'" );
}


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


    if( lc($priceLevel) eq 'budget' ) {
        return RPS::File::Sale::PLEVEL_BUDGET;
    
    } elsif( lc($priceLevel) eq 'mid' ) {
        return RPS::File::Sale::PLEVEL_MID;
    
    } elsif( lc($priceLevel) eq 'full' ) {
        return RPS::File::Sale::PLEVEL_FULL;
    }

    $self->fail( "Could not determine price tier from $priceLevel" );
}


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


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

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


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


sub returnsRevenue {
    my $self = shift;
    my $revenue = $self->_getByFieldName( 'returnsRevenue' );

    return ( $revenue < 0 ) ? abs($revenue) : 0;
}


sub units {
    my $self = shift;
    my $returns = $self->_getByFieldName( 'returns' );

    return $returns;
}


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