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

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

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use lib '/app/tools/data_classes/lib';
use Common::Assert;
use Common::RSApp;
use Common::RSDB;
use Client::Client;

# This package excapsulates the Factory constructor for Fetcher objects.
# Essentially, this call will abstract away the code that decides which
# Fetcher subclass to create.
#
# I prefer to put this sort of thing into it's own package to avoid weird
# circular includes that arise if you make this a static method of the base class...

# Don't forget to _include_ these packages...
#
use BookPub::Catalog::Import::Process::Fetcher::DTMVTest;
use BookPub::Catalog::Import::Process::Fetcher::DTMVDemo;
use BookPub::Catalog::Import::Process::Fetcher::Hachette;
use BookPub::Catalog::Import::Process::Fetcher::HachetteBeta;
use BookPub::Catalog::Import::Process::Fetcher::HachetteUK;
use BookPub::Catalog::Import::Process::Fetcher::Bloomsbury::UK;
use BookPub::Catalog::Import::Process::Fetcher::Bloomsbury::USA;
use BookPub::Catalog::Import::Process::Fetcher::Bloomsbury::WW;
use BookPub::Catalog::Import::Process::Fetcher::FaberAndFaber;
use BookPub::Catalog::Import::Process::Fetcher::FaberFactory;
use BookPub::Catalog::Import::Process::Fetcher::RandomHouseUK;
use BookPub::Catalog::Import::Process::Fetcher::Macmillan;
use BookPub::Catalog::Import::Process::Fetcher::MacmillanAU;
use BookPub::Catalog::Import::Process::Fetcher::MacmillanAUPrint;
use BookPub::Catalog::Import::Process::Fetcher::MacmillanUK;
use BookPub::Catalog::Import::Process::Fetcher::Penguin;
use BookPub::Catalog::Import::Process::Fetcher::Workman;
use BookPub::Catalog::Import::Process::Fetcher::Wiley;
use BookPub::Catalog::Import::Process::Fetcher::OpenRoad;
use BookPub::Catalog::Import::Process::Fetcher::WWNorton;
use BookPub::Catalog::Import::Process::Fetcher::OxfordUniversityPress;
use BookPub::Catalog::Import::Process::Fetcher::Disney;
use BookPub::Catalog::Import::Process::Fetcher::BerrettKoehler;
use BookPub::Catalog::Import::Process::Fetcher::RowmanLittlefield;
use BookPub::Catalog::Import::Process::Fetcher::Hyperion;
use BookPub::Catalog::Import::Process::Fetcher::SagePub;

my %gPackageMap = (
    Client::Client::kDTMVTest()          => 'BookPub::Catalog::Import::Process::Fetcher::DTMVTest',
    Client::Client::kDTMVDemo()          => 'BookPub::Catalog::Import::Process::Fetcher::DTMVDemo',
    Client::Client::kHachetteUK()        => 'BookPub::Catalog::Import::Process::Fetcher::HachetteUK',
    Client::Client::kBloomsburyUK()      => 'BookPub::Catalog::Import::Process::Fetcher::Bloomsbury::UK',
    Client::Client::kBloomsburyUSA()     => 'BookPub::Catalog::Import::Process::Fetcher::Bloomsbury::USA',
    Client::Client::kBloomsburyWW()      => 'BookPub::Catalog::Import::Process::Fetcher::Bloomsbury::WW',
    Client::Client::kMacmillan()         => 'BookPub::Catalog::Import::Process::Fetcher::Macmillan',
    Client::Client::kMacmillanAU()       => 'BookPub::Catalog::Import::Process::Fetcher::MacmillanAU',
    Client::Client::kMacmillanAUPrint()  => 'BookPub::Catalog::Import::Process::Fetcher::MacmillanAUPrint',
    Client::Client::kMacmillanUK()       => 'BookPub::Catalog::Import::Process::Fetcher::MacmillanUK',
    Client::Client::kWiley()             => 'BookPub::Catalog::Import::Process::Fetcher::Wiley',
    Client::Client::kBerrettKoehler()    => 'BookPub::Catalog::Import::Process::Fetcher::BerrettKoehler',
    Client::Client::kRowmanLittlefield() => 'BookPub::Catalog::Import::Process::Fetcher::RowmanLittlefield',
    Client::Client::kSagePub()           => 'BookPub::Catalog::Import::Process::Fetcher::SagePub',
);

sub NewFetcher {
    my (%args) = @_;

    my $clientID = Common::RSApp::GetClientID();
    my $pkg      = $gPackageMap{$clientID};
    die "ERROR - no package declared for client $clientID" unless $pkg;

    return $pkg->new(%args);
}

# Returns an array of valid client ids.
# These will comprise all the clients we have a Fetcher package for, EXCEPT for
# our test clients.
#
sub ValidClientIDs {
	my $testThisMethod = shift || 0;

    # On non-production (i.e. dev) servers, we just want to return the ids for
    # clients we actually have onboard the server...
    #
    my $localIDs;
    if ( not Common::RSApp::IsProductionServer() and not $testThisMethod ) {
        $localIDs = {};
        foreach my $localID ( Common::RSDB::GetLocalClientIDs() ) {
            $localIDs->{$localID} = 1;
        }
    }

    my @ids;
    foreach my $id ( keys %gPackageMap ) {

        #next if (Client::Client::kDTMVTest() == $id);
        #next if (Client::Client::kDTMVDemo() == $id);
        next if ( defined $localIDs && !$localIDs->{$id} );

        push @ids, $id;
    }

    return @ids;
}

1;
