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

use strict;

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

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

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

sub _fieldMap {
    {
        # Statement Summary Tab
        #
        dateBegin => 'B7',

        # detail
        #
        labelName       => 'A',
        countryCode     => 'N',
        artistName      => 'C',
        upc             => 'I',
        albumName       => 'D',
        clientProductID => 'B',
        totalRevenue    => 'AB',
    };
}

sub _unverifiedFieldMap {
    {
        netQuantity  => 'S',
        netSales     => 'AB',
        formatConfig => 'G',
    };
}

sub _headerIdentifier { ( artistName => 'Release Artist' ) }

sub currencyCode { 'GBP' }

sub physical { 1 }

sub channel { RPS::File::Sale::CHANNEL_RETAIL }

sub priceLevel { RPS::File::Sale::PLEVEL_UNKNOWN }

sub productType {
    my $self = shift;
    my $cfg  = $self->_getByFieldName('formatConfig');
    if ( $cfg =~ /^cd$/i ) {
        RPS::File::Sale::TYPE_CD;
    } elsif ( $cfg =~ /^lp(s)?$/i ) {
        RPS::File::Sale::TYPE_LP;
    } elsif ( $cfg =~ /^s12$/i ) {
        RPS::File::Sale::TYPE_LP5;
    } else {
        $self->fail("Unable to get configuration from '$cfg'");
    }
}

sub saleDates {
    my $self = shift;
    return ( $self->{_dateBegin}, $self->{_dateEnd} );    # See _preProcess
}

sub _preProcess {
    my $self = shift;
    my %args = @_;

    $self->SUPER::_preProcess(%args);

    my $linenum  = 0;
    my $sheetnum = 0;

    foreach my $sheet ( @{ $args{sheets} } ) {
        my $sheetName = $args{sheet_names}->[ $sheetnum++ ];

        # Check if Digital Sales sheet exists and error out if it has any data
        #
        if ( $sheetName =~ /^digital sales$/i ) {
            $linenum = 0;
            foreach my $line (@$sheet) {
                $self->{line} = $line;

                my $dt = $self->_getByFieldName('clientProductID');
                $dt =~ s/\s+//g;

                if ( $dt !~ /^catalogno$/i && '' ne $dt ) {
                    $self->fail("Found digital data in physical sales file");
                }
                $linenum++;
            }
        }

        # Get the date information from the Statement Summary tab
        #
        elsif ( $sheetName =~ /statement summary/i ) {
            $linenum = 0;
            foreach my $line (@$sheet) {
                $self->{line} = $line;

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

                # parse and store the date
                #
                if ( $dt =~ /^Period: (\d{1,2})\/(\d{1,2})\/(\d{4})/ )    # DD/MM/YYYY  (Euro-style)
                {
                    my $dateBegin = sprintf( "%04d-%02d-%02d", $3, $2, $1 );
                    ( $self->{_dateBegin}, $self->{_dateEnd} ) = $self->_getDates( date_begin => $dateBegin );
                }
                $linenum++;
            }
        }
    }
}

sub sales {
    my $self    = shift;
    my $sales   = $self->_getByFieldName('netQuantity');
    my $revenue = $self->_getByFieldName('netSales');
    return ( $sales >= 0 && $revenue > 0 ) ? $sales : 0;
}

sub salesRevenue {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('netSales');
    return ( $revenue >= 0 ) ? $revenue : 0;
}

sub returns {
    my $self    = shift;
    my $sales   = $self->_getByFieldName('netQuantity');
    my $revenue = $self->_getByFieldName('netSales');
    return ( $revenue < 0 ) ? abs($sales) : 0;    # Follow the sign of the revenue
}

sub returnsRevenue {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('netSales');
    return ( $revenue < 0 ) ? abs($revenue) : 0;
}

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

    return $self->SUPER::_processSheet( $sheet, $sheetnum ) if ( $self->{sheetname} =~ /physical sales/i );
}

sub _isValidSaleRecord {
    my $self = shift;

    # The line is valid if it has sales or returns, and has an album title
    #
    return ( ( $self->sales != 0 || $self->returns != 0 ) && $self->albumName );
}

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