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

package RPS::Import::SoundCloud::v1;

use strict;
use POSIX qw/ceil/;

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

use lib '/app/tools/rps/lib';
use RPS::Import::ServiceMap;

use base 'RPS::Import::Importer';

sub _fieldMap {
    my $self = shift;

    my %map = (
        1 => {
            dateBegin        => 'M',
            labelName        => 'C',
            formatType       => 'S',
            artistName       => 'F',
            serviceProductID => 'I',
            upc              => 'L',
            isrc             => 'K',
            albumName        => 'G',
            trackName        => 'H',
            units            => 'O',
            price            => 'V',
            countryCode      => 'N',
            currencyCode     => 'Q',
            mediaType        => 'J',
        },
        2 => {
            labelName        => 'C',
            countryCode      => 'N',
            dateBegin        => 'M',
            productType      => 'K',
            formatType       => 'R',
            serviceProductID => 'I',
            artistName       => 'F',
            upc              => 'L',
            albumName        => 'G',
            isrc             => 'K',
            trackName        => 'H',
            units            => 'O',
            currencyCode     => 'P',
            price            => 'U',
            mediaType        => 'J',
        }
    );

    return $map{ $self->version };
}

sub _headerIdentifier { artistName => 'Artist Name' }

sub productType { RPS::File::Sale::TYPE_TRACK; }

sub formatType {
    my $self = shift;
    my $fmt  = $self->_getByFieldName('formatType');
    if ( $fmt =~ /^streaming$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    $self->fail("Unknown format type '$fmt'");
}

sub mediaType {
    my $self = shift;
    my $fmt  = $self->_getByFieldName('mediaType');
    if ( $fmt =~ /^(audio|music)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    $self->fail("Unknown media type '$fmt'");
}

sub saleDates {
    my $self = shift;

    my $dateBegin = $self->_getByFieldName('dateBegin');

    return $self->_getDates( date_begin => $dateBegin );
}

sub units {
    my $self = shift;

    my $units = $self->_getByFieldName('units') || 0;
    return ceil($units);
}

sub _isValidSaleRecord {
    my $self = shift;
    return ($self->units || $self->price) && $self->isrc;
}


1;
