#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# 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',
        },
        19 => {
            countryCode      => 'AV',
            dateBegin        => 'A',
            productType      => 'Z',
            artistName       => 'E',
            clientProductID  => 'AD',
            upc              => 'AE',
            albumName        => 'D',
            currencyCode     => 'F',
            retailPrice      => 'G',
            sales            => 'H',
            salesRevenue     => 'Y',
        },
        20 => {
            countryCode      => 'AV',
            dateBegin        => 'A',
            productType      => 'Z',
            artistName       => 'E',
            clientProductID  => 'AC',
            upc              => 'AD',
            albumName        => 'D',
            currencyCode     => 'F',
            retailPrice      => 'G',
            sales            => 'H',
            salesRevenue     => 'Y',
        },
        23 => {
            countryCode      => 'AT',
            dateBegin        => 'A',
            productType      => 'Y',
            artistName       => 'E',
            clientProductID  => 'AB',
            upc              => 'AC',
            albumName        => 'D',
            currencyCode     => 'F',
            retailPrice      => 'G',
            sales            => 'H',
            salesRevenue     => 'X',
        }
    );
    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
        || $type =~ /^''Alex Calder'' \+ ''Time'' Bundle$/i
        || $type =~ /^''Dada'' \+ ''No Worry No Mind'' Bundle$/i
        || $type =~ /^''Won'' \+ ''Wishlist'' Bundle$/i
        || $type =~ /^.*\(CD\)$/i
        || $type =~ /^‘Alex Calder'' \+ ''Time'' Bundle$/i
        || $type =~ /^CD \+ January Sun Bonus Audio$/i
        || $type =~ /^Compact Disc$/i
        || $type =~ /^Compact Disc \(CD\) \+ Digital Album$/i
        || $type =~ /^'Dada'' \+ ''No Worry No Mind'' Bundle$/i
        || $type =~ /^Limited Edition CD$/i
        || $type =~ /^Limited First Run CD$/i
        || $type =~ /^Offering CD$/i
        || $type =~ /^'Won'' \+ ''Wishlist'' Bundle$/i
        ) {
        $productType = RPS::File::Sale::TYPE_CD;
    }
    elsif ( $type =~ /^4xCD$/i
        || $type =~ /^Buy Double CD Album$/i
        || $type =~ /^3xCD release!$/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 =~ /^Limited Edition 12.* Vinyl \+ Digital Album$/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
        || $type =~ /^.*\(black vinyl\)$/i
        || $type =~ /^2xLP/i
        || $type =~ /^No Worry No Mind 12.* EP/i
        || $type =~ /^12'' Black Vinyl$/i
        || $type =~ /^12" Vinyl LP$/i
        || $type =~ /^Black Vinyl LP$/i
        || $type =~ /^Clear Vinyl 2xLP Gatefold$/i
        || $type =~ /^Limited Edition 12'' Vinyl LP$/i
        || $type =~ /^Limited Edition 5LP Box Set \(Clear Vinyl\)$/i
        || $type =~ /^Limited Edition Clear Vinyl$/i
        || $type =~ /^Limited Edition Vinyl$/i
        || $type =~ /^Limited Edition White Vinyl LP$/i
        || $type =~ /^LP Bundle$/i
        || $type =~ /^LP\+7''$/i
        || $type =~ /^Offering LP$/i
        || $type =~ /^Record\/Vinyl$/i
        || $type =~ /^Standard 12'' Vinyl$/i
        || $type =~ /^Standard Edition 12'' Black Vinyl$/i
        || $type =~ /^Special Edition LP$/i
        || $type =~ /^Vinyl LP$/i
      ) {
        $productType = RPS::File::Sale::TYPE_LP;
    }
    elsif ( $type =~ /^Limited Edition 7.* Vinyl$/i
        || $type =~ /^Signed Limited Edition 7'' Vinyl$/i
        || $type =~ /^7'' Single$/i
        || $type =~ /^12'' EP$/i
        || $type =~ /^12'' Single$/i
        || $type =~ /^GRIM REAPER ''The Show Must Go On'' 7-inch single, 1984$/i
        ) {
        $productType = RPS::File::Sale::TYPE_LP5;
    }
    elsif ( $type =~ /^CD\/DVD$/i ) {
        $productType = RPS::File::Sale::TYPE_DVD_CD_SET;
    }
    elsif ( $type =~ /^(?:TAPE )?Cassette$/i
        || $type =~ /^Cassette Pre-Order$/i
        || $type =~ /^Limited Edition Cassette$/i
        || $type =~ /^Cassete$/i
        || $type =~ /^CRYPTIC SLAUGHTER ''Stream Of Consciousness'' cassette$/i
        || $type =~ /^Offering Cassette$/i
        ) {
        $productType = RPS::File::Sale::TYPE_CASS;
    }
    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 );
}

sub _processSheet {
    my ($self, $sheet, $sheetnum) = @_;

    if ( $self->_version && $self->_version == 20 ) {
        # We only process the 'Physical' for version 20
        return unless $self->{sheetname} =~ /^Physical$/i;
    }

    return $self->RPS::Import::Importer::_processSheet( $sheet, $sheetnum );
}

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