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

package RPS::Import::DDS::v2;

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 = (
        2  => {
            labelName     => 'E',
            serviceID     => 'C',
            countryCode   => 'D',
            dateBegin     => 'A',
            dateEnd       => 'B',
            productType   => 'H',
            formatType    => 'Q',
            artistName    => 'F',
            upc           => 'I',
            albumName     => 'G',
            isrc          => 'J',
            trackName     => 'H',
            units         => 'K',
            price         => 'M',
            mediaType     => 'Q',
        },
    );
    return $field_map{$self->_version()};
}

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

    my %field_map = (
        2 => {
            unitPrice     => 'L',
        },
    );
    return $field_map{$self->_version()};
}

sub _headerIdentifier { ( labelName => 'Label' ) }
sub currencyCode { 'USD' }


sub saleDates {
    my $self = shift;
    my $dateBegin = $self->_getByFieldName('dateBegin');
    my $dateEnd = $self->_getByFieldName('dateEnd');
    my $nextMonth;

    # if dateBegin is populated
    if( $dateBegin =~ /^(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})$/ ) {
        # Amazon reports the last day of the month when the dateBegin
        # should actually be the first of the next month
        $nextMonth = $1 + 1;
        $dateBegin = sprintf("%04d-%02d-%02d", $3, $nextMonth, 1 );
        return $self->_getDates( date_begin => $dateBegin );
    } 
    elsif( $dateBegin =~ /^(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})$/ ) {
        my $year = $1;
        $nextMonth = $2 + 1;
        if( $nextMonth == 13 ) {
            $nextMonth = 1;
            $year++;
        }
        $dateBegin = sprintf("%04d-%02d-%02d", $year, $nextMonth, 1 );
        return $self->_getDates( date_begin => $dateBegin );
    } 
    # if dateBegin is blank
    elsif( undef $dateBegin || $dateBegin eq '' ) {
        #use dateEnd for date
        # MM/DD/YYYY
        if( $dateEnd =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/ ) {
            $dateEnd = sprintf("%04d-%02d-%02d", $3, $1, 1 );
            return $self->_getDates( date_begin => $dateEnd );
        } 
        # YYYY'M'MM, like 2015M11 for November 2015
        elsif( $dateEnd =~ /^(\d{4})M(\d{1,2})$/ ) {
            $dateEnd = sprintf("%04d-%02d-%02d", $1, $2, 1 );
            return $self->_getDates( date_begin => $dateEnd );
        } 
        else {
            $self->fail("Could not find date in '$dateEnd'");
        }
    }
    else {
        $self->fail("Could not find date in '$dateBegin'");
    }
}

sub serviceID {
    my $self = shift;
    
    my $service = $self->_getByFieldName('serviceID');
    my $serviceID = $self->getServiceID(lc ($service)) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };
    return $serviceID if( $serviceID );
    $self->fail("Unknown service '$service'");
}

sub productType {
    my $self = shift;
    my $trackName = $self->_getByFieldName('trackName');
    my $upc = $self->_getByFieldName('upc');
    my $isrc = $self->_getByFieldName('isrc');
    
    if( $trackName eq '' || lc($trackName) eq 'full album' ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } elsif( $upc && ! $isrc ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } else {
        return RPS::File::Sale::TYPE_TRACK;
    }
}

sub formatType {
    my $self = shift;
    my $format = $self->_getByFieldName('formatType');
    
    if( $format =~ /^(as|s|cl|nr|av|streaming radio|streaming|stream|radio|cloud|st)$/i ||
        $format =~ /^apple radio$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif( $format =~ /^(da|dt|download)$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif( $format =~ /^(td|ta)$/i ) {
        return RPS::File::Sale::FORMAT_TETHERED;
    }
    else {
        $self->fail("Unable to determine format type from '$format'");
    }
}

sub mediaType {
    my $self = shift;
    my $format = $self->_getByFieldName('formatType');
    
    if( $format =~ /^(as|dt|s|td|cl|da|ta|nr|download|streaming radio|streaming|stream|radio|cloud|st)$/i ||
        $format =~ /^apple radio$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    if( $format =~ /^av$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }
    else {
        $self->fail("Unable to determine media type from '$format'");
    }
}

sub countryCode {
    my $self  = shift;
    my $country = $self->_getByFieldName( 'countryCode' );
    my %countryMap = (
        'U.S.' => 'US'
    );
    $country = (exists $countryMap{ uc $country }) ? $countryMap{ uc $country } : $country;
    my $obj = Common::Country->Get( $country );

    return $obj ? $obj->alpha2 : undef;
}

sub unitPrice {
    my $self = shift;

    # The sales file is reporting the unit price, so
    # we provide unitPrice to prevent it from being derived
    #
    my $unitPrice = $self->_getByFieldName('unitPrice');

    return $unitPrice;
}


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