package RPS::Import::Sony::v8;

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',
        albumName   => 'I',
        trackName   => 'I',
        formatType  => 'H',
        units       => 'Z',
        price       => 'AA',
        mediaType   => 'O',
    };
}

sub currencyCode { 'AUD' }

sub mediaType {
    my $self = shift;
    my $mediaType = uc( $self->_getByFieldName('mediaType') ) || return;
    if ( $mediaType =~ /B5$/ ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    } else {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
}

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} (.{2})/ ) {
        my $productCode = $1;

        if ( $productCode == 91 ) {
            return RPS::File::Sale::TYPE_TRACK;

        } elsif ( $productCode == 95 ) {
            return RPS::File::Sale::TYPE_ALBUM;

        } elsif ( $productCode eq 'b5' ) {
            return RPS::File::Sale::TYPE_TRACK;

        } elsif ( $productCode eq 'r3' ) {
            return RPS::File::Sale::TYPE_TRACK;

        } elsif ( $productCode eq 'r5' ) {
            return RPS::File::Sale::TYPE_TRACK;

        }
    }

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

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

    if ( $format eq 'cds' ) {
        return RPS::File::Sale::FORMAT_STREAM;

    } elsif ( $format eq 'bdd' ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;

    } elsif ( $format eq 'dol' ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;

    } elsif ( $format eq 'dts' ) {
        return RPS::File::Sale::FORMAT_RINGTONE;
    }

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

sub trackName {
    my $self = shift;
    return $self->productType eq RPS::File::Sale::TYPE_ALBUM ? undef : $self->SUPER::trackName;
}

sub albumName {
    my $self = shift;
    return $self->productType eq RPS::File::Sale::TYPE_TRACK ? undef : $self->SUPER::albumName;
}

sub _isValidSaleRecord {
    my $self = shift;

    return ( defined( $self->units ) && ( $self->albumName || $self->trackName ) );
}

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