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

package RPS::Import::SecretlyPhysical;

use strict;

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

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

sub _fieldMap {
    my $self      = shift;
    my %field_map = (
        2 => {
            labelName       => 'L',
            countryCode     => 'G',
            dateBegin       => 'Q',
            productType     => 'D',
            artistName      => 'E',
            clientProductID => 'N',
            albumName       => 'F',
            retailPrice     => 'J',
            sales           => 'I',
            salesRevenue    => 'O',
            totalRevenue    => 'O',
        },
        3 => {
            labelName       => 'T',
            countryCode     => 'G',
            dateBegin       => 'Q',
            productType     => 'D',
            artistName      => 'E',
            clientProductID => 'C',
            albumName       => 'F',
            retailPrice     => 'J',
            sales           => 'I',
            salesRevenue    => 'O',
            totalRevenue    => 'O',
        },
        4 => {
            labelName       => 'W',
            countryCode     => 'H',
            dateBegin       => 'T',
            productType     => 'E',
            artistName      => 'F',
            clientProductID => 'C',
            upc             => 'D',
            albumName       => 'G',
            retailPrice     => 'M',
            sales           => 'L',
            salesRevenue    => 'R',
            totalRevenue    => 'R',
        },
        5 => {
            labelName       => 'U',
            countryCode     => 'H',
            dateBegin       => 'R',
            dateEnd         => 'R',
            productType     => 'E',
            artistName      => 'F',
            clientProductID => 'C',
            upc             => 'D',
            albumName       => 'G',
            wholesalePrice  => 'K',
            sales           => 'J',
            salesRevenue    => 'P',
            totalRevenue    => 'P',
        },
    );
    return $field_map{ $self->_version };
}

sub _headerIdentifier { ( dateBegin => 'dateProcessedlast' ) }

sub currencyCode { 'USD' }

sub physical { 1 }

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

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

sub saleDates {
    my $self      = shift;
    my $startDate = $self->_getDateBegin();
    my ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $startDate );
    return ( $dateBegin, $dateEnd );
}

sub countryCode {
    my $self    = shift;
    my $country = $self->_getByFieldName('countryCode');

    my %countryMap = ( 'benelux' => 'belgium', );
    $country = $countryMap{ lc $country } if ( exists $countryMap{ lc $country } );

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

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

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

    if (   $type =~ /12"$/i
        || $type =~ /14xLP Boxset$/i
        || $type =~ /(2|3|7)xLP$/i
        || $type =~ /LP$/i
        || $type =~ /LP \(180 gram\)$/i
        || $type =~ /LP \+ CD$/i
        || $type =~ /LP\+CD$/i
        || $type =~ /LPx7$/i
        || $type =~ /\(Red Vinyl\) LP$/i
        || $type =~ /\(Splatter Vinyl\) LP$/i ) {
        return RPS::File::Sale::TYPE_LP;
    } elsif ( $type =~ /(2|2x|3x)CD$/i ) {
        return RPS::File::Sale::TYPE_DBL_CD;
    } elsif ( $type =~ /(?:5x)?CD\+DVD$/i ) {
        return RPS::File::Sale::TYPE_DVD_CD_SET;
    } elsif ( $type =~ /7"$/i ) {
        return RPS::File::Sale::TYPE_LP5;
    } elsif ( $type =~ /CD(?:EP)?$/i
        || $type =~ /USB Flash Drive/i ) {
        return RPS::File::Sale::TYPE_CD;
    } elsif ( $type =~ /CDS$/i ) {
        return RPS::File::Sale::TYPE_CD_SIN;
    } elsif ( $type =~ /DVD$/i ) {
        return RPS::File::Sale::TYPE_DVD;
    } else {
        return RPS::File::Sale::TYPE_CD;    # Default
    }
}

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

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

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

sub returnsRevenue {
    my $self         = shift;
    my $sales        = $self->_getByFieldName('sales');
    my $salesRevenue = $self->_getByFieldName('salesRevenue');
    return ( $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 );
}

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