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

use strict;

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

sub _fieldMap {
    {
        dateBegin        => 'A3', # pre-header
        countryCode      => 'H',
        clientProductID  => 'B',
        upc              => 'D',
        retailPrice      => 'K',
        sales            => 'L',
        salesRevenue     => 'M',
        totalRevenue     => 'M',
    }
}

sub _unverifiedFieldMap {
    {
        itemDescription  => 'C', # we'll get the config, album name and artist name from here
    }
};

sub _headerIdentifier { ( countryCode => 'Country' ) }

sub currencyCode { 'USD'; }

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

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

sub physical { 1; }

sub _parseItemDescription {
    my $self = shift;
    my $desc = $self->_getByFieldName('itemDescription');

    my $artist;
    my $album;
    my $config;

    if( $desc =~ /(.+)-(.+)\((.+)\)$/ ) #  artistName - albumTitle - (config)
    {
        ( $artist, $album, $config ) = ($1, $2, $3);
        $artist = Common::Util::trimspaces($artist);
        $album = Common::Util::trimspaces($album);
    }
    return( $artist, $album, $config );
}

sub albumName {
    my $self = shift;
    my( $artist, $album, $config ) = $self->_parseItemDescription();
    return $album;
}

sub artistName {
    my $self = shift;
    my( $artist, $album, $config ) = $self->_parseItemDescription();
    return $artist;
}

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

    # Determine the country code
    #
    my %countryMap = (
        "usa" => "US",
        "u.s.a" => "US",
    );
    $name = ( exists $countryMap{ lc $name } ) ? $countryMap{ lc $name } : $name;
    if( !$name || '' eq $name )
    {
        $self->fail("Missing country code on line ". $self->linenum);
    }
    my $o = Common::Country->Get( $name );
    return (defined $o) ? $o->alpha2() : $name;
}

sub saleDates {
    my $self = shift;
    my $dt = $self->_getByFieldName('dateBegin');
    return $self->_getDates( date_begin => $dt );
}

sub productType {
    my $self = shift;
    my( $artist, $album, $config ) = $self->_parseItemDescription();

    if( $config =~ /^cd$/i ||
        $config =~ /^White w\/ Purple Swirl EP$/i )
    {
        return RPS::File::Sale::TYPE_CD;
    }
    elsif( $config =~ /^lp$/i )
    {
        return RPS::File::Sale::TYPE_LP;
    }
    elsif( $config =~ /10"/ ||
           $config =~ /Vinyl EP/i ||
           $config =~ /12"/ ||
           $config =~ /^2XLP$/i ||
           $config =~ /^ep$/i )
    {
        return RPS::File::Sale::TYPE_LP5;
    }
    $self->fail("Unable to determine config from '$config'");
}


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

    return ( $sales > 0 ) ? $sales : 0;
}

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

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

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

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 );
}

# Only the 'sales' tab is processed for physical sales.  We do check the 'digital'
# tab though to make sure that no digital sales are in the file.
#
sub _processSheet {
    my $self     = shift;
    my $sheet    = shift;
    my $sheetnum = shift;

    my $sheetname = $self->{sheetname};

    if( $sheetname =~ /^sales$/i )
    {
        return $self->SUPER::_processSheet( $sheet, $sheetnum );
    }
    elsif( $sheetname =~ /^digital$/i )
    {
        # Make sure no digital sales are in this file.
        #
        foreach my $line(@$sheet)
        {
            $self->{line} = $line;

            # Skip the line if it looks like a header
            #
            next if( @$line[1] =~ /^Release$/i && @$line[2] =~ /^UPC$/i && @$line[3] =~ /^Artist$/i );

            # Otherwise check for _any_ data on the line
            #
            my $i=0;
            foreach my $col(@$line)
            {
                if( $col && '' ne $col )
                {
                    $self->fail("Sales detected on '$sheetname' worksheet");
                    last;
                }
                $i++;
            }
        }
    }
}

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