<?php
/**
 * Admin registration handler.
 *
 * @package JWP_STK
 */

namespace JWP_STK\Admin;

defined( 'ABSPATH' ) || exit;

/**
 * Register admin menus and assets.
 */
class Register {

	/**
	 * Class instance.
	 *
	 * @var Register
	 */
	protected static $instance;

	/**
	 * REST API instance.
	 *
	 * @var REST_API
	 */
	public $rest_api;

	/**
	 * Get an instance of class.
	 *
	 * @return Register
	 */
	public static function get_instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'admin_menu', array( $this, 'register_menu' ) );
		add_action( 'admin_bar_menu', array( $this, 'add_toolbar_menu' ), 100 );
		add_action( 'wp_head', array( $this, 'add_toolbar_styles' ) );
		add_action( 'admin_head', array( $this, 'add_toolbar_styles' ) );

		$this->rest_api = REST_API::get_instance();
	}

	/**
	 * Register plugin menu.
	 *
	 * @return void
	 */
	public function register_menu() {
		$menu_icon = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDgiIGhlaWdodD0iNDgiIHZpZXdCb3g9IjAgMCA0OCA0OCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTQzLjIgMEg0LjhDMi4xNDkwMyAwIDAgMi4xNDkwMyAwIDQuOFY0My4yQzAgNDUuODUxIDIuMTQ5MDMgNDggNC44IDQ4SDQzLjJDNDUuODUxIDQ4IDQ4IDQ1Ljg1MSA0OCA0My4yVjQuOEM0OCAyLjE0OTAzIDQ1Ljg1MSAwIDQzLjIgMFoiIGZpbGw9IiMzMzZBNDQiIGZpbGwtb3BhY2l0eT0iMC4xIi8+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMzAuMDU2NyAzOC40MDAxQzI2LjAzNTIgMzguMzk0NiAyMi4wMTM4IDM4LjM4OSAxNy45OTI0IDM4LjM4MzRDMTcuNTk4MSAzMy4zMTQzIDE0LjgxOSAzMC41Mjc2IDkuNjQ1NDcgMzAuMDMyNEM5LjYzMDIgMjYuMDM0NCA5LjYxNSAyMi4wMzY2IDkuNTk5NzMgMTguMDM4N0MxNS4zMzI3IDE3LjEyMjMgMTcuNzA4NCAxNC4wMjQ1IDE4LjA0MjIgOS42MDAxQzIyLjAxNjYgOS42MDU1NyAyNS45OTExIDkuNjExMSAyOS45NjU1IDkuNjE2NjFDMzAuNzYxOSAxNC43MzE1IDMzLjUyOCAxNy41OTAxIDM4LjM5NTUgMTguMDQyNEwzOC4zOTk3IDMwLjA0MDFMMzAuMDU2NyAzMC4wMzk5VjM4LjQwMDFaTTE3Ljg1OTggMzAuMDg5OEgzMC4wNDgyVjE3LjcwMDdIMTcuODU5OFYzMC4wODk4WiIgZmlsbD0id2hpdGUiLz4KPC9zdmc+Cg==';

		add_menu_page(
			__( 'Store Toolkit', 'jetix-store-toolkit' ),
			__( 'Store Toolkit', 'jetix-store-toolkit' ),
			'manage_options',
			'jetix-store-toolkit',
			array( $this, 'render_dashboard' ),
			$menu_icon,
			57
		);

		// Dashboard — first submenu replaces parent link.
		add_submenu_page(
			'jetix-store-toolkit',
			__( 'Dashboard — Store Toolkit', 'jetix-store-toolkit' ),
			__( 'Dashboard', 'jetix-store-toolkit' ),
			'manage_options',
			'jetix-store-toolkit',
			array( $this, 'render_dashboard' )
		);

		add_submenu_page(
			'jetix-store-toolkit',
			__( 'Modules — Store Toolkit', 'jetix-store-toolkit' ),
			__( 'Modules', 'jetix-store-toolkit' ),
			'manage_options',
			'jwp-stk-modules',
			array( $this, 'render_dashboard' )
		);

		add_submenu_page(
			'jetix-store-toolkit',
			__( 'Blocks — Store Toolkit', 'jetix-store-toolkit' ),
			__( 'Blocks', 'jetix-store-toolkit' ),
			'manage_options',
			'jwp-stk-blocks',
			array( $this, 'render_dashboard' )
		);

		add_submenu_page(
			'jetix-store-toolkit',
			__( 'Global Settings — Store Toolkit', 'jetix-store-toolkit' ),
			__( 'Global Settings', 'jetix-store-toolkit' ),
			'manage_options',
			'jwp-stk-settings',
			array( $this, 'render_dashboard' )
		);
	}

	/**
	 * Render the dashboard page (React app mount point).
	 *
	 * @return void
	 */
	public function render_dashboard() {
		Admin_Dashboard::output();
	}

	/**
	 * Add Store Toolkit dropdown to the WP admin toolbar.
	 *
	 * @param \WP_Admin_Bar $wp_admin_bar Admin bar instance.
	 * @return void
	 */
	public function add_toolbar_menu( $wp_admin_bar ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$icon_svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" fill="none" aria-hidden="true">'
			. '<path fill-rule="evenodd" clip-rule="evenodd" d="M30.0567 38.4001C26.0352 38.3946 22.0138 38.389 17.9924 38.3834C17.5981 33.3143 14.819 30.5276 9.64547 30.0324C9.6302 26.0344 9.615 22.0366 9.59973 18.0387C15.3327 17.1223 17.7084 14.0245 18.0422 9.6001C22.0166 9.60557 25.9911 9.6111 29.9655 9.61661C30.7619 14.7315 33.528 17.5901 38.3955 18.0424L38.3997 30.0401L30.0567 30.0399V38.4001ZM17.8598 30.0898H30.0482V17.7007H17.8598V30.0898Z" fill="currentColor"/>'
			. '</svg>';

		$kses_svg = array(
			'svg'  => array(
				'xmlns'       => array(),
				'viewbox'     => array(),
				'fill'        => array(),
				'aria-hidden' => array(),
			),
			'path' => array(
				'd'         => array(),
				'fill'      => array(),
				'fill-rule' => array(),
				'clip-rule' => array(),
			),
		);

		$wp_admin_bar->add_node(
			array(
				'id'    => 'jwp-stk',
				'title' => '<span class="jwp-stk-ab-icon">' . wp_kses( $icon_svg, $kses_svg ) . '</span>'
					. '<span class="ab-label">' . esc_html__( 'Store Toolkit', 'jetix-store-toolkit' ) . '</span>',
				'href'  => admin_url( 'admin.php?page=jetix-store-toolkit' ),
				'meta'  => array( 'class' => 'jwp-stk-toolbar-item' ),
			)
		);

		$module_manager = \JWP_STK\Module_Manager::get_instance();
		$modules        = $module_manager->get_modules();
		$active_modules = $module_manager->get_active_modules();

		foreach ( $active_modules as $slug ) {
			if ( ! isset( $modules[ $slug ] ) || empty( $modules[ $slug ]['enabled'] ) ) {
				continue;
			}
			$wp_admin_bar->add_node(
				array(
					'parent' => 'jwp-stk',
					'id'     => 'jwp-stk-module-' . sanitize_html_class( $slug ),
					'title'  => esc_html( $modules[ $slug ]['title'] ),
					'href'   => admin_url( 'admin.php?page=jwp-stk-modules&module=' . rawurlencode( $slug ) ),
				)
			);
		}
	}

	/**
	 * Output inline CSS for the admin toolbar icon.
	 *
	 * @return void
	 */
	public function add_toolbar_styles() {
		if ( ! is_admin_bar_showing() ) {
			return;
		}
		echo '<style>
			#wpadminbar #wp-admin-bar-jwp-stk .jwp-stk-ab-icon {
				display: inline-flex;
				width: 16px;
				height: 16px;
				vertical-align: middle;
				margin-right: 5px;
				position: relative;
				top: -1px;
				flex-shrink: 0;
			}
			#wpadminbar #wp-admin-bar-jwp-stk .jwp-stk-ab-icon svg {
				width: 100%;
				height: 100%;
			}
		</style>';
	}
}
