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

use strict;

use lib '/app/tools/rps/lib';

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

# Tech Note: There are three ever-so-slightly different Zvook headers covered by
# this importer version: subscription/streaming, download/tracks and
# download/releases.  The first two are mostly the same -- just some slight
# differences in the column header text.  The last one (download/releases) takes
# it a step further and puts the artist and units data in different columns,
# hence the extra logic in the artistName() and units() methods.
#
sub _fieldMap {
    {
        dateBegin   => 'A5',
        productType => 'A6',
        formatType  => 'A6',
        artistName  => 'E',
        isrc        => 'B',
        upc         => 'B',
        trackName   => 'C',
        albumName   => 'C',
        units       => 'F',
        price       => 'K',
    },
    ;
}

sub _unverifiedFieldMap {
    {
        # The following are for the download/releases tab only.
        # All others use the 'artistName' and 'units' column.
        #
        altArtistName => 'D',
        altUnits      => 'E',
    };
}

sub _headerIdentifier { ( price => qr/^Total Sum for Licensor/i ) }

sub countryCode { 'RU' }
sub mediaType   { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub currencyCode { shift->{_currencyCode} }    # See _isHeader

sub artistName {
    my $self = shift;
    return $self->{_isDownloadReleasesSheet} ? $self->_getByFieldName('altArtistName') : $self->_getByFieldName('artistName');
}

sub productType {
    my $self = shift;

    return $self->{_productType} if ( $self->{_productType} );

    my $type = $self->_getByFieldName('productType');
    if ( $type =~ /^(streaming|subscription|download\: tracks)$/i ) {
        $self->{_productType} = RPS::File::Sale::TYPE_TRACK;
    } elsif ( $type =~ /^download\: releases$/i ) {
        $self->{_productType} = RPS::File::Sale::TYPE_ALBUM;
    } else {
        $self->fail("Could not parse product type from '$type'");
    }
}

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

    return $self->{_formatType} if ( $self->{_formatType} );

    my $type = $self->_getByFieldName('formatType');
    if ( $type =~ /^streaming$/i ) {
        $self->{_formatType} = RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $type =~ /^subscription$/i ) {
        $self->{_formatType} = RPS::File::Sale::FORMAT_TETHERED;
    } elsif ( $type =~ /^download\: (tracks|releases)$/i ) {
        $self->{_formatType} = RPS::File::Sale::FORMAT_DOWNLOAD;
    } else {
        $self->fail("Could not parse format type from '$type'");
    }
}

sub saleDates {
    my $self = shift;

    return ( $self->{_dateBegin}, $self->{_dateEnd} ) if ( $self->{_dateBegin} );

    my $dt = $self->_getByFieldName('dateBegin');

    my ( $startDate, $endDate ) = $self->_getDates( date_begin => $dt );

    if ( $startDate && $endDate ) {
        ( $self->{_dateBegin}, $self->{_dateEnd} ) = ( $startDate, $endDate );
    } else {
        $self->fail("Could not date from '$dt'");
    }
}

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

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

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

sub upc {
    my $self = shift;
    return if ( $self->productType ne RPS::File::Sale::TYPE_ALBUM );
    my $upc = $self->_getByFieldName('upc');
    if ($upc) {
        return Common::Util::normalize_upc($upc);
    }
}

sub units {
    my $self = shift;

    my $units = $self->{_isDownloadReleasesSheet} ? $self->_getByFieldName('altUnits') : $self->_getByFieldName('units');
    my $price = $self->_getByFieldName('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 ensures that returns are recorded correctly.
    #
    if ( defined($price) ) {
        return $price < 0 ? abs($units) * -1 : $units;
    } else {
        return $units;
    }
}

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

sub _isHeader {
    my $self = shift;

    # Grab the currency code from the header if we haven't already done so
    #
    if ( !$self->{_currencyCode} ) {
        my $price = $self->_getByFieldName('price');
        if ( $price =~ /Total Sum For Licensor \((\w{3})\)/i ) {
            $self->{_currencyCode} = $1;
        }
    }
    return $self->SUPER::_isHeader();
}

sub _processSheet {
    my $self     = shift;
    my $sheet    = shift;
    my $sheetnum = shift;
    my $version  = $self->_version;

    # Reset our custom attributes for each sheet. These can vary from
    # sheet to sheet, so we reset them before we process the sheet.
    #
    $self->{_currencyCode}            = undef;
    $self->{_productType}             = undef;
    $self->{_formatType}              = undef;
    $self->{_dateBegin}               = undef;
    $self->{_dateEnd}                 = undef;
    $self->{_isDownloadReleasesSheet} = undef;

    # Check if we're dealing with the 'Releases' tab of a download file.
    # If so, then flag it so we know where to find the artist and units info.
    #
    $self->{_isDownloadReleasesSheet} = 1 if ( $self->{sheetname} =~ /^releases$/i );

    # Process the detail lines
    #
    $self->RPS::Import::Importer::_processSheet( $sheet, $sheetnum );
}
###
1;    # Play nicely.
###
