package RPS::Import::State51::v3;

use strict;

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

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

sub _fieldMap {
    {
        labelName        => 'N',
        countryCode      => 'Z',
        dateBegin        => 'C',
        dateEnd          => 'D',
        productType      => 'AC',
        artistName       => 'G',
        upc              => 'P',
        isrc             => 'R',
        serviceProductID => 'S',
        clientProductID  => 'Q',
        albumName        => 'I',
        trackNum         => 'M',
        trackName        => 'J',
        units            => 'V',
        price            => 'BV',
        currencyCode     => 'BT1',
    };
}

sub _unverifiedFieldMap {
    {
        serviceName   => 'B',
        productTitle  => 'K',
        activityType  => 'AE',
        countryOfSale => 'Y',
    };
}

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

sub _headerIdentifier { ( albumName => 'Album Title' ) }

sub albumName {
    my $self         = shift;
    my $albumName    = $self->_getByFieldName('albumName');
    my $productTitle = $self->_getByFieldName('productTitle');
    if ( $self->productType eq RPS::File::Sale::TYPE_ALBUM ) {
        return ($albumName) ? $albumName : $productTitle;
    }
    return $albumName;
}

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

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

sub serviceID {
    my $self        = shift;

    my $serviceName = $self->_getByFieldName('serviceName') || '';
    return Client::Service::DSP_STATE51 if $serviceName =~ /I Feel Music/i;

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

    $self->fail("Unknown service '$serviceName'");
}

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;
}

sub price {
    my $self  = shift;
    my $units = $self->_getByFieldName('units');
    my $price = $self->_getByFieldName('price');
    return ( $units > 0 && $price < 0 ) ? ( $price * -1 ) : $price;
}

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

sub countryCode {
    my $self  = shift;

    my $countryOfSale = $self->_getByFieldName('countryOfSale') || "";
    my $countryCode   = $self->_getByFieldName('countryCode') || "";

    return $countryCode   if ($countryCode);
    return $countryOfSale if ($countryOfSale);
    return "GB";
}

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');    # AC
    my $isrc = $self->_getByFieldName('isrc');           # R

    if ($type) {
        if ( $type =~ /^(a|album|b_album)$/i ) {
            return RPS::File::Sale::TYPE_ALBUM;
        }
        elsif ( $type =~ /^b_single$/i ) {
            if ($isrc) {
                return RPS::File::Sale::TYPE_TRACK;
            }
            else {
                return RPS::File::Sale::TYPE_ALBUM;
            }
        }
        elsif ( $type =~ /^(audio|t|track|trackrelease)$/i ) {
            return RPS::File::Sale::TYPE_TRACK;
        }

    } else {

        # if col AC is blank use col R (isrc) to determine the type
        #
        if ($isrc) {
            return RPS::File::Sale::TYPE_TRACK;
        } else {
            return RPS::File::Sale::TYPE_ALBUM;
        }
    }
    $self->fail("Unable to determine product type from '$type'");
}

sub formatType {
    my $self = shift;
    my $type = $self->_getByFieldName('activityType') || "";    # AE

    if (   $type =~ /^a la carte$/i
        || $type =~ /^download$/i
        || $type =~ /^permanentdownload$/i
        || $type =~ /^permanent mp3 a la carte tracks$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $type =~ /^device play counts$/i
        || $type =~ /^play counts$/i ) {
        return RPS::File::Sale::FORMAT_TETHERED;
    } elsif ( $type =~ /(?:non-subscriber )?stream/i
        || $type =~ /on-demand/i
        || $type =~ /ondemandstream/i
        || $type =~ /streams aac\+/i
        || $type =~ /streams mp3/i
        || $type =~ /streams mp3 trial overage/i
        || $type =~ /subscriber stream/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    }

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

sub currencyCode {
    my $self = shift;

    my $code = $self->_getByFieldName('currencyCode') || "";
    $code =~ /\s(\w+)$/;
    return $1 if $1;

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

1;
