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

package RPS::Import::CreateSpace::v6;

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 {
    my $self = shift;
    my $version = $self->_version;

    my %field_map = (
        6 => {
            dateBegin       => 'A',
            albumName       => 'B',
            trackName       => 'C',
            type            => 'D',
            serviceID       => 'E',
            upc             => 'F',
            isrc            => 'H',
            clientProductID => 'I',
            units           => 'M',
            price           => 'N',
        },
        7 => {
            dateBegin       => 'A',
            albumName       => 'B',
            trackName       => 'C',
            type            => 'D',
            serviceID       => 'E',
            upc             => 'F',
            isrc            => 'H',
            clientProductID => 'I',
            units           => 'L',
            price           => 'M',
        },
    );
    return $field_map{$version};
}

sub _productTypes {
    {
        'mp3 track' => {
                productType => RPS::File::Sale::TYPE_TRACK,
                formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
                mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
            },
        'mp3 album' => {
                productType => RPS::File::Sale::TYPE_ALBUM,
                formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
                mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
            },
        'video album' => {
                productType => RPS::File::Sale::TYPE_ALBUM,
                formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
                mediaType   => RPS::DB::Item::MediaType::kMediaTypeVideo,
            },
        'video download purchase' => {
                productType => RPS::File::Sale::TYPE_ALBUM,
                formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
                mediaType   => RPS::DB::Item::MediaType::kMediaTypeVideo,
            },
    }
}

sub currencyCode { 'USD' }

sub countryCode { 'US' }

sub _headerIdentifier { ( albumName => 'Title Name' ) }

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

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

    if ( $service )
    {
        $service =~ s/MP3 Track//;
        $service =~ s/MP3 Album//;

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

    $self->fail( "Unknown service '$service'" ) unless( $serviceID );

    return $serviceID;
}

sub saleDates {
    my $self = shift;
    my $_date = $self->_getByFieldName('dateBegin');

    my $dateBegin;
    my $dateEnd;

    if( $_date =~ /(\d{5})\./ )
    {
        ($dateBegin, $dateEnd) = $self->_getDates( date_begin => $_date, type => 'msft' );
    }
    else
    {
        ($dateBegin, $dateEnd) = $self->_getDates( date_begin => $_date );
    }
    
    return( $dateBegin, $dateEnd );
}

sub _formatPrice {
    my $self = shift;
    my $price = shift || return;
    return $price;
}

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