package RPS::Import::EMI::Physical;

use strict;

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

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

use Data::Dumper;

sub _processHeader {
    my $self = shift;

    return if ( $self->{_dateBegin} );

    my $label = $self->_getByFieldName('dateLabel') || return;
    my $value = $self->_getByFieldName('dateValue') || return;

    if ( $label =~ /month/i && $value ) {
        $self->{_month} = $value;
    } elsif ( $label =~ /year/i && $value ) {
        $self->{_year} = $value;
    }

    if ( $self->{_month} && $self->{_year} ) {
        my $d = new Common::Date("$self->{_month} 1, $self->{_year}");
        $self->{_dateBegin} = $self->{_dateEnd} = $d->asString;
    }

    die if ( $self->_isHeader() );
}

sub _preProcess {

    my $self = shift;

    my %args     = @_;
    my $fileName = $args{file}->OrigFileName();

    my $currencyCode;
    my $countryCode;

    # If the filename contains "_CA" or "_CANADA", followed by spaces or an underscore or period,
    # then assume it's Canadian.
    # If the filename contains "_US" or "_USA", followed by spaces or an underscore or period,
    # then assume it's US.
    #
    if ( $fileName =~ /_ca(nada)?(\s+|_|\.)/i ) {
        $currencyCode = "CAD";
        $countryCode  = "CA";
    } elsif ( $fileName =~ /_us(a)?(\s+|_|\.)/i ) {
        $currencyCode = "USD";
        $countryCode  = "US";
    } else {
        return undef;
    }

    # Get date information from the filename if it's there (FB279)
    #
    if ( $fileName =~ /(\d{4})-(\d{1,2})/i )    # YYYY-M or YYYY-MM
    {
        $self->{_dateBegin} = sprintf( "%4d-%02d-01", $1, $2, 1 );
    }

    $self->{_currencyCode} = $currencyCode;
    $self->{_countryCode}  = $countryCode;
}

sub _getDateBegin { shift->{_dateBegin} }
sub _getDateEnd   { shift->{_dateEnd} }

sub physical   { 1 }
sub channel    { RPS::File::Sale::CHANNEL_RETAIL }
sub priceLevel { RPS::File::Sale::PLEVEL_FULL }

sub countryCode {
    my $self = shift;
    $self->fail("Filename doesn't contain CA or US") if ( !$self->{_countryCode} );
    return $self->{_countryCode};
}

sub currencyCode {
    my $self = shift;
    $self->fail("Filename doesn't contain CA or US") if ( !$self->{_currencyCode} );
    return $self->{_currencyCode};
}

sub upc {
    my $upc = shift->SUPER::upc() || return;
    $upc =~ /(\d+)/;
    return $1;
}

sub albumName {
    my $name = shift->SUPER::albumName() || return;
    my ( $artist, $album ) = split( /\//, $name );
    return $album;
}

sub artistName {
    my $name = shift->SUPER::albumName() || return;
    my ( $artist, $album ) = split( /\//, $name );
    return $artist;
}

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

    if ( $type =~ /^p/i ) {
        return RPS::File::Sale::TYPE_CD;
    }

    die "Unknown product type '$type'";
}

sub returns {
    my $returns = shift->SUPER::returns();
    return $returns && $returns < 0 ? $returns * -1 : $returns;
}

sub returnsRevenue {
    my $self    = shift;
    my $revenue = $self->SUPER::returnsRevenue;

    return $revenue ? $revenue * -1 : 0;
}

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