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

package RPS::Import::TopSpin::v2;

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tool/common/lib';
use Common::Country;

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

sub _fieldMap {
    {
        countryCode  => 'BB',
        dateBegin    => 'G',
        dateEnd      => 'G',
        productType  => 'AA',
        artistName   => 'E',
        albumName    => 'AA',
        retailPrice  => 'J',
        sales        => 'P',
        salesRevenue => 'Q',
        currencyCode => 'I',
        totalRevenue => 'V',
    };
}

sub _unverifiedFieldMap {
    {
        distFee   => 'U',
        creditFee => 'T',
    };
}

my %productTypeMap = (
    'cd'                                              => RPS::File::Sale::TYPE_CD,
    'cd + download'                                   => RPS::File::Sale::TYPE_CD,
    'cd + digital'                                    => RPS::File::Sale::TYPE_CD,
    'cd + dvd'                                        => RPS::File::Sale::TYPE_CD,
    'cd + dvd + t-shirt'                              => RPS::File::Sale::TYPE_CD,
    'cd & digital + t-shirt'                          => RPS::File::Sale::TYPE_CD,
    'cd + t-shirt'                                    => RPS::File::Sale::TYPE_CD,
    'cd + official fan club'                          => RPS::File::Sale::TYPE_CD,
    'dvd'                                             => RPS::File::Sale::TYPE_DVD,
    'documentary dvd'                                 => RPS::File::Sale::TYPE_DVD,
    'merchandise'                                     => RPS::File::Sale::TYPE_LP,
    'deluxe + t-shirt'                                => RPS::File::Sale::TYPE_LP,
    'deluxe'                                          => RPS::File::Sale::TYPE_LP,
    'deluxe edition'                                  => RPS::File::Sale::TYPE_LP,
    'deluxe package + dvd'                            => RPS::File::Sale::TYPE_LP,
    'deluxe package + t-shirt'                        => RPS::File::Sale::TYPE_LP,
    'deluxe package + t-shirt + dvd'                  => RPS::File::Sale::TYPE_LP,
    'deluxe version'                                  => RPS::File::Sale::TYPE_LP,
    'limited edition + fan club'                      => RPS::File::Sale::TYPE_LP,
    'limited edition deluxe box set'                  => RPS::File::Sale::TYPE_LP,
    "limited edition collector's set w/ bonus tracks" => RPS::File::Sale::TYPE_LP,
    'lp'                                              => RPS::File::Sale::TYPE_LP,
    'lp + digital'                                    => RPS::File::Sale::TYPE_LP,
    'lp + dvd'                                        => RPS::File::Sale::TYPE_LP,
    'lp + dvd + t-shirt'                              => RPS::File::Sale::TYPE_LP,
    'lp + t-shirt'                                    => RPS::File::Sale::TYPE_LP,
    'vinyl'                                           => RPS::File::Sale::TYPE_LP,
    'vinyl + official fan club'                       => RPS::File::Sale::TYPE_LP,
    'vinyl + t-shirt'                                 => RPS::File::Sale::TYPE_LP,
    'ep'                                              => RPS::File::Sale::TYPE_EP,
);

sub channel    { RPS::File::Sale::CHANNEL_DIRECT }
sub priceLevel { RPS::File::Sale::PLEVEL_UNKNOWN }
sub physical   { 1 }

sub _headerIdentifier { ( artistName => 'Artist Name' ) }

sub salesRevenue {
    my $self      = shift;
    my $distFee   = $self->_getByFieldName('distFee');
    my $creditFee = $self->_getByFieldName('creditFee');
    my $revenue   = $self->SUPER::salesRevenue - $distFee - $creditFee;

    return $revenue;
}

sub albumName {
    my $self = shift;

    my $albumName = $self->_getByFieldName('albumName');

    $albumName =~ s/ - .+?$//;

    return $albumName;
}

sub productType {
    my $self = shift;

    my $configuration = $self->_getByFieldName('productType');

    if ( $configuration =~ m/ - (.+?)$/ ) {
        my $productType = $productTypeMap{ lc $1 };

        if ($productType) {
            return $productType;
        }
    }

    # They removed the dash from some of the lines,
    # so we have to try finding a match a little differently now.
    #
    foreach my $productText ( keys %productTypeMap ) {

        # Make the product text regex-safe
        my $productTextClean = $productText;
        $productTextClean =~ s/\//\\\//g;
        $productTextClean =~ s/\+/\\\+/g;

        print STDERR "---- testing " . lc($configuration) . " against $productText ($productTextClean)\n";

        if ( lc($configuration) =~ m/$productTextClean$/ ) {
            print STDERR "---- found a match, looking in hash for value of key $productText\n";
            return $productTypeMap{$productText};
        }
    }

    print STDERR "Can't determine product type from $configuration";
    $self->errstr("Can't determine product type from $configuration");
    return undef;
}

sub _isValidSaleRecord {
    my $self = shift;

    my $productType = $self->_getByFieldName('productType');

    # We want to skip sales with this configuration.
    if ( lc($productType) =~ m/mmj sign up image/ ) {
        return undef;
    }

    return ( $self->sales && $self->albumName );
}

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