package RPS::Import::X5::v1;

use strict;

use lib '/app/tools/common/lib';

use Common::Country;

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

use Data::Dumper;

sub _headerIdentifier { ( upc => 'UPC' ) }

sub _fieldMap {
    {
        dateBegin    => 'S', 
        dateEnd      => 'T',                      
        serviceID    => 'F',
        countryCode  => 'G',
        formatType   => 'R',
        artistName   => 'A',
        upc          => 'C',
        albumName    => 'B',
        isrc         => 'E',
        trackName    => 'D',
        units        => 'Q',
        currencyCode => 'K',
        price        => 'N',
    }
}

sub productType { RPS::File::Sale::TYPE_TRACK }


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

    if( $format =~ /^d$/i )
    {
        $format = RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif( $format =~ /^s$/i )
    {
        $format = RPS::File::Sale::FORMAT_STREAM
    }
    else
    {
        $self->fail( "Unknown format type '$format'" );
    }
    return $format;
}

sub mediaType { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub countryCode
{
    my $self = shift;

    if (defined $self->_getByFieldName( 'countryCode' ))
    {
        my $name = $self->_getByFieldName( 'countryCode' );
        $name = 'FM' if( $name =~ /^micronesia$/i );
        $name = 'KR' if( $name =~ /^korea \(south\)$/i );
        $name = 'VI' if( $name =~ /^United States Virgin Islands$/i );

        my $country = Common::Country->Get( $name );
        if ($country)
        {
            return $country->alpha2();
        }
        else
        {
            return $name;
        }
    }
}

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 _processSheet {
    my $self     = shift;
    my $sheet    = shift;
    my $sheetnum = shift;

    if( $self->{sheetname} =~ /Statistics from X-Act/i )
    {
        return $self->SUPER::_processSheet( $sheet, $sheetnum );
    }
}

# Method _processLine is overloaded so we can detect and buffer any album/track
# data.  This will be used for the current and all subsequent lines until we detect
# new album/track data or hit the end of file.
#
# See Import::Importer under sale_import/lib.
#
sub _processLine
{
    my $self = shift;

    my $artistName = $self->line->[$self->_getOffset( 'artistName' )];
    my $albumName  = $self->line->[$self->_getOffset( 'albumName' )];
    my $upc        = $self->line->[$self->_getOffset( 'upc' )];
    my $trackName  = $self->line->[$self->_getOffset( 'trackName' )];
    my $isrc       = $self->line->[$self->_getOffset( 'isrc' )];

    if( $artistName && $albumName && $upc && $trackName && $isrc )
    {
        $self->{_artistName} = $artistName;
        $self->{_albumName}  = $albumName;
        $self->{_upc}        = $upc;
        $self->{_trackName}  = $trackName;

        $isrc =~ s/-//g;
        $self->{_isrc}       = $isrc;

    }

    return $self->SUPER::_processLine(@_);
}

sub artistName { shift->{_artistName} }
sub albumName  { shift->{_albumName} }
sub upc        { shift->{_upc} }
sub trackName  { shift->{_trackName} }
sub isrc       { shift->{_isrc} }

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