#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2013 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package RPS::Import::Bandcamp::v9;

use strict;

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

sub _fieldMap {
    {
        dateBegin     => 'A',
        productType   => 'B',
        formatType    => 'D',
        artistName    => 'E',
        upc           => 'H',
        albumName     => 'C',
        isrc          => 'I',
        trackName     => 'C',
        units         => 'G',
        price         => 'J',
    }
}

sub _headerIdentifier { ( upc => 'upc' ) }
sub mediaType { RPS::DB::Item::MediaType::kMediaTypeAudio }
sub countryCode { 'US' }
sub currencyCode { 'USD' }

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType') || return;
    if( $type =~ /^track$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    }
    elsif( $type =~ /^album$/i )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    }
    $self->fail("Could not parse product type from '$type'");
}

sub formatType {
    my $self = shift;
    my $format = $self->_getByFieldName('formatType');
    if( $format =~ /^digital download$/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    $self->fail("Could not parse format type from '$format'");
}

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 isrc {
    my $self = shift;
    my $isrc = $self->_getByFieldName('isrc');
    if( $isrc )
    {
        return Common::Util::normalize_isrc($isrc);
    }
}

sub upc {
    my $self = shift;
    my $upc = $self->_getByFieldName('upc');
    if( $upc )
    {
        return Common::Util::normalize_upc($upc);
    }
}

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin');
    
    # 12/1/14 10:46am
    if( $date =~ /(\d{1,2})\/\d{1,2}\/(\d{2})/ )
    {
        my $month = $1;
        if ($month < 10)
        {
            $month = "0" . $month;
        }
        my $year = "20" . $2;
        $date = $year . "-" . $month . "-01";
    }
    return $date
}

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

    $units =~ s/\.0$//;

    # 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.
###
