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

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;

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

sub _fieldMap {
    {
        labelName   => 'E',
        dateBegin   => 'O',
        productType => 'P',
        formatType  => 'N',
        artistName  => 'H',
        upc         => 'A',
        isrc        => 'C',
        trackName   => 'G',
        albumName   => 'F',
        price       => 'AB',
        countryCode => 'K',
    };
}

sub _unverifiedFieldMap {
    { saleType => 'Y', };
}

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

sub currencyCode { 'USD' }

sub countryCode {
    my $self        = shift;
    my $countryCode = $self->_getByFieldName('countryCode');

    $countryCode = 'ZAF' if lc($countryCode) eq 'za1';

    if ($countryCode) {
        my $c = new Common::Country( search => $countryCode );
        return $c->alpha2() if ($c);
    }
    return $countryCode;    # Return original name and let MapFace deal with it
}

sub units {
    my $self  = shift;
    my $units = 1;          # default - unless line looks like a 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 ensure that the returns are recorded
    # correctly.
    #
    if ( defined($price) ) {
        return $price < 0 ? abs($units) * -1 : $units;
    } else {
        return $units;
    }
}

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

sub saleDates {
    my $self = shift;

    my $dateBegin;
    my $dateEnd;

    my $startDate = $self->_getDateBegin();

    if ($startDate) {
        ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $startDate );
    }

    return ( $dateBegin, $dateEnd );
}

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

    if ( $type =~ /^release$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } elsif ( $type =~ /^single$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    } else {
        $self->fail("Could not parse product type from '$type'");
    }
}

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

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

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

    if ( $type =~ /^download$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    $self->fail("Could not parse media type from '$type'");
}

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