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

package RPS::Import::Bandcamp::v3;

use strict;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_date normalize_upc);

use lib '/app/tools/rps/lib';
use RPS::Import::Bandcamp::BandcampBase;
use base 'RPS::Import::Bandcamp::BandcampBase';

sub _fieldMap {
    my $self = shift;
    my $version = $self->_version;
    my %field_map = (
        3 => {
            countryCode   => 'AC',
            dateBegin     => 'A',
            productType   => 'U',
            artistName    => 'E',
            serviceProductID => 'X',
            upc           => 'Y',
            albumName     => 'D',
            currencyCode  => 'F',
            sales         => 'H',
            salesRevenue  => 'T',
        },
        5 => {
            countryCode   => 'AI',
            dateBegin     => 'A',
            productType   => 'AA',
            artistName    => 'E',
            serviceProductID => 'AD',
            upc           => 'AE',
            albumName     => 'D',
            currencyCode  => 'F',
            sales         => 'H',
            salesRevenue  => 'Z',
        },
        8 => {
            countryCode   => 'AG',
            dateBegin     => 'A',
            productType   => 'Y',
            artistName    => 'E',
            serviceProductID => 'AB',
            upc           => 'AC',
            albumName     => 'D',
            currencyCode  => 'F',
            sales         => 'H',
            salesRevenue  => 'X',
        },
        11 => {
            countryCode   => 'AU',
            dateBegin     => 'A',
            productType   => 'AA',
            artistName    => 'E',
            serviceProductID => 'AD',
            upc           => 'AE',
            albumName     => 'D',
            currencyCode  => 'F',
            sales         => 'H',
            salesRevenue  => 'Z',
        },
        15 => {
            countryCode   => 'AL',
            dateBegin     => 'A',
            productType   => 'AC',
            artistName    => 'E',
            serviceProductID => 'AF',
            upc           => 'AG',
            albumName     => 'D',
            currencyCode  => 'F',
            retailPrice   => 'G',
            sales         => 'H',
            salesRevenue  => 'AB',
        },
    );
    return $field_map{$version};
}

sub physical { 1 }
sub _headerIdentifier { ( upc => 'upc' ) }
sub priceLevel { RPS::File::Sale::PLEVEL_UNKNOWN; }
sub channel { RPS::File::Sale::CHANNEL_RETAIL; }

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

    my $productType = "";

    if( $type =~ /^CD$/i ||
        $type =~ /^CD Album/i ||
        $type =~ /^Kill the Hook - CD$/i ||
        $type =~ /^The Chase - CD$/i ||
        $type =~ /^CD-ROM$/i ||
        $type =~ /^4 Track Limited Edition Signed and Numbered CD$/i ||
        $type =~ /^Buy CD Album/i ||
        $type =~ /^Signed and Numbered CD Album$/i )
    {
        $productType = RPS::File::Sale::TYPE_CD;
    }
    elsif( $type =~ /^4xCD$/i ||
           $type =~ /^Buy Double CD Album$/i )
    {
        $productType = RPS::File::Sale::TYPE_DBL_CD;
    }
    elsif( $type =~ /^4 Track CD$/i )
    {
        $productType = RPS::File::Sale::TYPE_CD_SIN;
    }
    elsif( $type =~ /^White 12'' Vinyl$/i ||
           $type =~ /^Built on Glass Clear Vinyl 2LP$/i ||
           $type =~ /^Double LP Vinyl Gatefold$/i ||
           $type =~ /^Double Vinyl/i ||
           $type =~ /^LP/i ||
           $type =~ /^The Chase - LP/i ||
           $type =~ /^12.* Vinyl/i ||
           $type =~ /^2LP Vinyl/i ||
           $type =~ /^Kill the Hook - LP$/i ||
           $type =~ /^Limited Edition 12.* Vinyl$/i ||
           $type =~ /^BUY 12'' Vinyl Album$/i ||
           $type =~ /^Buy Vinyl Album$/i ||
           $type =~ /^Special Edition 2 LP 12'' Vinyl$/i ||
           $type =~ /^Double LP/i ||
           $type =~ /^Signed and Numbered LP/i ||
           $type =~ /^Signed and Numbered Special Edition 12'' Vinyl$/i ||
           $type =~ /^Vinyl/i )  # starts with "Vinyl"
    {
        $productType = RPS::File::Sale::TYPE_LP;
    }
    elsif( $type =~ /^Limited Edition 7.* Vinyl$/i ||
           $type =~ /^Signed Limited Edition 7'' Vinyl$/i )
    {
        $productType = RPS::File::Sale::TYPE_LP5;
    }
    elsif( $type =~ /^CD\/DVD$/i )
    {
        $productType = RPS::File::Sale::TYPE_DVD_CD_SET;
    }
    else
    {
        $self->fail( "Could not parse product type from '$type'" );
    }
    
    return $productType;
}

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

    if ( $upc ) {
        return Common::Util::normalize_upc($upc);
    }  
}

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName( 'dateBegin' );
    
    # 04/01/2016 19:54
    if ($date =~ /(\d{1,2})\/\d{1,2}\/(\d{4})/) {
        $date = sprintf("%04d-%02d-01", $2, $1);
    }
    # 12/1/14 10:46am
    elsif ($date =~ /(\d{1,2})\/\d{1,2}\/(\d{2})/) {
        my $month = $1;
        if ($month < 10) {
            $month = "0" . $month;
        }
        my $year = "20" . $2;
        $date = $year . "-" . $month . "-01";
    }
    return $date

}

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

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

sub returns {
    my $self = shift;
    my $units = $self->_getByFieldName('sales');
    my $revenue = $self->_getByFieldName('salesRevenue');
    return ($units < 0 || $revenue < 0) ? abs($units) : 0;
}

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

sub totalRevenue {
    my $self = shift;
    return $self->salesRevenue - $self->returnsRevenue;
}

sub _isValidSaleRecord {
    my $self = shift;

    return ( $self->totalRevenue != 0 || $self->sales != 0 || $self->returns != 0 || $self->upc || $self->albumName );
}



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