package RPS::Import::AbsoluteDistribution::v1;

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::UTF8;

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

sub _fieldMap {
    {
        labelName        => 'B',
        serviceID        => 'H',
        dateBegin        => 'G',
        productType      => 'I',
        formatType       => 'O',
        serviceProductID => 'M',
        upc              => 'L',
        isrc             => 'K',
        trackName        => 'E',
        albumName        => 'F',
        price            => 'W',
        units            => 'T',
        countryCode      => 'J',
    }
}

sub _unverifiedFieldMap {
    {
        trackArtist => 'C',
        albumArtist => 'D',
    }
}

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

sub currencyCode { 'GBP' }

sub serviceID {
    my $self = shift;
    my $service = $self->_getByFieldName('serviceID');

    $self->fail( "Service name not defined" ) unless( $service );

    my $serviceID = $self->getServiceID(lc ($service)) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };

    $self->fail( "Unable to determine service id from '$service'" ) unless( $serviceID );

    return $serviceID;
}

sub artistName {
    my $self = shift;
    if( $self->productType eq RPS::File::Sale::TYPE_TRACK )
    {
        return $self->_getByFieldName('trackArtist');
    }
    elsif( $self->productType eq RPS::File::Sale::TYPE_ALBUM )
    {
        return $self->_getByFieldName('albumArtist');
    }
}

sub trackName {
    my $self = shift;
    return ( $self->productType eq RPS::File::Sale::TYPE_TRACK ) ? $self->_getByFieldName('trackName') : undef;
}

sub isrc {
    my $self = shift;
    return ( $self->productType eq RPS::File::Sale::TYPE_TRACK ) ? $self->_getByFieldName('isrc') : undef;
}

sub countryCode {
    my $self = shift;
    my $cname = $self->_getByFieldName('countryCode');
    if( $cname )
    {
        my $o = Common::Country->Get( $cname );
        return (defined $o) ? $o->alpha2() : $cname;
    }

    # Country not specified; return US if this is YouTube
    #
    my $service = $self->_getByFieldName('serviceID');
    return 'US' if( $service =~ /youtube/i );

    $self->fail("Unknown country '$cname'");
}

sub saleDates {
    my $self = shift;
    my $dt = $self->_getDateBegin();
    my($dateBegin, $dateEnd) = $self->_getDates( date_begin => $dt );
}

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

    if( $type =~ /^Album.Plus \(DRM-free\)$/i ||
        $type =~ /^Bundle$/i )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    } 
    elsif( $type =~ /^Song.Match Download.Plus \(DRM-free\)$/i ||
           $type =~ /^Song.Plus \(DRM-free\)$/i ||
           $type =~ /^track$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    } 
    else
    {
        $self->fail("Could not parse product type from '$type'");
    }
}

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

    if( $type =~ /^(audio|itunes) match$/i || $type =~ /^SFTP Upload$/i || $type =~ /^stream$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif( $type =~ /^download$/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    $self->fail("Could not parse product type from '$type'");
}

sub mediaType {
    my $self = shift;
    my $type = $self->_getByFieldName('formatType');

    if( $type =~ /^(audio|itunes) match$/i || $type =~ /^download$/i || $type =~ /^stream$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    elsif( $type =~ /^SFTP Upload$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }
    $self->fail("Could not media product type from '$type'");
}

sub _isValidSaleRecord {
    my $self = shift;
    my $labelName = lc $self->_getByFieldName('labelName');
    my $trackArtist = lc $self->_getByFieldName('trackArtist');

    # Skip the line with the x's
    #
    return undef if( $labelName eq 'x' and $trackArtist eq 'x' );

    return $self->SUPER::_isValidSaleRecord();
}

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