#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2013 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::Import::Wimp::v1;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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 {
    {
        serviceID        => 'T',
        dateBegin        => 'A',
        artistName       => 'P',
        upc              => 'I',
        isrc             => 'N',
        trackName        => 'K',
        albumName        => 'H',
        price            => 'R',
        units            => 'G',
        currencyCode     => 'S',
        countryCode      => 'U',
    }
}

sub _unverifiedFieldMap {
    {
        countryHeader   => 'U2'
    }
}

sub countryCode {
    my $self = shift;
    my $name = $self->_getByFieldName('countryCode');
    if( $name )
    {
        my $o = Common::Country->Get( $name );
        if( $o )
        {
            return $o->alpha2;
        }
        $self->fail("Unrecognized country '$name'");
    }
    return $self->{_countryCode}; # See _preProcess
}


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

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

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

sub formatType  { RPS::File::Sale::FORMAT_STREAM }

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

    my $serviceID;

    if( $service =~ /^wimp$/i )
    {
        $serviceID = Client::Service::DSP_WIMP_ASPIRO;
    }
    else
    {
        $serviceID = $self->getServiceID(lc ($service)) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };
    }

    return $serviceID;
}

sub saleDates {
    my $self = shift;

    my $dt = $self->_getByFieldName('dateBegin');
    my $startDate;
    my $endDate;

    if( $dt =~ /^(\d{4})-(\d{1,2})-(\d{1,2})$/ ) # YYYY-MM-DD
    {
        my $year  = $1;
        my $month = $2;
        my $day   = $3;
        $startDate = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        $endDate   = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month) );
    }
    elsif( $dt =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/ ) # MM/DD/YYYY
    {
        my $month = $1;
        my $day   = $2;
        my $year  = $3;
        $startDate = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        $endDate   = sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month) );
    }
    return( $startDate, $endDate );
}

sub _preProcess {
    my $self = shift;

    my %args = @_;

    $self->SUPER::_preProcess(%args);

    foreach my $line( @{ $args{lines} } )
    {
        $self->{line} = $line;  # needs to be set in order to use _getByFieldName
        my $cheader = $self->_getByFieldName( 'countryHeader' );
        return if( $cheader =~ /country/i );
    }

    if( $self->filename =~ /^\d{4}-\d{1,2}-\d{1,2}_(\w{2})_/i )
    {
        $self->{_countryCode} = $1;
    }

}

sub _isValidSaleRecord {
    my $self = shift;
    my $serviceID = $self->serviceID();

    # Skip incomplete lines (lines w/o serviceID) (FB4205)
    #
    return undef if( !$serviceID );

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

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