#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package BookPub::Tracker::Command::DownloadRoyaltyReport::Factory;
use strict;
use warnings;

# This package provides a 'Factory' interface for the Download Royalty Report response.
# It will return the 'correct' class, based on client id.

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use lib '/app/tools/data_classes/lib';
use Client::Client;
use Common::RSApp;
use Common::Assert;
use BookPub::Tracker::Command::DownloadRoyaltyReport::MacmillanAU;
use BookPub::Tracker::Command::DownloadRoyaltyReport::MacmillanAUPrint;
use BookPub::Tracker::Command::DownloadRoyaltyReport::RowmanLittlefield;
use BookPub::Tracker::Command::DownloadRoyaltyReport::HachetteUK;
use BookPub::Tracker::Command::DownloadRoyaltyReport::SagePub;

my %gPackages = (
    Client::Client::kMacmillanAU()       => 'BookPub::Tracker::Command::DownloadRoyaltyReport::MacmillanAU',
    Client::Client::kMacmillanAUPrint()  => 'BookPub::Tracker::Command::DownloadRoyaltyReport::MacmillanAUPrint',
    Client::Client::kRowmanLittlefield() => 'BookPub::Tracker::Command::DownloadRoyaltyReport::RowmanLittlefield',
    Client::Client::kHachetteUK()        => 'BookPub::Tracker::Command::DownloadRoyaltyReport::HachetteUK',
    Client::Client::kSagePub()           => 'BookPub::Tracker::Command::DownloadRoyaltyReport::SagePub',
);

sub GetResponse {
    my ($report) = @_;
    assert( defined $report, 'MISSING report parameter' );

    # Do we have a custom response for this client?
    #
    my $response;
    my $clientID = Common::RSApp::GetClientID();
    my $pkg      = $gPackages{$clientID};

    if ($pkg) {
        $response = $pkg->getResponse($report);
    } else {

        # Else just do it the standard way
        open( my $fh, $report->filePath() );
        $response = RSApache::Response::Download->new( $fh, 'application/octet-stream', $report->fileName() );
    }

    return $response;
}

1;
