<?php
namespace SPAATG;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use SPAATG\ShortPixelLogger\ShortPixelLogger as Log;
use SPAATG\Notices\NoticeController as Notices;
use SPAATG\Controller\QueueController as QueueController;
use SPAATG\Controller\QuotaController as QuotaController;
use SPAATG\Controller\AjaxController as AjaxController;
use SPAATG\Controller\AdminController as AdminController;
use SPAATG\Controller\ApiKeyController as ApiKeyController;
use SPAATG\Controller\FileSystemController;
use SPAATG\Controller\Optimizer\OptimizeAiController;

use SPAATG\Helper\InstallHelper as InstallHelper;
use SPAATG\Helper\UiHelper as UiHelper;

use SPAATG\Model\AccessModel as AccessModel;
use SPAATG\Model\SettingsModel as SettingsModel;

/** Plugin class
 * This class is meant for: WP Hooks, init of runtime and Controller Routing.
 */
class ShortPixelPlugin {

	private static $instance;
	protected static $modelsLoaded = array(); // don't require twice, limit amount of require looksups..

	protected $is_noheaders = false;

	protected $plugin_path;
	protected $plugin_url;

	protected $shortPixel; // shortpixel megaclass

	protected $admin_pages = array();  // admin page hooks.

	public function __construct() {
		// $this->initHooks();
		add_action( 'plugins_loaded', [$this, 'lowInit'], 5 ); // early as possible init.

	}

	/** LowInit after all Plugins are loaded. Core WP function can still be missing. This should mostly add hooks */
	public function lowInit() {

		$this->plugin_path = plugin_dir_path( SPAATG_PLUGIN_FILE );
		$this->plugin_url  = plugin_dir_url( SPAATG_PLUGIN_FILE );

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended  -- This is not a form.
		$noheader = isset( $_REQUEST['noheader'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['noheader'] ) ) : null;
		if ( ! is_null( $noheader ) ) {
			$this->is_noheaders = true;
		}

		/*
		Filter to prevent SPAATG from starting. This can be used by third-parties to prevent init when needed for a particular situation.
		* Hook into plugins_loaded with priority lower than 5 */
		$init = apply_filters( 'spaatg/plugin/init', true );

		if (false === $init ) {
			return;
		}


		$admin        = Controller\AdminController::getInstance();
		$adminNotices = Controller\AdminNoticesController::getInstance(); // Hook in the admin notices.

//		$this->initHooks();
		$this->ajaxHooks();

		add_action ('init', [$this, 'init']);
		add_action('init', [$this, 'initHooks']);
		add_action( 'admin_init', [ $this, 'admin_init' ] );
	}

	public function init()
	{
		Controller\CronController::getInstance();  // cron jobs - must be init to function!

		$access = AccessModel::getInstance();

		$isAdminUser = $access->userIsAllowed('is_admin_user');

		if ( $isAdminUser ) {
			// toolbar notifications

			// only if the key is not yet valid or the user hasn't bought any credits.
			// @todo This should not be done here.
			$settings     = $this->settings();
			$stats        = $settings->currentStats;
			$totalCredits = isset( $stats['APICallsQuotaNumeric'] ) ? $stats['APICallsQuotaNumeric'] + $stats['APICallsQuotaOneTimeNumeric'] : 0;
			$keyControl = ApiKeyController::getInstance();


			if ( true || false === $keyControl->keyIsVerified() || $totalCredits < 4000 ) {
				require_once 'class/view/shortpixel-feedback.php';
				new ShortPixelFeedback( SPAATG_PLUGIN_FILE, 'shortpixel-ai-alt-text-generator' );
			}
		}

	}


	/** Mainline Admin Init. Tasks that can be loaded later should go here */
	public function admin_init() {
			// This runs activation thing. Should be -after- init
			$this->check_plugin_version();


			$notices             = Notices::getInstance(); // This hooks the ajax listener
			$keyControl = ApiKeyController::getInstance();
			if ($keyControl->keyIsVerified()) {
				$quotaController = QuotaController::getInstance();
				$quotaController->getQuota();
			}

	}

	/** Function to get plugin settings
     *
     * @return SettingsModel The settings model object.
     */
	public function settings() {
			return SettingsModel::getInstance();
	}

	/** Function to get all enviromental variables
     *
     * @return EnvironmentModel
     */
	public function env() {
		return Model\EnvironmentModel::getInstance();
	}

	/** Get the SPAATG FileSystemController
	 *
	 * @return FileSystemController
	 */
	public function fileSystem() {
		return new Controller\FileSystemController();
	}

	/** Create instance. This should not be needed to call anywhere else than main plugin file
     * This should not be called *after* plugins_loaded action
     **/
	public static function getInstance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new ShortPixelPlugin();
		}
		return self::$instance;

	}

	/** Hooks for all WordPress related hooks
     * For now hooks in the lowInit, asap.
     */
	public function initHooks() {

		add_action( 'admin_menu', array( $this, 'admin_pages' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); // admin scripts
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_styles' ) ); // admin styles
		add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_scripts' ), 90 ); // loader via route.
		add_action( 'enqueue_block_assets', array($this, 'load_admin_scripts'), 90);
		// defer notices a little to allow other hooks ( notable adminnotices )

		// Media Library - Actions to route screen
		add_action( 'load-upload.php', array( $this, 'route' ) );
		add_action( 'load-post.php', array( $this, 'route' ) );

		$admin = AdminController::getInstance();

		add_action( 'wp_handle_replace', array( $admin, 'handleReplaceHook' ) );

		// Action / hook for who wants to use CRON. Please refer to manual / support to prevent loss of credits.
		add_action( 'spaatg/hook/processqueue', array( $admin, 'processQueueHook' ) );

		add_filter('bulk_actions-upload', array($admin, 'registerMediaBulkActions'));
		add_filter('handle_bulk_actions-upload', array($admin, 'handleMediaBulkActions'), 10, 3);
		add_action('admin_notices', array($admin, 'displayMediaBulkActionNotice'));

		$optimizeAiController = OptimizeAiController::getInstance();
		if (true === $optimizeAiController->isAutoAiEnabled())
		{
			add_action( 'enable-media-replace-upload-done', array( $admin, 'handleReplaceEnqueue' ), 10, 3 );

			// Run one hit earlier than optimization, to do this action first if needed.
			add_filter( 'wp_generate_attachment_metadata', array( $admin, 'handleAiImageUploadHook' ), 4, 2 );
			add_filter( 'mpp_generate_metadata', array( $admin, 'handleAiImageUploadHook' ), 9, 2 );
		}


		$this->env()->setDefaultViewModeList();// set default mode as list. only @ first run

		add_filter( 'plugin_action_links_' . plugin_basename( SPAATG_PLUGIN_FILE ), array( $admin, 'generatePluginLinks' ) );// for plugin settings page

		add_action( 'admin_bar_menu', array( $admin, 'toolbar_spaatg_processing' ), 999 );

		if ($this->env()->is_multisite)
		{
			 add_action('network_admin_menu', [$this, 'admin_network_pages']) ;
		}

	}

	protected function ajaxHooks() {

		// Ajax hooks. Should always be prepended with ajax_ and *must* check on nonce in function
		add_action( 'wp_ajax_spaatg_image_processing', array( AjaxController::getInstance(), 'ajax_processQueue' ) );

		add_action( 'wp_ajax_spaatg_check_quota', array( AjaxController::getInstance(), 'ajax_checkquota' ) );


		add_action( 'wp_ajax_spaatg_ajaxRequest', array( AjaxController::getInstance(), 'ajaxRequest' ) );
		add_action( 'wp_ajax_spaatg_settingsRequest', array( AjaxController::getInstance(), 'settingsRequest'));

	}

	/** Hook in our admin pages */
	public function admin_pages() {
		$admin_pages = array();
		// settings page
		$admin_pages[] = add_options_page( __( 'ShortPixel AI Alt Text Generator', 'shortpixel-ai-alt-text-generator' ), __( 'ShortPixel AI Alt Text Generator', 'shortpixel-ai-alt-text-generator' ), 'manage_options', 'spaatg-settings', array( $this, 'route' ) );

		/*translators: title and menu name for the Bulk Processing page*/
		$admin_pages[] = add_media_page( __( 'ShortPixel AI Alt Text Generator Bulk Process', 'shortpixel-ai-alt-text-generator' ), __( 'Bulk AI Alt Text', 'shortpixel-ai-alt-text-generator' ), 'edit_others_posts', 'spaatg-bulk', array( $this, 'route' ) );

		$this->admin_pages = $admin_pages;
	}

	public function admin_network_pages()
	{
	//	  	add_menu_page(__('Shortpixel MU', 'shortpixel-ai-alt-text-generator'), __('Shortpixel', 'shortpixel-ai-alt-text-generator'), 'manage_sites', 'spaatg-network-settings', [$this, 'route'], $this->plugin_url('res/img/shortpixel.png') );
	}

	/** All scripts should be registed, not enqueued here (unless global wp-admin is needed )
     *
     * Not all those registered must be enqueued however.
     */
	public function admin_scripts( $hook_suffix ) {

		$settings       = \wpSPAATG()->settings();
		$env = \wpSPAATG()->env();
		$ajaxController = AjaxController::getInstance();

		$secretKey = $ajaxController->getProcessorKey();

		$keyControl = \SPAATG\Controller\ApiKeyController::getInstance();
		$apikey     = $keyControl->getKeyForDisplay();

		$is_bulk_page = \wpSPAATG()->env()->is_bulk_page;

		$queueController = new QueueController(['is_bulk' =>  $is_bulk_page ]);
		$quotaController = QuotaController::getInstance();

		$OptimizeAiController = OptimizeAiController::getInstance();

		wp_register_script( 'spaatg-jquery-knob', plugins_url( '/res/js/jquery.knob.min.js', SPAATG_PLUGIN_FILE ), array(), SPAATG_IMAGE_OPTIMISER_VERSION, true );

		wp_register_script( 'spaatg-debug', plugins_url( '/res/js/debug.js', SPAATG_PLUGIN_FILE ), array( 'jquery', 'jquery-ui-draggable' ), SPAATG_IMAGE_OPTIMISER_VERSION, true );

		wp_register_script( 'spaatg-tooltip', plugins_url( '/res/js/shortpixel-tooltip.js', SPAATG_PLUGIN_FILE ), array( 'jquery' ), SPAATG_IMAGE_OPTIMISER_VERSION, true );

		$tooltip_localize = array(
			'processing' => __('Processing... ','shortpixel-ai-alt-text-generator'),
			'pause' =>  __('Click to pause', 'shortpixel-ai-alt-text-generator'),
			'resume' => __('Click to resume', 'shortpixel-ai-alt-text-generator'),
			'item' => __('item in queue', 'shortpixel-ai-alt-text-generator'),
			'items' => __('items in queue', 'shortpixel-ai-alt-text-generator'),
		);

		wp_localize_script( 'spaatg-tooltip', 'spaatg_tooltipStrings', $tooltip_localize);

		wp_register_script( 'spaatg-settings', plugins_url( 'res/js/shortpixel-settings.js', SPAATG_PLUGIN_FILE ), array('spaatg', 'spaatg-shiftselect', 'spaatg-inline-help', 'media-editor'), SPAATG_IMAGE_OPTIMISER_VERSION, true );

		wp_register_script('spaatg-shiftselect', plugins_url('res/js/shift-select.js', SPAATG_PLUGIN_FILE), array(), SPAATG_IMAGE_OPTIMISER_VERSION, true);

		wp_localize_script('spaatg-settings', 'spaatgSettingsStrings', UiHelper::getSettingsStrings(false));


		wp_register_script( 'spaatg-onboarding', plugins_url( 'res/js/shortpixel-onboarding.js', SPAATG_PLUGIN_FILE ), array('spaatg-settings'), SPAATG_IMAGE_OPTIMISER_VERSION, true );

		wp_register_script('spaatg-inline-help', plugins_url('res/js/shortpixel-inline-help.js',  SPAATG_PLUGIN_FILE), [], SPAATG_IMAGE_OPTIMISER_VERSION, true);

		wp_register_script( 'spaatg-processor', plugins_url( '/res/js/shortpixel-processor.js', SPAATG_PLUGIN_FILE ), array( 'jquery', 'spaatg-tooltip' ), SPAATG_IMAGE_OPTIMISER_VERSION, true );

		 // How often JS processor asks for next tick on server. Low for fastestness and high loads, high number for surviving servers.
		$interval = apply_filters( 'spaatg/processor/interval', 3000 );

		// If the queue is empty how often to check if something new appeared from somewhere. Excluding the manual items added by current processor user.
		$deferInterval = apply_filters( 'spaatg/process/deferInterval', 60000 );

		wp_localize_script(
            'spaatg-processor',
            'SPAATGProcessorData',
            array(
				'bulkSecret'        => $secretKey,
				'isBulkPage'        => (bool) $is_bulk_page,
				'workerURL'         => plugins_url( 'res/js/shortpixel-worker.js', SPAATG_PLUGIN_FILE ),
				'nonce_process'     => wp_create_nonce( 'processing' ),
				'nonce_exit'        => wp_create_nonce( 'exit_process' ),
				'nonce_ajaxrequest' => wp_create_nonce( 'ajax_request' ),
				'nonce_settingsrequest' => wp_create_nonce('settings_request'),
				'startData'         => ( \wpSPAATG()->env()->is_screen_to_use ) ? $queueController->getStartupData() : false,
				'interval'          => $interval,
				'deferInterval'     => $deferInterval,
				'debugIsActive' 		=> (\wpSPAATG()->env()->is_debug) ? 'true' : 'false',
				'autoMediaLibrary'  => ($settings->autoMediaLibrary) ? 'true' : 'false',
				'disable_processor' => apply_filters('spaatg/processorjs/disable', false),
            )
        );

		/*** SCREENS */
		wp_register_script('spaatg-screen-base', plugins_url( '/res/js/screens/screen-base.js', SPAATG_PLUGIN_FILE ), array( 'jquery', 'spaatg-processor' ), SPAATG_IMAGE_OPTIMISER_VERSION, true );

		wp_register_script('spaatg-screen-item-base', plugins_url( '/res/js/screens/screen-item-base.js', SPAATG_PLUGIN_FILE ), array( 'jquery', 'spaatg-processor', 'spaatg-screen-base'), SPAATG_IMAGE_OPTIMISER_VERSION, true );

		wp_register_script( 'spaatg-screen-media', plugins_url( '/res/js/screens/screen-media.js', SPAATG_PLUGIN_FILE ), array( 'jquery', 'spaatg-processor', 'spaatg-screen-base', 'spaatg-screen-item-base' ), SPAATG_IMAGE_OPTIMISER_VERSION, true );

		wp_register_script( 'spaatg-screen-nolist', plugins_url( '/res/js/screens/screen-nolist.js', SPAATG_PLUGIN_FILE ), array( 'jquery', 'spaatg-processor', 'spaatg-screen-base' ), SPAATG_IMAGE_OPTIMISER_VERSION, true );

	  $screen_localize = array(  // Item Base
			'startAction' => __('Processing... ','shortpixel-ai-alt-text-generator'),
			'startActionAI' => __('Generating image SEO data', 'shortpixel-ai-alt-text-generator'),
			'fatalError' => __('ShortPixel encountered a fatal error while generating AI SEO data. Please check the issue below. If this is caused by a bug please contact our support', 'shortpixel-ai-alt-text-generator'),
			'fatalErrorStop' => __('ShortPixel has encounted multiple errors and has now stopped processing', 'shortpixel-ai-alt-text-generator'),
			'fatalErrorStopText' => __('No items are being processed. To try again after solving the issues, please reload the page ', 'shortpixel-ai-alt-text-generator'),
			'fatalError500' => __('A fatal error HTTP 500 has occurred. On the bulk screen, this may be caused by the script running out of memory. Check your error log, increase memory or disable heavy plugins.', 'shortpixel-ai-alt-text-generator'),

		);


	 $screen_localize_media = [
			'hide_ai' => ! $OptimizeAiController->isAiEnabled(),  // turn around negative setting
			'hide_spaatg_in_popups' => apply_filters('spaatg/js/media/hide_in_popups', false),
			'alt_text_label' => __('Alt Text', 'shortpixel-ai-alt-text-generator'),
			'alt_text_empty' => __('No alt text available yet.', 'shortpixel-ai-alt-text-generator'),
			'alt_text_loading' => __('Loading alt text...', 'shortpixel-ai-alt-text-generator'),
			'wp_screen_id' => $env->screen_id,
	 ];

		wp_localize_script('spaatg-screen-media', 'spaatg_mediascreen_settings', $screen_localize_media);

		wp_localize_script( 'spaatg-screen-base', 'spaatg_screenStrings', $screen_localize);

		wp_register_script( 'spaatg-screen-bulk', plugins_url( '/res/js/screens/screen-bulk.js', SPAATG_PLUGIN_FILE ), array( 'jquery', 'spaatg-processor', 'spaatg-screen-base'), SPAATG_IMAGE_OPTIMISER_VERSION, true );

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended  -- This is not a form
		$panel = isset( $_GET['panel'] ) ? sanitize_text_field( wp_unslash($_GET['panel']) ) : false;

		$bulkLocalize = [
			'endBulk'   => __( 'This will stop the bulk processing and take you back to the start. Are you sure you want to do this?', 'shortpixel-ai-alt-text-generator' ),
			'reloadURL' => admin_url( 'upload.php?page=spaatg-bulk'),
		];
		if ( $panel ) {
			$bulkLocalize['panel'] = $panel;
        }

		// screen translations. Can all be loaded on the same var, since only one screen can be active.
		wp_localize_script( 'spaatg-screen-bulk', 'spaatgScreen', $bulkLocalize );

		wp_register_script( 'spaatg', plugins_url( '/res/js/shortpixel.js', SPAATG_PLUGIN_FILE ), array( 'jquery', 'spaatg-jquery-knob' ), SPAATG_IMAGE_OPTIMISER_VERSION, true );

		// Using an Array within another Array to protect the primitive values from being cast to strings
		$SPAATGConstants = array(
			array(
				'WP_PLUGIN_URL'     => plugins_url( '', SPAATG_PLUGIN_FILE ),
				'WP_ADMIN_URL'      => admin_url(),
				'API_IS_ACTIVE'     => $keyControl->keyIsVerified(),
				'AJAX_URL'          => admin_url( 'admin-ajax.php' ),
				'BULK_SECRET'       => $secretKey,
				'nonce_ajaxrequest' => wp_create_nonce( 'ajax_request' ),
				'HAS_QUOTA'         => ( $quotaController->hasQuota() ) ? 1 : 0,

			),
		);

		if ( Log::isManualDebug() ) {
			Log::addInfo( 'Ajax Manual Debug Mode' );
			$logLevel                           = Log::getLogLevel();
			$SPAATGConstants[0]['AJAX_URL'] = admin_url( 'admin-ajax.php?SPAATG_DEBUG=' . $logLevel );
		}

		$jsTranslation = array(
			'generateAI' 				  => __( 'Generate image SEO data', 'shortpixel-ai-alt-text-generator'),
			'loading'                     => __( 'Loading...', 'shortpixel-ai-alt-text-generator' ),

		);

		wp_localize_script( 'spaatg', 'spaatgTr', $jsTranslation );
		wp_localize_script( 'spaatg', 'SPAATGConstants', $SPAATGConstants );

	}

	public function admin_styles() {

		// notices. additional styles for SPAATG.
		wp_register_style( 'spaatg-notices', plugins_url( '/res/css/shortpixel-notices.css', SPAATG_PLUGIN_FILE ), array( 'spaatg-admin' ), SPAATG_IMAGE_OPTIMISER_VERSION );

		wp_register_style('spaatg-notices-module', plugins_url('/build/shortpixel/notices/src/css/notices.css', SPAATG_PLUGIN_FILE), array(), SPAATG_IMAGE_OPTIMISER_VERSION);

		// load everywhere, because we are inconsistent.
		wp_register_style( 'spaatg-toolbar', plugins_url( '/res/css/shortpixel-toolbar.css', SPAATG_PLUGIN_FILE ), array( 'dashicons' ), SPAATG_IMAGE_OPTIMISER_VERSION );

		// @todo Might need to be removed later on
		wp_register_style( 'spaatg-admin', plugins_url( '/res/css/shortpixel-admin.css', SPAATG_PLUGIN_FILE ), array(), SPAATG_IMAGE_OPTIMISER_VERSION );

		wp_register_style( 'spaatg-bulk', plugins_url( '/res/css/shortpixel-bulk.css', SPAATG_PLUGIN_FILE ), array(), SPAATG_IMAGE_OPTIMISER_VERSION );

		wp_register_style( 'spaatg-settings', plugins_url( '/res/css/shortpixel-settings.css', SPAATG_PLUGIN_FILE ), array(), SPAATG_IMAGE_OPTIMISER_VERSION );
	}


	/** Load Style via Route, on demand */
	public function load_style( $name ) {
		if ( $this->is_noheaders ) {  // fail silently, if this is a no-headers request.
			return;
		}

		if ( wp_style_is( $name, 'registered' ) ) {
			wp_enqueue_style( $name );
		} else {
			$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
			Log::addWarn( "Style $name was asked for, but not registered", $request_uri );
		}
	}

	/** Load Style via Route, on demand */
	public function load_script( $script ) {
		if ( $this->is_noheaders ) {  // fail silently, if this is a no-headers request.
			return;
		}

		if ( ! is_array( $script ) ) {
			$script = array( $script );
		}

		foreach ( $script as $index => $name ) {
			if ( wp_script_is( $name, 'registered' ) ) {
				wp_enqueue_script( $name );
			} else {
				$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
				Log::addWarn( "Script $name was asked for, but not registered", $request_uri  );
			}
		}
	}

	/** This is separated from route to load in head, preventing unstyled content all the time */
	 public function load_admin_scripts( $hook_suffix ) {
		global $plugin_page;
		$screen_id = $this->env()->screen_id;

		$load_processor = array( 'spaatg', 'spaatg-processor' );  // a whole suit needed for processing, not more. Always needs a screen as well!
		$load_bulk      = array();  // the whole suit needed for bulking.
		if ( \wpSPAATG()->env()->is_screen_to_use ) {
			$this->load_script( $load_processor );
			$this->load_style( 'spaatg-toolbar' );
			$this->load_style('spaatg-notices');
			$this->load_style('spaatg-notices-module');
		}

		if ( $plugin_page == 'spaatg-settings' || $plugin_page == 'spaatg-network-settings' ) {
			wp_enqueue_media();
			$this->load_script( $load_processor );

			$this->load_script( 'spaatg-screen-nolist' ); // screen
			$this->load_script( 'spaatg-settings' );

			// @todo Load onboarding only when no api key / onboarding required
			$this->load_script('spaatg-onboarding');

			$this->load_style( 'spaatg-admin' );

			$this->load_style( 'spaatg-settings' );

		} elseif ( $plugin_page == 'spaatg-bulk' ) {
			$this->load_script( 'spaatg-screen-bulk' );

			$this->load_style( 'spaatg-admin' );
			$this->load_style( 'spaatg-bulk' );
		} elseif ( $screen_id == 'upload' || $screen_id == 'attachment' ) {

			$this->load_script( 'spaatg-screen-media' ); // screen

			$this->load_style( 'spaatg-admin' );
			$this->load_style( 'spaatg-notices-module');
			if ( $this->env()->is_debug ) {
				$this->load_script( 'spaatg-debug' );
			}
		} elseif (true === $this->env()->is_gutenberg_editor || true === $this->env()->is_classic_editor)
		{
			$this->load_script( $load_processor );
			$this->load_script( 'spaatg-screen-media' ); // screen

			$this->load_style( 'spaatg-admin' );
		}
		elseif (true === \wpSPAATG()->env()->is_screen_to_use  )
		{
			// If our screen, but we don't have a specific handler for it, do the no-list screen.
			$this->load_script( 'spaatg-screen-nolist' ); // screen
		}

	}

	/** Route, based on the page slug
     *
     * Principially all page controller should be routed from here.
     */
	public function route() {
		global $plugin_page;

		$default_action = 'load'; // generic action on controller.
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended  -- This is not a form
		$action         = isset( $_REQUEST['sp-action'] ) ? sanitize_text_field( wp_unslash($_REQUEST['sp-action']) ) : $default_action;
		$controller = false;

		$url       = menu_page_url( $plugin_page, false );
		$screen_id = \wpSPAATG()->env()->screen_id;

        switch ( $plugin_page ) {
            case 'spaatg-settings': // settings
						$controller = 'SPAATG\Controller\View\SettingsViewController';
						wp_enqueue_media();
        	break;
            case 'spaatg-bulk':
						$controller = '\SPAATG\Controller\View\BulkViewController';
           break;
           case null:
            default:
                switch ( $screen_id ) {
					case 'upload':
                  $controller = '\SPAATG\Controller\View\ListMediaViewController';
                        break;
					case 'attachment': // edit-media
                   $controller = '\SPAATG\Controller\View\EditMediaViewController';
                     break;
                }
                break;

		}
		if ( $controller !== false ) {
			$c = $controller::getInstance();
			$c->setControllerURL( $url );
			if ( method_exists( $c, $action ) ) {
				$c->$action();
			} else {
				Log::addWarn( "Attempted Action $action on $controller does not exist!" );
				$c->$default_action();
			}
		}
	}


	// Get the plugin URL, based on real URL.
	public function plugin_url( $urlpath = '' ) {
		$url = trailingslashit( $this->plugin_url );
		if ( strlen( $urlpath ) > 0 ) {
			$url .= $urlpath;
		}
		return $url;
	}

	// Get the plugin path.
	public function plugin_path( $path = '' ) {
		$plugin_path = trailingslashit( $this->plugin_path );
		if ( strlen( $path ) > 0 ) {
			$plugin_path .= $path;
		}

		return $plugin_path;
	}

	/** Returns defined admin page hooks. Internal use - check states via environmentmodel
     *
     * @returns Array
     */
	public function get_admin_pages() {
		return $this->admin_pages;
	}

	protected function check_plugin_version() {
      $version     = SPAATG_IMAGE_OPTIMISER_VERSION;
			$db_version = $this->settings()->currentVersion;

		if ( $version !== $db_version ) {
			InstallHelper::activatePlugin();
			$this->settings()->currentVersion = $version;

		}
	}




} // class plugin
