package RPS::Import::Redeye::v11;

use strict;
use warnings;

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

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

sub _fieldMap {
    {
        countryCode  => 'O',
        dateBegin    => 'H',
        dateEnd      => 'H',
        productType  => 'A',
        artistName   => 'C',
        upc          => 'B',
        albumName    => 'D',
        totalRevenue => 'M',
    };
}

sub _unverifiedFieldMap {
    {
       _netUnits => 'E',
    };
}

sub physical          { 1 }
sub _headerIdentifier { upc => 'UPC' }
sub channel           { RPS::File::Sale::CHANNEL_RETAIL; }
sub priceLevel        { RPS::File::Sale::PLEVEL_UNKNOWN; }
sub currencyCode      { 'USD' }

sub saleDates {
    my $self = shift;

    my $dateBegin = $self->_getByFieldName('dateBegin');
    return $self->_getDates( date_begin => $dateBegin );
}

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('productType') || '';

    if ( $type =~ /^CD-/i ) {
        return RPS::File::Sale::TYPE_CD;
    } elsif ( $type =~ /^(?:LP|BOX)-/i ) {
        return RPS::File::Sale::TYPE_LP;
    } elsif ( $type =~ /^DVD-/i ) {
        return RPS::File::Sale::TYPE_DVD;
    } elsif ( $type =~ /^SI-/i ) {
        return RPS::File::Sale::TYPE_LP5;
    }

    $self->fail("Could not get product type from '$type'");
}

sub upc {
    my $self = shift;

    my $upc = $self->_getByFieldName('upc') || 0;
    return sprintf "%d", $upc;
}

sub netUnits           { $_[0]->_getByFieldName('_netUnits')    || 0 }
sub totalRevenue       { $_[0]->_getByFieldName('totalRevenue') || 0 }
sub sales              { $_[0]->netUnits     > 0 ? $_[0]->netUnits         : 0 }
sub salesRevenue       { $_[0]->totalRevenue > 0 ? $_[0]->totalRevenue     : 0 }
sub returns            { $_[0]->netUnits     < 0 ? abs $_[0]->netUnits     : 0 }
sub returnsRevenue     { $_[0]->totalRevenue < 0 ? abs $_[0]->totalRevenue : 0 }
sub _isValidSaleRecord {
    return ( $_[0]->_getByFieldName('artistName') || $_[0]->_getByFieldName('albumName') )
    && $_[0]->netUnits
}


1;