<?php
/**
 * Instance of Shared for a Plugin.
 *
 * @package northmill-online-shared
 *
 * @noinspection PhpUnused
 */

declare(strict_types=1);

namespace Northmill_Online_Shared;

use Northmill_Online_Shared\Modules\Info\Plugin_Info_Module;
use Northmill_Online_Shared\Modules\Uninstallers\Plugin_Uninstaller_Module;
use Northmill_Online_Shared\Modules\Updaters\Plugin_Updater_Module;

/**
 * Class Plugin_Shared
 */
abstract class Abstract_Plugin_Shared extends Abstract_Shared_Base {

	/**
	 * The instance of the plugin shared class.
	 *
	 * @var Abstract_Plugin_Shared
	 */
	protected static Abstract_Plugin_Shared $base_plugin_instance;

	/**
	 * The plugin info instance.
	 *
	 * @var Plugin_Info_Module
	 */
	public Plugin_Info_Module $info;

	/**
	 * The plugin uninstaller instance.
	 *
	 * @var Plugin_Uninstaller_Module
	 */
	public Plugin_Uninstaller_Module $uninstaller;

	/**
	 * Get shared plugin instance.
	 */
	public static function base_plugin_instance(): Abstract_Plugin_Shared {
		return self::$base_plugin_instance;
	}

	/**
	 * Plugin_Shared constructor.
	 */
	final public function __construct() {
		self::$base_plugin_instance = $this;
		parent::__construct();

		$this->setup_hooks( Plugin_Info_Module::generate_underscored_service_name() );

		$this->info         = new Plugin_Info_Module( $this );
		$this->package_info = $this->info;

		$this->uninstaller = new Plugin_Uninstaller_Module( $this );
		$this->updater     = new Plugin_Updater_Module( $this );

		$this->post_construct();

		add_action( 'plugins_loaded', array( $this, 'setup' ), 1, 0 );

		register_activation_hook(
			$this->package_info->get_file_path(),
			array( $this, 'plugin_activation' )
		);

		register_deactivation_hook(
			$this->package_info->get_file_path(),
			array( $this, 'plugin_deactivation' )
		);

		register_uninstall_hook(
			$this->package_info->get_file_path(),
			array( Plugin_Uninstaller_Module::class, 'plugin_uninstall' )
		);
	}

	/**
	 * Code run on 'plugins_loaded'.
	 *
	 * @return void
	 */
	final protected function internal_setup(): void {}

	/**
	 * Runs on plugin activation.
	 */
	final public function plugin_activation(): void {
		$this->package_activation->package_activation();
	}

	/**
	 * Runs on plugin deactivation.
	 */
	final public function plugin_deactivation(): void {
		$this->package_activation->package_deactivation();
	}
}
