<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://wp-deployer.io
 * @since      1.0.0
 *
 * @package    EaLab\WP_Deployer
 * @subpackage WP_Deployer/admin
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Deploy_Sync_Content__Admin {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string $plugin_name The ID of this plugin.
	 */
	private string $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string $version The current version of this plugin.
	 */
	private string $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @param string $plugin_name The name of this plugin.
	 * @param string $version The version of this plugin.
	 *
	 * @since    1.0.0
	 */
	public function __construct( string $plugin_name, string $version ) {
		$this->plugin_name = $plugin_name;
		$this->version     = $version;
	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles( $hook ) {
		if ( $hook === 'toplevel_page_deploy-sync-content' ) {
			wp_enqueue_style( 'bootstrap_523_css', plugin_dir_url( __FILE__ ) . 'assets/bootstrap/css/bootstrap.min.css' );
			wp_enqueue_style( 'bootstrap_icons_css', plugin_dir_url( __FILE__ ) . 'assets/icons/bootstrap-icons.min.css' );
			wp_enqueue_style( 'tempus_dominus_6713_css', plugin_dir_url( __FILE__ ) . 'assets/tempus-dominus/css/tempus-dominus.min.css' );
			wp_enqueue_style( 'select2_css', plugin_dir_url( __FILE__ ) . 'assets/select2/select2.min.css' );
			wp_enqueue_style( 'select2_bootstrap_css', plugin_dir_url( __FILE__ ) . 'assets/select2/select2-bootstrap-5-theme.min.css' );
		}

		wp_enqueue_style( $this->plugin_name . '-style', plugin_dir_url( __FILE__ ) . 'assets/css/plugin-style.css', array(), $this->version, 'all' );
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts( $hook ) {
		if ( $hook === 'toplevel_page_deploy-sync-content' ) {
			wp_enqueue_script( 'bootstrap_523_js', plugin_dir_url( __FILE__ ) . 'assets/bootstrap/js/bootstrap.bundle.min.js' );
			wp_enqueue_script( 'popper_js', plugin_dir_url( __FILE__ ) . 'assets/js/popper.min.js' );
			wp_enqueue_script( 'tempus_dominus_js', plugin_dir_url( __FILE__ ) . 'assets/tempus-dominus/js/tempus-dominus.min.js' );
			wp_enqueue_script( 'select2_js', plugin_dir_url( __FILE__ ) . 'assets/select2/select2.min.js' );
		}
		wp_enqueue_script( $this->plugin_name . '-post-form-data-as-json', plugin_dir_url( __FILE__ ) . 'assets/js/post-form-data-as-json.js', array( 'jquery' ), $this->version, false );
		wp_enqueue_script( $this->plugin_name . '-script', plugin_dir_url( __FILE__ ) . 'assets/js/plugin-script.js', array( 'jquery' ), $this->version, false );
	}

	/**
	 * Add to the menu sidebar
	 *
	 * @since    1.0.0
	 */
	public function initMenuPluginPage() {
		add_action(
			'admin_menu',
			static function () {
				global $menu;

				// Find the position of the "Settings" menu.
				$settings_position = 0;

				foreach ( $menu as $order => $item ) {
					if ( 'options-general.php' === $item[2] ) {
						$settings_position = $order;
						break;
					}
				}

				// Calculate the position just before the "Settings" menu.
				$dsc_position = $settings_position - 1;

				// Add a new menu item to the admin menu before Settings.
				add_menu_page(
					'Deploy & Sync Content',
					'Deploy & Sync Content',
					'manage_options',
					'deploy-sync-content',
					static function () {
						include_once __DIR__ . '/partials/deploy-sync-content-admin-display.php';
					},
					DEPLOY_SYNC_CONTENT__PLUGIN_URL . 'admin/assets/img/Logo-WPD-blanc-icone-menu-y-20.svg',
					$dsc_position
				);
			}
		);

		include_once __DIR__ . '/menuless-mode.php';
	}
}
