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

package RPS::Import::Bleep::v5;

use strict;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::Client;

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

use lib '/app/tools/data_classes/lib';

use lib '/app/tools/rps/lib';
use RPS::Import::SaleRec;
use RPS::DB::Item::MediaType;
use RPS::File::Sale;

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

sub _fieldMap {
    {
        labelName       => 'C',
        countryCode     => 'O',
        productType     => 'G',
        artistName      => 'I',
        albumName       => 'J',
        trackName       => 'K',
        upc             => 'M',
        isrc            => 'N',
        units           => 'T',
        price           => 29,
        clientProductID => 'E',
    };
}

sub _unverifiedFieldMap {
    {
        month  => 'A',
        year   => 'B',
        format => 'H',
    };
}

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

sub upc {
    my $self = shift;

    my $upc = $self->_getByFieldName('upc') || '';
    $upc =~ s/^(\d+).*/$1/g;

    return $upc;
}

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

sub currencyCode { 'GBP' }

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

sub _headerIdentifier { ( upc => 'FormatUPC' ) }

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

    if ( $ptype =~ m/^track$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    } elsif ( $ptype =~ m/^release$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } else {
        $self->fail("Unable to determine product type from '$ptype'");
    }
}

sub formatType {
    my $self = shift;
    my $format = lc( $self->_getByFieldName('format') ) || "";

    return RPS::File::Sale::FORMAT_DOWNLOAD if ($format =~ /^mp3$/i);
    return RPS::File::Sale::FORMAT_DOWNLOADPREMIUM if ($format =~ /^(?:flac|wav( 24bit)?)$/i);

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

sub saleDates {
    my $self  = shift;
    my $month = $self->_getByFieldName('month');
    my $year  = $self->_getByFieldName('year');
    my $d     = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
    return $self->_getDates( date_begin => $d );
}

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