<?php

namespace GemBoards;

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

class Admin {

	public static function init() {
		$self = new self();
		add_action( 'admin_menu', [ $self, 'register_menu' ] );
		add_action( 'admin_notices', [ $self, 'configuration_check' ] );
		add_action( 'admin_enqueue_scripts', array( $self, 'enqueue_assets' ) );
	}

	public function register_menu() {
		$icon_url = GEMBOARDS_PLUGIN_ROOT_URI . 'assets/images/logo_admin_menu.svg';
		add_menu_page(
			__( 'GemBoards', 'gemboards' ),
			'GemBoards',
			'manage_options',
			'gemboards-settings',
			[ $this, 'render_dashboard' ],
			$icon_url,
			95
		);
	}

	public function render_dashboard() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		echo '<div id="gemboards-admin-settings"></div>';
	}

	public function configuration_check(): void {
		if ( empty( get_option( 'permalink_structure' ) ) ) {
			echo '<div class="notice notice-error"><p><strong>GemBoards: Please select a permalink structure other than "Plain" in <a href="' . esc_url( admin_url( 'options-permalink.php' ) ) . '">Permalink Settings</a>.</strong></p></div>';
		}

		if ( empty( get_option( GEMBOARDS_SETTINGS_NAME ) ) ) {
			$settings_url = admin_url( 'admin.php?page=gemboards-settings' );
			echo '<div class="notice notice-error"><p>';
			echo '<strong>GemBoards is not properly configured.</strong> ';
			echo 'Please configure it from here: <a href="' . esc_url( $settings_url ) . '">GemBoards Settings</a>.';
			echo '</p></div>';
		}
	}

	public function enqueue_assets() {
		wp_enqueue_style( 'gemboards-admin-menu', GEMBOARDS_ASSETS_URI . 'css/admin-menu.css', [], GEMBOARDS_VERSION );
	}
}
