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

package RPS::Import::Addictech::v2;

use strict;

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

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

sub _fieldMap {
    {
        labelName        => 'L',
        dateBegin        => 'G',
        dateEnd          => 'G',
        formatType       => 'P',
        productType      => 'O',
        artistName       => 'M',
        upc              => 'C',
        isrc             => 'B',
        trackName        => 'N',
        albumName        => 'K',
        price            => 'Q',
        serviceProductID => 'E',
    };
}

sub units { 1; }

sub countryCode { 'US' }

sub currencyCode { 'USD' }

sub _headerIdentifier { ( dateBegin => 'Sale Timestamp' ) }

sub trackName {
    my $self = shift;

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

    return undef;
}

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

    if ( $type eq "3" ) {
        return RPS::File::Sale::TYPE_TRACK;
    } elsif ( $type eq "6" ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } else {
        $self->fail("Could not parse product type from '$type'");
    }
}

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

    my $format;
    if ( $formatType =~ m/^(192 kbps mp3|320 kbps mp3)$/i ) {
        $format = RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $formatType =~ m/^(wav|flac)$/i ) {
        $format = RPS::File::Sale::FORMAT_DOWNLOADPREMIUM;
    } else {
        $self->fail("Could not parse format type from '$formatType'");
    }
    return $format;
}

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

    my $mediaType;
    if (   $formatType =~ m/^(192 kbps mp3|320 kbps mp3)$/i
        || $formatType =~ m/^(wav|flac)$/i ) {
        $mediaType = RPS::DB::Item::MediaType::kMediaTypeAudio;
    } else {
        $self->fail("Could not parse media type from '$formatType'");
    }
    return $mediaType;
}

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