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

package RPS::Import::Integra::v6;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

use Carp qw(croak);

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

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

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

sub _fieldMap {
    {
        dateBegin => 'E',    # header area

        productType => 'A',
        labelName   => 'F',
        artistName  => 'G',
        albumName   => 'H',

        isrc => 'J',
        upc  => 'J',

        trackName => 'I',
        units     => 'L',
        price     => 'O',
    };
}

sub _unverifiedFieldMap {
    { credits => 'N', };
}

sub saleDates {
    my $self = shift;

    # from header; see _preProcess
    return ( $self->{_startDate}, $self->{_endDate} ) if ( $self->{_startDate} );
}

sub formatType { RPS::File::Sale::FORMAT_DOWNLOAD }

sub _headerIdentifier { ( isrc => 'LookUP' ) }

sub currencyCode { 'USD' }

sub countryCode { 'US' }

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

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

    if ( uc($productType) eq 'S' ) {
        return RPS::File::Sale::TYPE_TRACK;
    } elsif ( ( uc($productType) eq 'F' ) || ( uc($productType) eq 'T' ) ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } else {
        $self->fail("Unrecognized product type: $productType");
    }
}

sub units {
    my $self    = shift;
    my $count   = $self->_getByFieldName('units');
    my $credits = $self->_getByFieldName('credits');
    return $count + $credits;
}

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

    return $upc if ( !$trackName || $trackName eq '' );
}

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

    return $isrc if ( $trackName && $trackName ne '' );
}

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

    my $artistName = $name;

    if ( $name =~ /(\w+),\s+(.*)$/i )    # lastname, firstname
    {
        $artistName = $2 . " " . $1;
    }
    return $artistName;
}

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

    if ( $self->{sheetname} =~ /^dist rep(ort)?$/i ) {

        # Get the date information from the header area of the current sheet
        #
        foreach my $line (@$sheet) {
            $self->{line} = $line;

            unless ($self->{_startDate} && $self->{_endDate}){
                my $dt = $self->_getByFieldName('dateBegin');
                if ( $dt =~ /^ROYALTY REPORT: (\w+) (\d{4})$/i ) {
                    my $month = $1;
                    my $year  = $2;

                    my ( $sdate, $edate ) = $self->_getDates( date_begin => "$1 $2" );    # Ex: January 2013
                    $self->{_startDate} = $sdate;
                    $self->{_endDate}   = $edate;
                    last;
                }
            }

            $self->{linenum}++;
        }

        return $self->SUPER::_processSheet( $sheet, $sheetnum );
    }

    #    else { print STDERR "Skipping sheet '". $self->{sheetname} . "'\n"; }
}

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