package RPS::Import::Sony::v6;

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 _headerIdentifier { ( upc => 'UPC' ) }

sub _fieldMap {
    {
        # Header
        dateBegin => 'A',

        # Data
        type       => 'J',
        artistName => 'C',
        upc        => 'H',
        albumName  => 'E',
        isrc       => 'G',
        trackName  => 'D',
        units      => 'L',
        price      => 'M',
    };
}

sub _unverifiedFieldMap {
    { format => 'K', };
}

sub countryCode  { 'CA' }
sub currencyCode { 'CAD' }

sub _getDateBegin { shift->{_dateBegin} }

sub _processHeader {
    my $self = shift;
    my $date = $self->SUPER::_getDateBegin() || return;

    $self->{_dateBegin} = $date if ( $date =~ /^\w{3,4} \d+$/ );
}

sub _productTypes {
    {
        'audio album' => {
            formatType  => RPS::File::Sale::FORMAT_RINGTONE,
            mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
            productType => RPS::File::Sale::TYPE_ALBUM,
        },
        'audio track' => {
            formatType  => RPS::File::Sale::FORMAT_RINGTONE,
            mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
            productType => RPS::File::Sale::TYPE_TRACK,
        },
        'video track' => {
            formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
            mediaType   => RPS::DB::Item::MediaType::kMediaTypeVideo,
            productType => RPS::File::Sale::TYPE_TRACK,
        },
        'digital video ep' => {
            formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
            mediaType   => RPS::DB::Item::MediaType::kMediaTypeVideo,
            productType => RPS::File::Sale::TYPE_ALBUM,
        },
        'ringback tone' => {
            formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
            mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
            productType => RPS::File::Sale::TYPE_TRACK,
        },
        'mastertone' => {
            formatType  => RPS::File::Sale::FORMAT_VPD,
            mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
            productType => RPS::File::Sale::TYPE_TRACK,
        },
    };
}

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

    if ( $type eq 'ringback tone' ) {
        return RPS::File::Sale::FORMAT_RINGTONE,

    } elsif ( $type eq 'mastertone' ) {
        return RPS::File::Sale::FORMAT_RINGTONE,

    } elsif ( $format eq 'mobile permanent download' ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD,

    } elsif ( $format eq 'mobile tethered (non-ad supported)' ) {
        return RPS::File::Sale::FORMAT_TETHERED,

    } elsif ( $format eq 'multi-platform permanent download' ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD,

    } elsif ( $format eq 'multi-platform tethered (non-ad supported)' ) {
        return RPS::File::Sale::FORMAT_TETHERED,

    } elsif ( $format eq 'online permanent download' ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD,

    } elsif ( $format eq 'online stream (ad supported)' ) {
        return RPS::File::Sale::FORMAT_STREAM,

    } elsif ( $format eq 'online stream (non-ad supported)' ) {
        return RPS::File::Sale::FORMAT_STREAM,

    } elsif ( $format eq 'multi-platform stream (non-ad supported)' ) {
        return RPS::File::Sale::FORMAT_STREAM,

    } elsif ( $format eq 'online tethered (non-ad supported)' ) {
        return RPS::File::Sale::FORMAT_TETHERED,;
    }

    $self->fail("Could not determine format type from format: '$format' type: '$type'");
}

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->SUPER::albumName ? $self->SUPER::albumName : $self->SUPER::trackName;
}

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