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

package RPS::Import::CreateSpace::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 {
    {
        dateBegin     => 'B',
        dateEnd       => 'B',
        albumName     => 'B',
        trackName     => 'C',
        type          => 'D',
        countryCode   => 'E',
        serviceID     => 'F',
        upc           => 'G',
        units         => 'N',
        price         => 'O',
    }
}

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

#-------------------------------------------------
# gDateBegin and gDateEnd will be set once we find
# this information in the header
#-------------------------------------------------
my( $gDateBegin, $gDateEnd );

sub _noHeader   { return 1; }

sub _processLine {
    my $self = shift;

    if( $self->linenum() == 3 )
    {
        my $_date = $self->_getByFieldName('dateBegin');
        $gDateBegin = $self->_normalizeDate($_date);

    }
    elsif( $self->linenum() == 4 )
    {
        my $_date = $self->_getByFieldName('dateEnd');
        $gDateEnd = $self->_normalizeDate($_date);

    }
    elsif( $self->linenum() > 6 and $self->_isValidSaleRecord() )
    {
        $self->_saveLine();
        $self->{_linesImported}++;
    }
    else
    {
        my $linenum   = $self->linenum();
        my $units     = $self->units();
        my $trackName = $self->trackName();
        print STDERR "skipping line $linenum, u($units) track($trackName)\n";

        $self->{_linesIgnored}++;
    }
}

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 _formatDate {
    my $self   = shift;
    my $day    = shift;
    my $month  = lc(shift);
    my $year   = shift;
    my %months = (
        jan => 1,
        feb => 2,
        mar => 3,
        apr => 4,
        may => 5,
        jun => 6,
        jul => 7,
        aug => 8,
        sep => 9,
        oct => 10,
        nov => 11,
        dec => 12 );

    my $date = sprintf( "%04s-%02s-%02s", $year, $months{$month}, $day );
    return $date;
}


sub _normalizeDate {
    my($self, $dt) = @_;
    my $date;

    my $year;
    my $month;
    my $day;

    my $monthSelector = "(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)";
    if ( $dt =~ /(\d+)-$monthSelector-(\d{2})/i )
    {
        $day   = $1;
        $month = $2;
        $year  = $3 + 2000;
        $date = $self->_formatDate( $day, $month, $year );
    }
    else
    {
        $date = $dt;
    }
    return $date;
}

sub saleDates {
    my $self = shift;
    return( $gDateBegin, $gDateEnd );
}


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

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