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

package RPS::Import::VirginMega::v4;

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::UTF8;

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

sub _fieldMap {
    {
        labelName     => 'P',
        countryCode   => 'A',
        dateBegin     => 'B',
        productType   => 'H',
        formatType    => 'D',
        artistName    => 'O',
        upc           => 'K',
        albumName     => 'L',
        isrc          => 'Q',
        trackName     => 'N',
        units         => 'W',
        channel       => 'C',
        free          => 'E',
        price         => 'X',
	}
}

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

	if   ( lc($free) eq 'y' ) {
		return 1;

	} elsif( lc($free) eq 'n' ) {
		return 0;

	} else {
		$self->fail( "Unknown value in free column '$free'" )
	}

	return undef;
}


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

	if( $type eq 'track' ) {
		return RPS::File::Sale::TYPE_TRACK;
	} elsif( $type eq 'album' ) {
		return RPS::File::Sale::TYPE_ALBUM;
	}
}

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

	if( lc($type) eq 'wma' ) {
		return RPS::File::Sale::FORMAT_DOWNLOAD;
	}
}

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

sub currencyCode { 'EUR' }

sub _headerIdentifier { ( countryCode => 'Country' ) }

sub saleDates {
	my $self = shift;

	my $date = $self->_getByFieldName( 'dateBegin' ) || return;

    my %months = (
		jan    => 1,
        fev    => 2,
        mar    => 3,
        avr    => 4,
        mai    => 5,
        juin   => 6,
        juil   => 7,
        aout   => 8,
        sept   => 9,
        oct    => 10,
        nov    => 11,
        dec    => 12,

        # Note: Some of these had french characters in them which caused
		# perl to puke.  So I just stripped then out. case #10795
		janv   => 1,
        fvr    => 2,
        mars   => 3,
        avr    => 4,
        mai    => 5,
        juin   => 6,
        juli   => 7,
        aot    => 8,
        sept   => 9,
        oct    => 10,
        nov    => 11,
        dc     => 12,
	);

	unless( $date =~ /^(.+?)[\.-]+(\d+)$/ ) {
		$self->fail( "Could not parse date from '$date'" );
	}

    my $month;

	# Strip out any UTF8 chars for the month indexing
	if( $1 ) {
		my $monthString = Common::UTF8::ToAscii( $1 );
		$monthString =~ s/ //g;
	    $month = $months{$monthString};
	}

	my $year  = $2;

	unless( $month && $year ) {
		$self->fail( "Could not parse date from '$date'" );
	}

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

    return $self->_getDates( date_begin => $startDate );
}

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

	$price =~ s/,/./;

	return $price;
}

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