<?php
/**
 * Admin manager for registering menus, assets, and admin hooks.
 *
 * @package MonitorCat\Admin
 */

declare(strict_types=1);

namespace MonitorCat\Admin;

defined( 'ABSPATH' ) || exit;

use MonitorCat\Settings;

/**
 * Manages the WordPress admin integration for the plugin.
 */
final class AdminManager {

	/**
	 * Plugin settings.
	 *
	 * @var Settings
	 */
	private Settings $settings;

	/**
	 * Asset manager.
	 *
	 * @var AssetManager
	 */
	private AssetManager $assets;

	/**
	 * Dashboard page controller.
	 *
	 * @var DashboardPage
	 */
	private DashboardPage $dashboard;

	/**
	 * Constructor.
	 *
	 * @param Settings      $settings  Plugin settings.
	 * @param AssetManager  $assets    Asset manager.
	 * @param DashboardPage $dashboard Dashboard page controller.
	 */
	public function __construct( Settings $settings, AssetManager $assets, DashboardPage $dashboard ) {
		$this->settings  = $settings;
		$this->assets    = $assets;
		$this->dashboard = $dashboard;
	}

	/**
	 * Register admin hooks.
	 */
	public function register(): void {
		add_action( 'admin_menu', array( $this, 'register_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_shared_assets' ) );
		add_filter( 'admin_body_class', array( $this, 'add_body_class' ) );
	}

	/**
	 * Register the admin menu and submenu pages.
	 */
	public function register_menu(): void {
		add_menu_page(
			__( 'Monitor.Cat', 'monitor-cat' ),
			__( 'Monitor.Cat', 'monitor-cat' ),
			'manage_options',
			'monitor-cat',
			array( $this, 'render_dashboard' ),
			$this->get_menu_icon(),
			30
		);

		add_submenu_page(
			'monitor-cat',
			__( 'Dashboard', 'monitor-cat' ),
			__( 'Dashboard', 'monitor-cat' ),
			'manage_options',
			'monitor-cat',
			array( $this, 'render_dashboard' )
		);
	}

	/**
	 * Render the dashboard page.
	 */
	public function render_dashboard(): void {
		$this->dashboard->render();
	}

	/**
	 * Enqueue shared admin assets on plugin pages.
	 *
	 * @param string $hook_suffix The current admin page hook suffix.
	 */
	public function enqueue_shared_assets( string $hook_suffix ): void {
		if ( false === strpos( $hook_suffix, 'monitor-cat' ) ) {
			return;
		}

		$this->assets->enqueue_style(
			'monitorcat-fa',
			'assets/css/vendor/fontawesome.min.css'
		);

		$this->assets->enqueue_style(
			'monitorcat-fa-duotone',
			'assets/css/vendor/duotone-regular.css',
			array( 'monitorcat-fa' )
		);

		$this->assets->enqueue_style(
			'monitorcat-admin',
			'dist/css/admin.min.css',
			array( 'monitorcat-fa-duotone' )
		);

		$this->assets->enqueue_script(
			'monitorcat-admin-js',
			'dist/js/admin.min.js'
		);

		// Dashboard-specific assets on the main plugin page.
		if ( 'toplevel_page_monitor-cat' === $hook_suffix ) {
			$this->assets->enqueue_style(
				'monitorcat-dashboard',
				'dist/css/dashboard.min.css',
				array( 'monitorcat-admin' )
			);

			$this->assets->enqueue_script(
				'monitorcat-dashboard-js',
				'dist/js/dashboard.min.js',
				array( 'monitorcat-admin-js' )
			);
		}

		wp_localize_script(
			'monitorcat-admin-js',
			'MonitorCat',
			array(
				'restBase'  => esc_url_raw( rest_url( 'monitor-cat/v1/' ) ),
				'nonce'     => wp_create_nonce( 'wp_rest' ),
				'adminUrl'  => esc_url( admin_url() ),
				'pluginUrl' => esc_url( MONITORCAT_URL ),
				'siteUrl'   => esc_url( home_url( '/' ) ),
				'version'   => MONITORCAT_VERSION,
			)
		);
	}

	/**
	 * Add a custom body class on plugin admin pages.
	 *
	 * @param string $classes Space-separated list of body classes.
	 * @return string Modified body classes.
	 */
	public function add_body_class( string $classes ): string {
		$screen = get_current_screen();

		if ( null !== $screen && false !== strpos( $screen->id, 'monitor-cat' ) ) {
			$classes .= ' monitorcat-admin';
		}

		return $classes;
	}

	/**
	 * Get the base64-encoded SVG menu icon.
	 *
	 * @return string Data URI for the menu icon.
	 */
	private function get_menu_icon(): string {
		$svg_path = MONITORCAT_DIR . 'assets/img/monitor-cat-monochrome.svg';

		if ( file_exists( $svg_path ) ) {
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file read.
			$svg_content = file_get_contents( $svg_path );

			if ( false !== $svg_content ) {
				// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Required for data URI.
				return 'data:image/svg+xml;base64,' . base64_encode( $svg_content );
			}
		}

		return 'dashicons-heart';
	}
}
