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

package RPS::Import::Wimp::v2;

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        => 'F',
        labelName        => 'D',
        dateBegin        => 'A',
        productType      => 'H',
        artistName       => 'Q',
        upc              => 'R',
        isrc             => 'S',
        trackName        => 'P',
        albumName        => 'O',
        price            => 'M',
        units            => 'I',
        currencyCode     => 'N',
        countryCode      => 'E',
    }
}

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

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');
    if( $type =~ /^bundle$/i || $type =~ /^album$/i )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    }
    elsif( $type =~ /^track$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    }    

    $self->fail( "Unable to determine product type from '$type'" );
}

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

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


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

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

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

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

    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 };
    }

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

    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) );
    }
    return( $startDate, $endDate );
}

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