package RPS::Import::IODA::v18;

use strict;

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

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

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

sub _fieldMap {
    {
        serviceID       => 'A',
        artistName      => 'I',
        labelName       => 'G',
        albumName       => 'K',
        upc             => 'L',
        trackName       => 'M',
        isrc            => 'N',
        countryCode     => 'C',
        units           => 'R',
        currencyCode    => 'S',
        price           => 'W',
        productType     => 'X',
        formatType      => 'X',
        mediaType       => 'X',
        clientProductID => 'J',
    };
}

sub _unverifiedFieldMap {
    {
        period => 'E',
        year   => 'D',
    };
}

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

sub countryCode {
    my $self = shift;

    my $ccode;
    my $cname = $self->_getByFieldName('countryCode');

    $cname = "DO" if ( $cname =~ /^dominican repub/i );
    $cname = "AE" if ( $cname =~ /^united arab emi/i );
    $cname = "VE" if ( $cname =~ /^venezuela boliv/i );
    $cname = "CS" if ( $cname =~ /^serbia and mont/i );
    $cname = "BN" if ( $cname =~ /^brunei darussal/i );
    $cname = "PG" if ( $cname =~ /^papua new guine/i );
    $cname = "BA" if ( $cname =~ /^bosnia and herz/i );
    $cname = "AG" if ( $cname =~ /^antigua and bar/i );
    $cname = "KN" if ( $cname =~ /^saint kitts and/i );
    $cname = "TC" if ( $cname =~ /^turks and caico/i );
    $cname = "LY" if ( $cname =~ /^libyan arab jam/i );
    $cname = "LA" if ( $cname =~ /^laos/i );

    if ( $cname =~ /^(\w{2,3})$/ )    # 2 or 3 character
    {
        $ccode = $cname;
    } else {
        my $o = Common::Country->Get($cname) || $self->fail("Unknown country '$cname'");
        $ccode = $o->alpha2();
    }
    return $ccode;
}

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

    # return the serviceID if it exists
    return $serviceID if ($serviceID);

    # otherwise, key off product type and return one of these two Apple service IDs
    my $type = uc( $self->_getByFieldName('productType') );
    return Client::Service::DSP_APPLE_MUSIC if ( $service eq 'iTunes/Apple' && ( $type eq 'CL' || $type eq 'NR' ) );
    return Client::Service::DSP_ITUNES
      if ( ( $service eq 'iTunes/Apple' || $service eq 'iTunes/Apple Music' ) && ( $type eq 'DA' || $type eq 'DT' ) );

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

sub saleDates {
    my $self   = shift;
    my $period = $self->_getByFieldName('period');
    my $year   = $self->_getByFieldName('year');

    my %months = (
        jan => 1,
        feb => 2,
        mar => 3,
        apr => 4,
        may => 5,
        jun => 6,
        jul => 7,
        aug => 8,
        sep => 9,
        oct => 10,
        nov => 11,
        dec => 12
    );
    my $month = $months{ lc $period };

    my $sdate = sprintf( "%04d-%02d-01", $year, $month );

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

sub price {
    my $price = abs( shift->SUPER::price() );
    return $price;
}

sub units {
    my $self  = shift;
    my $units = $self->SUPER::units() || return;
    my $price = $self->SUPER::price();

    # The 'sign' of the units is driven by the price.
    # If the price is negative, we'll store negative units,
    # regardless of whether the units were positive or negative
    # in the sale file.  Accordingly, the price is always
    # positive.  This ensure that the returns are recorded
    # correctly.
    #
    if ( defined($price) ) {
        return $price < 0 ? abs($units) * -1 : $units;
    } else {
        return $units;
    }
}

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

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');
    if ( $type =~ /^(da|ta)$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } elsif ( $type =~ /^(as|av|dr|dt|cl|nr|ns|s|td|sv)$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    }
    $self->fail("Unknown product type '$type'");
}

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

    if ( $type =~ /^td$/i ) {
        return RPS::File::Sale::FORMAT_TETHERED;
    } elsif ( $type =~ /^(as|av|cl|nr|ns|s|sv)$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $type =~ /^(dt|da)$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $type =~ /^dr$/i ) {
        return RPS::File::Sale::FORMAT_RINGTONE;
    }

    $self->fail("Unknown format type '$type'");
}

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

    if ( $type =~ /^(av|sv)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    } elsif ( $type =~ /^(as|dr|dt|da|cl|nr|ns|s|td)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }

    $self->fail("Unknown media type '$type'");
}

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

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

###
1;    #
###
