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

package RPS::Import::Secretly;

use strict;

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

sub _fieldMap {
    {
        labelName        => 'V',
        dateBegin        => 'S',
        productType      => 'K',
        artistName       => 'G',
        clientProductID  => 'C',
        upc              => 'E',
        isrc             => 'F',
        trackName        => 'H',
        albumName        => 'D',
        price            => 'Q',
        units            => 'L',
        countryCode      => 'J',
    }
}

sub _unverifiedFieldMap {
    {
        dsp => 'I',  # digpay cosmetic dsp
    }
}

sub _headerIdentifier { ( dateBegin => 'dateProcessedlast' ) }


sub currencyCode { 'USD' }


sub saleDates {
    my $self = shift;
    my $startDate = $self->_getDateBegin();
    my ($dateBegin,$dateEnd) = $self->_getDates( date_begin => $startDate );
    return ($dateBegin, $dateEnd);
}

sub productType {
    my $self = shift;
    my $isrc = $self->_getByFieldName('isrc');
    return (defined $isrc && '' ne $isrc) ? RPS::File::Sale::TYPE_TRACK : RPS::File::Sale::TYPE_ALBUM;
}

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

    if( $type =~ /^(album|track)$/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif( $type =~ /^(cloud|stream)$/i ||
           $type =~ /^Radio \((Broadcasting Statutory|Direct|Statutory)\)$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif( $type =~ /^Not a sale$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    else
    {
        $self->fail( "Could not parse format type from '$type'" );
    }
}


sub albumName {
    my $self = shift;
    my $name = $self->_getByFieldName('albumName');
    return ( $name =~ /'(.+)'/ ) ? $1 : $name;
}


sub trackName {
    my $self = shift;
    my $name = $self->_getByFieldName('trackName');
    my $isrc = $self->_getByFieldName('isrc');
    return (defined $isrc && '' ne $isrc) ? $name : undef;
}


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


sub units {
    my $self = shift;
    my $units = $self->SUPER::units() || return;
    my $price = $self->SUPER::price();

    # The 'sign' of the units is driven by the price.
    # If the price is negative, we'll store negative units,
    # regardless of whether the units were positive or negative
    # in the sale file.  Accordingly, the price is always
    # positive.  This ensures that returns are recorded correctly.
    #
    if( defined($price) )
    {
        return $price < 0 ? abs($units) * -1 : $units;
    }
    else
    {
        return $units;
    }
}

sub unitPrice {
    return abs( shift->SUPER::unitPrice() );
}


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