<?php
/**
 * Admin menu
 *
 * @category   Admin
 * @package    CodexShaper_Framework
 * @author     CodexShaper <info@codexshaper.com>
 * @license    https://www.gnu.org/licenses/gpl-2.0.html
 * @link       https://codexshaper.com
 * @since      1.0.0
 */

namespace CodexShaper\Framework\Admin;

use CodexShaper\Framework\Foundation\Traits\Hook;

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

/**
 * Admin menu class
 *
 * @category   Class
 * @package    CodexShaper_Framework
 * @author     CodexShaper <info@codexshaper.com>
 * @license    https://www.gnu.org/licenses/gpl-2.0.html
 * @link       https://codexshaper.com
 * @since      1.0.0
 */
class Menu {
	use Hook;

	/**
	 * Instance
	 *
	 * @var \CodexShaper\Framework\Admin\Menu
	 * @static
	 * @since 1.0.0
	 */
	private static $instance;

	/**
	 * Modules
	 *
	 * @var \CodexShaper\Framework\Admin\Serttings
	 * @static
	 * @since 1.0.0
	 */
	private $modules;

	/**
	 * Settings
	 *
	 * @var \CodexShaper\Framework\Admin\Serttings
	 * @static
	 * @since 1.0.0
	 */
	private $settings;

	/**
	 * Constructor
	 *
	 * Admin menu register.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function __construct() {
		$this->settings = Settings::instance();
		$this->add_action( 'admin_menu', 'add_menu_pages', 9 );
		$this->add_action( 'admin_menu', 'set_menu_order', 10 );
	}

	/**
	 * Instance
	 *
	 * Ensures only one instance of the class is loaded or can be loaded.
	 *
	 * @since 1.0.0
	 * @access public
	 * @static
	 *
	 * @return \CodexShaper\Framework\Admin\Menu An instance of the class.
	 */
	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Theme admin menu page
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function add_menu_pages() {
		// Check user capability.
		if ( ! current_user_can( 'edit_posts', get_current_user_id() ) ) {
			return;
		}

		$csmf_icon = CSMF_URL . 'assets/images/icon.png';
		// Add menu page.
		add_menu_page(
			esc_html__( 'CodexShaper Framework Page', 'codexshaper-framework' ),
			esc_html__( 'CodexShaper Framework', 'codexshaper-framework' ),
			'manage_options',
			'codexshaper-framework',
			array( $this, 'render' ),
			$csmf_icon,
			'10'
		);

		add_submenu_page(
			'codexshaper-framework',
			__( 'Dashboard', 'codexshaper-framework' ),
			__( 'Dashboard', 'codexshaper-framework' ),
			'manage_options',
			'codexshaper-framework-dashboard',
			array( $this, 'render_dashboard' )
		);
		add_submenu_page(
			'codexshaper-framework',
			__( 'Settings', 'codexshaper-framework' ),
			__( 'Settings', 'codexshaper-framework' ),
			'manage_options',
			'codexshaper-framework-settings',
			array( $this->settings, 'render' ),
			1000,
		);
	}

	/**
	 * Unset sub menu
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function render() {
		?>
	<h2 class="csmf--page-title"><?php echo esc_html( get_admin_page_title() ); ?></h2>
		<?php
	}

	/**
	 * Render Dashboard Content
	 *
	 * @since 1.0.0
	 * @access public
	 * @return void
	 */
	public function render_dashboard() {
		csmf_view( 'admin.dashboard' );
	}

	/**
	 * Unset sub menu
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function set_menu_order() {
		global $submenu;
		if ( isset( $submenu['codexshaper-framework'] ) ) {
			$menus = $submenu['codexshaper-framework'];
			unset( $menus[0] );

			$settings_submenu = array();

			foreach ( $menus as $key => $menu ) {
				if ( isset( $menu[2] ) && $menu[2] === 'codexshaper-framework-settings' ) {
					$settings_submenu = $menu;
					unset( $menus[ $key ] );
				}
			}

			$menus[] = $settings_submenu;

			$submenu['codexshaper-framework'] = $menus;
		}
	}
}
