#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Support::App;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tools/support/lib';
use Support::Command;

use lib '/app/tools/rps/lib';
use RSApache::RSWebApp;

use base 'RSApache::RSWebApp';

my %gCommandMap = (

    # ---- User Commands ---- #
    'DEFAULT' => {
        DEFAULT => 'Support::Blank'
    },

    'login' => {
        'DEFAULT' => 'Support::User::Command::Login',
        'login'   => 'Support::User::Command::Login',
    },

    'logout' => {
        'DEFAULT' => 'AppUser::Logout::Command::Logout',
        'logout'  => 'AppUser::Logout::Command::Logout',
    },

    'load' => {
        DEFAULT  => 'Support::LoadHistory::Command::List',
        'list'   => 'Support::LoadHistory::Command::List',
        'detail' => 'Support::LoadHistory::Command::Detail',
    },

    'dtmv' => {
        DEFAULT   => 'Support::DTMV::Command::AllClientsLastStatus',
        'ci_all'  => 'Support::DTMV::Command::AllClientsLastStatus',
        'ci_c'    => 'Support::DTMV::Command::ClientCIStatus',
        'ci_s'    => 'Support::DTMV::Command::CIStatus',
        'waiting' => 'Support::DTMV::Command::WaitingFiles',
        'queue'   => 'Support::DTMV::Command::Queue',
    },

    'sale_file_explorer' => {
        'DEFAULT' => 'Support::SaleFileExplorer::Command::Main',
        'show'    => 'Support::SaleFileExplorer::Command::Show',
        'main'    => 'Support::SaleFileExplorer::Command::Main',
        'periods' => 'Support::SaleFileExplorer::Command::ListPeriodsAjax',
        'files'   => 'Support::SaleFileExplorer::Command::ListFilesAjax',
        'sales'   => 'Support::SaleFileExplorer::Command::ListSalesAjax',
    },

    'job_queue' => {
        'DEFAULT'             => 'Support::JobQueue::Command::List',
        'list'                => 'Support::JobQueue::Command::List',
        'show'                => 'Support::JobQueue::Command::Show',
        'show_job'            => 'Support::JobQueue::Command::ShowJob',
        'hoststatustoken'     => 'Support::JobQueue::Command::HostStatusTokenAjax',
        'gethoststatus'       => 'Support::JobQueue::Command::HostStatusAjax',
        'queuetoken'          => 'Support::JobQueue::Command::QueueStateTokenAjax',
        'getqueue'            => 'Support::JobQueue::Command::QueueAjax',
        'getwaitingqueue'     => 'Support::JobQueue::Command::QueueWaiting',
        'getrunningqueue'     => 'Support::JobQueue::Command::QueueRunning',
        'getfinishedqueue'    => 'Support::JobQueue::Command::QueueFinished',
        'togglehold'          => 'Support::JobQueue::Command::ToggleHold',
        'togglepaused'        => 'Support::JobQueue::Command::TogglePaused',
        'rules'               => 'Support::JobQueue::Command::EditRules',
        'skip'                => 'Support::JobQueue::Command::SaveSkipRules',
        'run'                 => 'Support::JobQueue::Command::SaveRunRules',
        'show_processes'      => 'Support::JobQueue::Command::ShowDistributionProcesses',
        'show_process_detail' => 'Support::JobQueue::Command::ShowDistributionProcess',
        'requeue'             => 'Support::JobQueue::Command::RequeueJob',
        'host_class'          => 'Support::JobQueue::Command::SaveHostClasses',
    },

    'admin' => {
        'DEFAULT'             => 'Support::Blank',
        'delete_page'         => 'Support::Module::Command::DeletePage',
        'modules'             => 'Support::Module::Command::List',
        'module_detail'       => 'Support::Module::Command::Show',
        'page_detail'         => 'Support::Module::Command::ShowPage',
        'support_roles'       => 'Support::SupportRole::Command::List',
        'support_role_detail' => 'Support::SupportRole::Command::Show',
        'users'               => 'Support::User::Command::Search',
        'user_list'           => 'Support::User::Command::List',
        'user_detail'         => 'Support::User::Command::Show',
        'reset_mfa'           => 'Support::User::Command::ResetMFA',
    },

    'client' => {
        'DEFAULT'           => 'Support::Blank',
        'clients'           => 'Support::Client::Command::Search',
        'client_list'       => 'Support::Client::Command::List',
        'client_detail'     => 'Support::Client::Command::Show',
        'service_list'      => 'Support::Client::Command::ServiceList',
        'importers'         => 'Support::SaleImporter::Command::List',
        'bookpub_importers' => 'Support::SaleImporter::Command::BookPubList',
        'download_sample'   => 'Support::SaleImporter::Command::Download',
        'importer_toggle'   => 'Support::SaleImporter::Command::ImporterToggle',
    },

    'cm' => {
        'DEFAULT'                   => 'Support::Blank',
        'assign_service_account'    => 'Support::Client::Command::AssignServiceAccount',
        'services'                  => 'Support::Service::Command::List',
        'service_detail'            => 'Support::Service::Command::Show',
        'service_account_detail'    => 'Support::ServiceAccount::Command::Show',
        'service_credential_detail' => 'Support::ServiceCredential::Command::Show',
        'client_config'             => 'Support::Client::Command::DistributionShow',
        'image_files'               => 'Support::FileManagement::Command::ShowImages',
        'image_search'              => 'Support::FileManagement::Command::SearchImage',
        'audio_files'               => 'Support::FileManagement::Command::ShowAudio',
        'audio_search'              => 'Support::FileManagement::Command::SearchAudio',
        'delete_image'              => 'Support::FileManagement::Command::DeleteImage',
        'delete_audio'              => 'Support::FileManagement::Command::DeleteAudio',
        'rename_file'               => 'Support::FileManagement::Command::RenameFile',
        'mark_distribute'           => 'Support::ProductDistribution::Command::MarkDistribution',
        'ajax_mark_distribute'      => 'Support::ProductDistribution::Command::AjaxMarkDistribution',
        'redeliver'                 => 'Support::ProductDistribution::Command::RedeliverSearch',
        'redeliver_list'            => 'Support::ProductDistribution::Command::RedeliverList',
        'redeliver_detail'          => 'Support::ProductDistribution::Command::Redeliver',
        'redeliver_job'             => 'Support::ProductDistribution::Command::RedeliverJob',
        'genre_map'                 => 'Support::GenreMap::Command::Show',
        'import_genre_map'          => 'Support::GenreMap::Command::Import',
        'export_genre_map'          => 'Support::GenreMap::Command::Export',
        'artist_role_map'           => 'Support::ArtistRoleMap::Command::Show',
        'import_artist_role_map'    => 'Support::ArtistRoleMap::Command::Import',
        'export_artist_role_map'    => 'Support::ArtistRoleMap::Command::Export',
    },

    'reports' => {
        'search_delivery_failures' => 'Support::Reports::Command::QueryDeliveryFailures',
        'search_albums'            => 'Support::Reports::Command::AlbumSearch',
        'album_count'              => 'Support::Reports::Command::AlbumCount',
        'pending_media'            => 'Support::Reports::Command::PendingMediaFiles',
        'sale_exceptions'          => 'Support::Reports::Command::SaleExceptions',
        'label_without_contracts'  => 'Support::Reports::Command::LabelWithoutContracts',
        'royalty_run_status'       => 'Support::Reports::Command::ArtistRoyaltyRunStatus',
        'artist_run_status'        => 'Support::Reports::Command::ArtistRoyaltyRunStatus',
        'label_run_status'         => 'Support::Reports::Command::LabelRoyaltyRunStatus',
        'mechanical_run_status'    => 'Support::Reports::Command::MechanicalRunStatus',
        'ca_mechanical_run_status' => 'Support::Reports::Command::CAMechanicalRunStatus',
        'uk_mechanical_run_status' => 'Support::Reports::Command::UKMechanicalRunStatus',
        'closed_periods'           => 'Support::Reports::Command::ClosedPeriods',
        'search_users'             => 'Support::Reports::Command::UserSearch',
        'orchard_sales'            => 'Support::Reports::Command::OrchardSales',
    },
    'messaging' => {
        'news_moderation'  => 'Support::News::Command::List',
        'toggleapproved'   => 'Support::News::Command::ToggleApproved',
        'message_detail'   => 'Support::Messaging::Command::Edit',
        'dashboard_update' => 'Support::Messaging::Command::DashboardUpdate',
        'dashboard_delete' => 'Support::Messaging::Command::DashboardUpdate',
        'system_alert'     => 'Support::Messaging::Command::SystemAlert',
        'system_delete'    => 'Support::Messaging::Command::SystemAlert',
    },
    'bookpub' => {
        'service_settings'          => 'Support::BookPub::Command::Services',
        'pos_feed'                  => 'Support::BookPub::Command::POSFeed',
        'retry_pos'                 => 'Support::BookPub::Command::POSRetry',
        'saveas'                    => 'Support::BookPub::Command::DownloadFile',
        'service_credential_detail' => 'Support::BookPub::Command::ServiceCredentialDetail',
    },
    'import' => {
        'DEFAULT'  => 'Support::DataImport::Command::Main',
        'download' => 'Support::DataImport::Command::Download',
        'upload'   => 'Support::DataImport::Command::Main',
        'finish'   => 'Support::DataImport::Command::FinishImport',
    },
);

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

    return \%gCommandMap;
}

sub CreateCommand {
    my ( $pkg, $cgi ) = @_;

    my $commandObj;

    if ( Support::Command->_area && Support::Command->_area ne 'DEFAULT' ) {
        $commandObj = $pkg->SUPER::CreateCommand($cgi);
    } else {
        my $cmdPkg = $gCommandMap{DEFAULT}{DEFAULT};

        eval "require $cmdPkg";
        die $@ if $@;

        $commandObj = $cmdPkg->new( cgi => $cgi );
    }

    Common::Log::Debug( "Created Command: " . ref($commandObj) );
    return $commandObj;
}
1;
