#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2012 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package RPS::Import::State51::v2;

use strict;

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

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

sub _fieldMap {
    {
        # pre-header
        currencyCode     => 'A',

        # detail
        labelName        => 'N',
        countryCode      => 'Z',
        dateBegin        => 'C',
        dateEnd          => 'D',
        productType      => 'AC',
        artistName       => 'G',
        upc              => 'P',
        isrc             => 'R',
        serviceProductID => 'S',  # DMS id
        clientProductID  => 'Q',  # Cat No
        albumName        => 'I',
        trackName        => 'J',
        units            => 'V',
        price            => 'BV',
    }
}

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');
    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') || return;
    my $price = $self->_getByFieldName('price');
    return ( $units > 0 && $price < 0 ) ? ($units * -1) : $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 $altCC = $self->_getByFieldName('countryOfSale');
    my $ccode = $self->_getByFieldName('countryCode');

    my %countryMap = (
        'uk' => 'GB',
    );

    $ccode = (!$ccode) ? $altCC : $ccode;

    $ccode = $countryMap{lc $ccode} if( defined $countryMap{lc $ccode});

    $self->fail('Missing country code') if ( !$ccode || "" eq $ccode );

    return $ccode if( my $o = Common::Country->Get($ccode) );

    $self->fail("Invalid country code '$ccode'");
}

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 =~ /^$/                   ||
        $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;

    return $self->{_currencyCode} if( $self->{_currencyCode} ); # from header; see _processSheet

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


sub _processSheet {
    my $self     = shift;
    my $sheet    = shift;
    my $sheetnum = shift;

    # Only process the sheet named "full ext"
    #
    if( $self->{sheetname} =~ /^full ext$/ )
    {
        my $currencyCode;

        foreach my $line(@$sheet)
        {
            $self->{line} = $line;

            my $_ccode = $self->_getByFieldName('currencyCode');

            if( $_ccode && $_ccode =~ /^sales \((\w{3})\)/i )
            {
                $self->{_currencyCode} = $1;
                last;
            }
        }
        return $self->SUPER::_processSheet( $sheet, $sheetnum );
    }
}

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