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

use strict;

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

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

# This importer has to deal with three tabs, two of which share
# the same header (the third, 'INTL SALES', is slightly different).
# To make things more interesting, there's a blank column on the
# left which causes the column labels to be off by one (hence the
# _getOffset method, below).  In the _fieldMap we're using the column
# labels from the importer template.  Again, the 'INTL SALES' tab is
# different so it won't exactly match the template columns.
#
sub _fieldMap {
    my $self      = shift;
    my $version   = $self->_version;
    my $sheetname = lc $self->sheetname;

    my %field_map = (
        'aust digital' => {
            dateBegin   => 'B',    # QUARTER ENDING
            productType => 'F',    # TRACK
            formatType  => 'D',    # SALES TYPE
            isrc        => 'C',    # CATALOGUE NO.
            albumName   => 'E',    # RECORDING, use if productType = A
            trackName   => 'E',    # RECORDING, use if productType = T
            units       => 'G',    # ROYALTY SALES
            price       => 'K',    # ROYALTY EARNINGS ($)
        },
        'intl sales' => {
            countryCode => 'D',    # TERRITORY
            dateBegin   => 'B',    # QUARTER ENDING
            productType => 'G',    # TRACK
            formatType  => 'E',    # SALES TYPE
            albumName   => 'F',    # RECORDING, use if productType = A
            isrc        => 'C',    # CATALOGUE NO.
            trackName   => 'F',    # RECORDING, use if productType = T
            units       => 'H',    # ROYALTY SALES
            price       => 'L',    # ROYALTY EARNINGS ($)
        },
        'nz sales' => {
            dateBegin   => 'B',    # QUARTER ENDING
            productType => 'F',    # TRACK
            formatType  => 'D',    # SALES TYPE
            isrc        => 'C',    # CATALOGUE NO.
            albumName   => 'E',    # RECORDING, use if productType = A
            trackName   => 'E',    # RECORDING, use if productType = T
            units       => 'G',    # ROYALTY SALES
            price       => 'K',    # ROYALTY EARNINGS ($)
        },
    );
    return $field_map{$sheetname};
}

sub _getOffset {
    my $self  = shift;
    my $field = shift;

    my $offset = $self->SUPER::_getOffset($field);

    return undef if ( !defined $offset );

    return $offset - 1;
}

sub _headerIdentifier { ( dateBegin => 'ENDING' ) }

sub currencyCode { 'AUD' }

sub countryCode {
    my $self = shift;

    if ( exists $self->_fieldMap->{'countryCode'} ) {
        my $country = $self->_getByFieldName('countryCode');

        # adjust for this common misspelling
        if ( lc($country) eq 'columbia' ) { $country = 'COLOMBIA' }

        my $obj = Common::Country->Get($country);
        return $obj->alpha2() if ($obj);

        return $country;    # Let MapFace deal with it
    }
    return 'AU' if ( $self->{sheetname} =~ /^AUST DIGITAL$/i );
    return 'NZ' if ( $self->{sheetname} =~ /^NZ SALES$/i );
}

sub albumName {
    my $self = shift;

    if ( exists $self->_fieldMap->{'albumName'}
        && ( !defined $self->productType || $self->productType eq RPS::File::Sale::TYPE_ALBUM ) ) {
        my $albumName = $self->_getByFieldName('albumName');
        return $albumName;
    }
}

sub trackName {
    my $self = shift;

    if ( exists $self->_fieldMap->{'trackName'}
        && ( !defined $self->productType || $self->productType eq RPS::File::Sale::TYPE_TRACK ) ) {
        my $trackName = $self->_getByFieldName('trackName');
        return $trackName;
    }
}

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

sub saleDates {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || return;

    my $dateBegin;
    my $dateEnd;

    if ( $date =~ /\d{5}/ )    # Excel
    {
        ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $date, type => "msft" );

    }
    return ( $dateBegin, $dateEnd );
}

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

    if ( $type =~ /^single track$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    } elsif ( $type =~ /^all tracks$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } else {
        return $self->handleError( "Unknown product type: $type", RPS::DB::Item::SaleImportError::kErrorProductType );
    }
}

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

    if (   $type =~ /^PORTABLE SUBSCRIPTION INCOME$/i
        || $type =~ /^AD-FUNDED STREAMING BY THIRD PARTY$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $type =~ /^DIGITAL NORMAL - PERMANENT DOWNLOAD$/i
        || $type =~ /^NORMAL RETAIL SALES$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $type =~ /^MOBILE MASTER TONES CALL RING$/i ) {
        return RPS::File::Sale::FORMAT_RINGTONE;
    } else {
        return $self->handleError( "Unknown product type: $type", RPS::DB::Item::SaleImportError::kErrorFormatType );
    }
}

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

    if (   $self->{sheetname} =~ /^INTL SALES$/i
        || $self->{sheetname} =~ /^AUST DIGITAL$/i
        || $self->{sheetname} =~ /^NZ SALES$/i ) {
        return $self->SUPER::_processSheet( $sheet, $sheetnum );
    }
}

#####    MapFace    #####
#
sub _mapFaceServiceID {
    return ('serviceID');
}

sub _mapFaceCountryCode {
    return ('countryCode');
}

sub _mapFaceProductType {
    return ('productType');
}

sub _mapFaceFormatType {
    return ('formatType');
}

sub _mapFaceMediaType {
    return ('mediaType');
}

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