<?php

namespace NeurogenesisTheme\Structure;

use NeurogenesisTheme\Core\Router;
use NeurogenesisTheme\Helpers\RecommendedPlugin;

class Menus {

	public static function init() {
		add_action( 'admin_menu', array( __CLASS__, 'register_theme_admin_menu' ) );
	}

	/**
	 * Register the theme admin menu and page.
	 *
	 * Under Appearance when Neurogenesis Styler is not active.
	 * When the plugin is active it owns the top-level Neurogenesis menu.
	 *
	 * @return void
	 */
	public static function register_theme_admin_menu() {
		if ( 'active' === RecommendedPlugin::get_status() ) {
			return;
		}

		$router        = new Router( NEUROGENESIS_THEME_PAGE_ID );
		$page_callback = $router->display();

		$hook = add_theme_page(
			__( 'Thinko', 'thinko' ),
			__( 'Thinko', 'thinko' ),
			'edit_theme_options',
			NEUROGENESIS_THEME_PAGE_ID,
			$page_callback
		);

		if ( ! is_string( $hook ) || '' === $hook ) {
			return;
		}

		add_action( "load-{$hook}", array( __CLASS__, 'hide_default_page_title' ) );

		neurogenesis_theme_registry_set( 'neurogenesis_theme.settings_page_hook', $hook );
	}

	/**
	 * Remove the default WP admin H1 so the custom header sits flush at the top.
	 *
	 * @return void
	 */
	public static function hide_default_page_title() {
		add_action(
			'admin_head',
			static function () {
				echo '<style id="neurogenesis-theme-hide-page-title">#wpbody-content > .wrap > h1{display:none!important}</style>';
			},
			1
		);
	}
}
