package RPS::Import::LiveNation::v2;

use strict;

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

sub _fieldMap {
    {
        labelName        => 'A',
        serviceID        => 'C',
        dateBegin        => 'B',
        productType      => 'H',
        formatType       => 'C',
        artistName       => 'G',
        upc              => 'K',
        albumName        => 'J',
        isrc             => 'I',
        trackName        => 'H',
        units            => 'L',
        price            => 'O',
    }
}

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

sub countryCode  { 'US' }
sub currencyCode { 'USD' }
sub mediaType    { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub serviceID {
    my $self = shift;

    my $service = lc( $self->_getByFieldName('serviceID') || '' );
    my $serviceID = $self->getServiceID($service) || $RPS::Import::ServiceMap::SERVICE_ALIASES{$service};
    return $serviceID if $serviceID;

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

sub productType {
    my $self = shift;

    my $songTitle = $self->_getByFieldName('trackName')  || '';
    my $albumTitle = $self->_getByFieldName('albumName') || '';

    return RPS::File::Sale::TYPE_TRACK if $songTitle;
    return RPS::File::Sale::TYPE_ALBUM if !$songTitle && $albumTitle;

    $self->fail("Unable to determine product type");
}

sub formatType {
    my $self = shift;

    my $type = $self->_getByFieldName('formatType') || "";

    return RPS::File::Sale::FORMAT_STREAM if ($type =~ /^youtube(?:_red)?$/i);

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


sub units {
    my $self = shift;

    my $units = $self->_getByFieldName('units') || 0;
    my $price = $self->_getByFieldName('price') || 0;

    if ( $price > 0 ) {
        $units = $units ? abs($units) : 1;
    } elsif ( $price < 0 ) {
       $units = $units ? -1 * abs($units) : -1;
    }

    return $units;
}

1;
