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

package RPS::LabelRoyalty::Fast::Script::MapPayees;

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

use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';
use lib '/app/tools/raptor/lib';

use DB_File;

use Common::Util;
use Common::Log;
use Common::Parser;
use Common::WriteXML;

use RPS::LabelRoyalty::Fast::Static::LabelPayeeAccounts;
use RPS::LabelRoyalty::Fast::Mapped::Data;


use base 'Common::Script';


sub _options {{
    client_id => 
    {
        short       => 'c',
        required    => 1,
        description => 'Limit to this client id',
        parameter    => 'i'
    },
    data_path => 
    {
        short       => 'd',
        required    => 1,
        description => 'path to directory containing static data',
        parameter    => 's'
    },
    output_path => 
    {
        short       => 'o',
        required    => 1,
        description => 'path to directory to write output files',
        parameter    => 's'
    },
}}



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

    my $dataPath = $self->param('data_path');

    my $outputDirectory = $self->param('output_path');

    # JPK - Should it be the responsibility of this process to create the directory if
    # it doesn't already exist?   It's a bit of a stretch, but then again it's convenient...
    #
    if (! -d $outputDirectory)
    {
        # !!! Might want to make this behavior optional...
        #
        mkpath($outputDirectory);
    }



    # Ok, what is the point of this mapper?
    # 

    Log->info("Getting Payee IDs");
    my $accounts = RPS::LabelRoyalty::Fast::Static::LabelPayeeAccounts->GetLabelPayeeAccounts($dataPath); 


    my $fd = $self->_openFileDescriptor($outputDirectory);
    
    while (my ($labelPayeeID, $dataString) = each %$accounts)
    {
        $self->_outputLine($fd, $labelPayeeID, $dataString);
    }
}


sub _openFileDescriptor
{
    my ($self, $outputDirectory) = @_;

    open OUTPUT, "> $outputDirectory/" . RPS::LabelRoyalty::Fast::Mapped::Data->FileName() . "_$$.unsorted" or die "ERROR: $!";

    return *OUTPUT;
}



sub _outputLine
{
    my ($self, $outFD, $labelPayeeID, $dataString) = @_;

    my ($minPayment, $balance, $onHold) = split("\t", $dataString);


    # This data will look like a regular sale data line, except most of the columns will be blank.
    #
    my @outData;
    push @outData, $labelPayeeID;
    push @outData, RPS::LabelRoyalty::Fast::Mapped::Data::kDataPayee;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, 0;
    push @outData, $minPayment;
    push @outData, $balance;
    push @outData, $onHold;

    print $outFD join("\t", @outData) . "\n";


}


1;
