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

package RPS::LabelRoyalty::Fast::Mapped::Data;

use strict;
use Data::Dumper;
use File::Path;
use IO::File;

use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';
use lib '/app/tools/raptor/lib';
use Common::RSApp;

use Data::Dumper;
use DB_File;

use Common::Log;

# Common columns
#
# !!! I don't know that I actually need the album and track ids in here, since we have product_id...
#
use constant kPayeeID          => 0;
use constant kTypeCode         => 1;
use constant kLabelID          => 2;
use constant kLabelContractID  => 3;
use constant kAlbumID          => 4;
use constant kTrackID          => 5;
use constant kServiceID        => 6;
use constant kDistributorID    => 7;
use constant kProductType      => 8;
use constant kFormatType       => 9;
use constant kMediaType        => 10;
use constant kCountryCode      => 11;
use constant kConversionRate   => 12;
use constant kDistributionFee  => 13;
use constant kProductID        => 14;
use constant kPrice            => 15;
use constant kFree             => 16;
use constant kUnits            => 17;
use constant kStartDate        => 18;
use constant kEndDate          => 19;
use constant kCurrencyCode     => 20;
use constant kSaleID           => 21;
use constant kAlbumName        => 22;    # something of a waste of space, but since we want to sort by these...
use constant kDiscNo           => 23;    # but, we do this in the interest of (processing) time. perhaps in
use constant kTrackNo          => 24;    # the future, we'll come up with a better time/space tradeoff.
use constant kPayeeMinPayment  => 25;
use constant kPayeeBalance     => 26;
use constant kPayeeOnHold      => 27;
use constant kExceptionID      => 28;
use constant kExceptionService => 29;
use constant kExceptionCountry => 30;

# Pending transaction columns
# !!! This is not going to work, ever.
# !!! The data is going to be interpreted abstractly by the assembler, so at the very least
# !!! we need to shift these columns over to leave the regular ID columns as 0.
#
use constant kPendingTransactionID          => 8;
use constant kPendingTransactionAmount      => 9;
use constant kPendingTransactionMemo        => 10;
use constant kPendingTransactionCheckNumber => 11;
use constant kPendingTransactionTypeCode    => 12;
use constant kPendingTransactionDate        => 13;

# We'll have the transactions sort before the sales data.
#
use constant kDataPayee       => 0;
use constant kDataTransaction => 1;
use constant kDataSale        => 2;

sub FileName { 'mapped_data' }

sub _new {
    my ( $class, $dataPath ) = @_;

    my $self = bless {}, $class;
    return $self->_init($dataPath);
}

sub _init {
    my ( $self, $dataPath ) = @_;

    $self->{FD} = IO::File->new("< $dataPath") or die "ERROR : Unable to open $dataPath for reading: $!";

    return $self;
}

sub nextLine {
    my ($self) = @_;

    my $fd   = $self->{FD};
    my $line = <$fd>;
    return undef unless $line;

    chomp $line;
    my @data = split( "\t", $line );
    return \@data;
}

sub GetMappedData {
    my ( $class, $dataPath ) = @_;

    my $filename = "$dataPath/" . $class->FileName() . '.sorted';

    return $class->_new($filename);
}

sub GetPayeeToSalesMapCount {
    my ( $class, $dataPath ) = @_;

    my $filename = "$dataPath/" . $class->FileName() . '.sorted';
    my $file = $class->_new($filename);
    my %payeeToSalesMapCount;

    while ( my $line = $file->nextLine() ) {
        my $payee_id = $line->[0];
        $payee_id =~ s/^\s+|\s+$//g;
        $payeeToSalesMapCount{$payee_id}++;
    }

    return %payeeToSalesMapCount;
}

1;

