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

package RPS::Import::Cleopatra::v1;

use strict;

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

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

sub _fieldMap {
    {
        countryCode => 'U',
        dateBegin   => 'Y',
        productType => 'X',
        formatType  => 'X',
        artistName  => 'AC',
        upc         => 'AA',
        albumName   => 'AB',
        isrc        => 'AA',
        trackName   => 'AB',
        units       => 'T',
        price       => 'AP',
    };
}

sub mediaType    { RPS::DB::Item::MediaType::kMediaTypeAudio }
sub currencyCode { 'USD' }

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

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

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

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

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType') || return;    # col S
    my $upc  = $self->_getByFieldName('upc');
    if ( $type =~ /^as|s|dt|td|ut|dr$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    } elsif ( $type =~ /^da$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } elsif ( $type =~ /^dd$/i ) {

        # if UPC starts with a number then it's an album, otherwise it's a track
        #
        return ( $upc =~ /^\d/ ) ? RPS::File::Sale::TYPE_ALBUM : RPS::File::Sale::TYPE_TRACK;
    }

    $self->fail("Unable to determine product type from '$type'");
}

sub formatType {
    my $self = shift;
    my $type = $self->_getByFieldName('formatType') || return;    # col S
    if ( $type =~ /^as|s$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $type =~ /^dt|da|dd$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $type =~ /^td$/i ) {
        return RPS::File::Sale::FORMAT_TETHERED;
    } elsif ( $type =~ /^ut$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOADUPGRADE;
    } elsif ( $type =~ /^dr$/i ) {
        return RPS::File::Sale::FORMAT_RINGTONE;
    }

    $self->fail("Unable to determine format type from '$type'");
}

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