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

package RPS::ArtistRoyalty::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::ArtistRoyalty::Fast::Static::ArtistPayeeAccounts;
use RPS::ArtistRoyalty::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);
    }

    # !!! Need to make this even more generic.
    # !!! We want to emit a statement for every payee that is not inactive.

    Log->info("Getting Payee IDs");
    my $accounts = RPS::ArtistRoyalty::Fast::Static::ArtistPayeeAccounts->GetArtistPayeeAccounts($dataPath);

    my $fd = $self->_openFileDescriptor($outputDirectory);

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

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

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

    return *OUTPUT;
}

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

    # All this does is put a 'no-op' line in the data stream with the payee_id.
    # This will cause us to create a Statement for every Payee that has an account.
    #

    my $gFormatString = '%12d'                                                    # payee_id
      . "\t" . '%12d'                                                             # album_id - let's set this to 0 explicitly
      . "\t" . '%12d'                                                             # set this to 0 as well
      . "\t" . RPS::ArtistRoyalty::Fast::Mapped::Data::kTypePayee . "\t" . '0'    # type
      . "\t" . '%12d'                                                             # and this
      . "\t" . '%12d'    # and this.  These first 6 fields are shared amongst all types.
      . "\n";

    print $outFD sprintf( $gFormatString, $artistPayeeID, 0, 0, 0, 0, );
}

1;
