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

package RPS::Import::Caroline::v11;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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 {
    my $self = shift;
    my $version = $self->_version;

    my %field_map = (
        11 => {
            artistName    => 'H',
            units         => 'K',
            price         => 'L',
        },
    );
    return $field_map{$version};
}

sub _unverifiedFieldMap {
    my $self = shift;
    my $version = $self->_version;

    my %field_map = (
        11 => {
            upcISRC       => 'F',
            subjArea      => 'J',
            title         => 'I',
            year          => 'A',
            month         => 'B',
        },
    );
    return $field_map{$version};
}

sub _headerIdentifier { ( year => 'Fiscal Year' ) }

sub saleDates {
    my $self  = shift;
    my $year  = $self->_getByFieldName('year');
    my $month = $self->_getByFieldName('month');

    my $dateBegin;
    my $dateEnd;

    $dateBegin = sprintf("%04d-%02d-%02d", $year, $month, 1);
    $dateEnd   = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year,  $month ));
    return ($dateBegin, $dateEnd);
}

sub countryCode {
    my $self  = shift;
    $self->{_countryCode}; # See _preProcess
}

sub currencyCode {
    my $self  = shift;
    $self->{_currencyCode}; # See _preProcess
}

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

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

    if( $self->filename =~ /_CA|_CN/i )
    {
        $self->{_countryCode} = 'CA';
        $self->{_currencyCode} = 'CAD';
    }
}

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

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

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

sub formatType {
    my $self  = shift;
    my $subjArea = $self->_getByFieldName('subjArea');
    if( $subjArea =~ /^s$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif( $subjArea =~ /^d$/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    $self->fail("Unable to determine format type from '$subjArea'");
}

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

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

sub productType {
    my $self  = shift;
    my $upcISRC = $self->_getByFieldName('upcISRC');
    if( $upcISRC =~ /^[a-z][a-z].*\d{7}$/i ) # ISRC?
    {
        return RPS::File::Sale::TYPE_TRACK;
    }
    elsif( $upcISRC =~ /^\d{12,13}$/ )  # UPC?
    {
        return RPS::File::Sale::TYPE_ALBUM;
    }
    $self->fail("Unable to determine product type from '$upcISRC'");
}

sub _isValidSaleRecord {
    my $self = shift;
    
    # We need to ignore the total line at the end, which only has values in the units and price columns.
    my $productType = $self->_getByFieldName( 'productType' );
    my $upcISRC = $self->_getByFieldName( 'upcISRC' );
    my $subjArea = $self->_getByFieldName( 'subjArea' );
    my $title = $self->_getByFieldName( 'title' );
    my $year = $self->_getByFieldName( 'year' );
    my $month = $self->_getByFieldName( 'month' );
    
    if (!$productType && !$self->artistName && !$productType && !$upcISRC && !$subjArea && !$title && !$year && !$month) {
        return 0;   
    }       
    
    # For albums, we need either a UPC or Album Name
    if ( $self->productType eq RPS::File::Sale::TYPE_ALBUM ) {
        if ( $self->albumName || $self->upc ) {
            return 1;
        } else {
            $self->fail( "Missing title and upc for album product" );
        }
    }

    # For tracks, we need either an ISRC or Track Name
    if ( $self->productType eq RPS::File::Sale::TYPE_TRACK ) {
        if ( $self->trackName || $self->isrc ) {
            return 1;
        } else {
            $self->fail( "Missing title and isrc for track product" );
        }            
    }    

    return $self->SUPER::_isValidSaleRecord();
}

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