package RPS::Import::Sony::v7;

use strict;

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

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

use Data::Dumper;

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

sub _fieldMap {
    {
        labelName      => 'D',
        countryCode    => 'G',
        dateBegin      => 'N',
        dateEnd        => 'N',
        productType    => 'O',
        artistName     => 'J',
        upc            => 'M',
        albumName      => 'I',
        channel        => 'H',
        priceLevel     => 'O',
        sales          => 'Z',
        returns        => 'Z',
        salesRevenue   => 'AA',
        returnsRevenue => 'AA',
        totalRevenue   => 'AA',
    };
}

sub currencyCode { 'AUD' }
sub physical     { 1 }

sub sales {
    my $self  = shift;
    my $sales = $self->_getByFieldName('sales');
    if ( $sales < 0 ) {
        return undef;
    } else {
        return $sales;
    }
}

sub returns {
    my $self    = shift;
    my $returns = $self->_getByFieldName('returns');
    if ( $returns > 0 ) {
        return undef;
    } else {
        $returns *= -1;
        return $returns;
    }
}

sub salesRevenue {
    my $self         = shift;
    my $salesRevenue = $self->_getByFieldName('salesRevenue');
    if ( $salesRevenue < 0 ) {
        return undef;
    } else {
        return $salesRevenue;
    }
}

sub returnsRevenue {
    my $self           = shift;
    my $returnsRevenue = $self->_getByFieldName('returnsRevenue');
    if ( $returnsRevenue > 0 ) {
        return undef;
    } else {
        $returnsRevenue *= -1;
        return $returnsRevenue;
    }
}

sub countryCode {

    my $self = shift;
    my $countryName = uc( $self->_getByFieldName('countryCode') ) || return;

    if ( $countryName eq 'NEZ' ) {
        $countryName = 'NZL';
    }

    my $cObj = Common::Country->Get($countryName);
    my $countryCode = $cObj->alpha2() if ($cObj);
    if ( !$countryCode ) {
        $self->fail("Could not determine country code from $countryName");
        return undef;
    }
    return $countryCode;
}

sub _getDateBegin {

    my $self = shift;
    my $dateBegin = lc( $self->_getByFieldName('dateBegin') ) || return;

    my $month = substr( $dateBegin, 0, 2 );

    my $year = substr( $dateBegin, 2 );

    if ( $month eq '1h' ) {
        $month = '01';
    } elsif ( $month eq '2h' ) {
        $month = '07';
    } else {
        $self->fail("Could not determine date from $dateBegin");
        return undef;
    }

    $dateBegin = $year . "-" . $month . "-01";
    return $dateBegin;
}

sub _getDateEnd {

    my $self = shift;
    my $dateEnd = lc( $self->_getByFieldName('dateEnd') ) || return;

    my $month = substr( $dateEnd, 0, 2 );
    my $year = substr( $dateEnd, 2 );
    if ( $month eq '1h' ) {
        $month = '06-30';
    } elsif ( $month eq '2h' ) {
        $month = '12-31';
    } else {
        $self->fail("Could not determine date from $dateEnd");
        return undef;
    }

    $dateEnd = $year . "-" . $month;
    return $dateEnd;
}

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

    if ( $productType =~ m/\w{1} (\d{2})/ ) {
        my $productCode = $1;

        if ( $productCode == 31 ) {
            return RPS::File::Sale::TYPE_CD;

        } elsif ( $productCode == 39 ) {
            return RPS::File::Sale::TYPE_CD;

        } elsif ( $productCode == 73 ) {
            return RPS::File::Sale::TYPE_DVD;
        }
    }

    $self->fail("Could not determine product type from $productType");
    return undef;
}

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

    if ( $priceLevel =~ m/(\w{1}) \d{2}/ ) {
        my $priceCode = $1;

        if ( $priceCode eq 'b' ) {
            return RPS::File::Sale::PLEVEL_BUDGET;

        } elsif ( $priceCode eq 'm' ) {
            return RPS::File::Sale::PLEVEL_MID;

        } elsif ( $priceCode eq 'f' ) {
            return RPS::File::Sale::PLEVEL_FULL;
        }
    }

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

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

    if ( $channel eq 'ind' ) {
        return RPS::File::Sale::CHANNEL_RETAIL;

    } elsif ( $channel eq 'rtt' ) {
        return RPS::File::Sale::CHANNEL_RETAIL;

    } elsif ( $channel eq 'rcc' ) {
        return RPS::File::Sale::CHANNEL_CLUB;

    } elsif ( $channel eq 'px' ) {
        return RPS::File::Sale::CHANNEL_MILITARY;
    }

    $self->fail("Could not determine channel from $channel");
    return undef;
}

sub _isValidSaleRecord {
    my $self = shift;

    return ( defined( $self->sales || $self->returns ) && $self->albumName );
}

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