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

package RPS::Import::XboxMusic::Dto::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';



my %fieldMap =
(
	2 => {
        albumName        => 'E', # Parent Title
        artistName       => 'C', # Artist
        countryCode      => 'K', # Territory Code
        currencyCode     => 'L', # Currency Code
        formatType       => 'T', # Usage Type
        isrc             => 'D', # Track ISRC
        labelName        => 'H', # Licensor Name
        price            => 'V', # Total Retail Price
        productType      => 'M', # Product Type
        serviceID        => 'J', # Retailer Name
        trackName        => 'B', # Product Title
        units            => 'S', # Number of Transactions
        upc              => 'G', # Album Upc
    },
	4 => {
        albumName        => 'F', # Parent Title
        artistName       => 'C', # Artist
        countryCode      => 'M', # Territory Code
        currencyCode     => 'S', # Payment Currency
        formatType       => 'V', # Usage Type
        isrc             => 'D', # Track ISRC
        labelName        => 'J', # Label Name
        price            => 'W', # Total Wholsale Price
        productType      => 'O', # Product Type
        serviceID        => 'L', # Retailer Name
        trackName        => 'B', # Product Title
        units            => 'U', # Number of Transactions
        upc              => 'H', # Album Upc
    },    
);

sub _fieldMap {
    my $self = shift;
    return $fieldMap{$self->_version()};
}

sub _headerIdentifier { ( productType => 'Product Type' ) }

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

    if( $type =~ /^(single|album) full-track$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    else
    {
        $self->fail( "Could not determine media type from '$type'" );
    }
}

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

    if( $type =~ m/^single full-track$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    } 
    elsif( $type =~ m/^album full-track$/i )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    } 
    else
    {
        $self->fail( "Could not parse product type from '$type'" );
    }
}

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

    my $format;
    if( $formatType =~ m/^(dto|redeemed)$/i )
    {
        $format = RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    else
    {
        $self->fail( "Could not parse format type from '$formatType'" );
    }
    return $format;
}

sub albumName {
    my $self = shift;

    my $productTitle1 = $self->_getByFieldName( 'albumName' ); # if product type is track
    my $productTitle2 = $self->_getByFieldName( 'trackName' ); # if product type is album

    return ( $self->productType eq RPS::File::Sale::TYPE_ALBUM ) ?  $productTitle2 : $productTitle1;
}

sub trackName {
    my $self = shift;

    my $trackTitle = $self->_getByFieldName( 'trackName' );

    return ( $self->productType eq RPS::File::Sale::TYPE_TRACK ) ?  $trackTitle : "";
}

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

    my $serviceID;

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

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

    return $serviceID;
}

# saleDates - return the start/end date information for the file.
#
sub saleDates {
    my $self = shift;

    return ($self->{_startDate}, $self->{_endDate}) if( $self->{_startDate} );

    $self->fail( "Unknown filename" ) unless( $self->filename );

    unless( $self->filename =~ /(\d{4})(\d{2})/ ) {
        $self->fail( "Could not determine period from filename '" . $self->filename . "'" );
    }

    my $startDate = sprintf( "%04d-%02d-%02d", $1, $2, 1 );

    ($self->{_startDate}, $self->{_endDate}) = $self->_getDates( date_begin => $startDate );

    return ($self->{_startDate}, $self->{_endDate});
}

sub price {
    my $self = shift;
    my $price = $self->_getByFieldName( 'price' );
    
    $price =~ s/,/\./;
    
    return $price;
}

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