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

package RPS::Import::CreateSpace::v3;

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 {
    {
        countryCode => 'E',
        dateBegin   => 'A',
        dateEnd     => 'A',
        productType => 'F',
        upc         => 'G',
        trackName   => 'C',
        albumName   => 'B',
        units       => 'M',
        price       => 'N',
    };
}

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

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

    if ( $type =~ /Amazon MP3 Track/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    } elsif ( lc $type =~ /Amazon MP3 Album/ ) {
        return RPS::File::Sale::TYPE_ALBUM;
    }
}

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

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

sub _noHeader { return 1; }

sub _processLine {
    my $self = shift;

    if ( $self->linenum() == 3 ) {

        $gDateBegin = $self->_getByFieldName('albumName');

    } elsif ( $self->linenum() == 4 ) {

        $gDateEnd = $self->_getByFieldName('albumName');

    } elsif ( $self->linenum() > 6 and $self->_isValidSaleRecord() ) {
        $self->_saveLine();
        $self->{_linesImported}++;
    } else {
        $self->{_linesIgnored}++;
    }
}

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

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

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