<?php
/**
 * Admin Hooks Class
 *
 * @package errigal
 * @since 1.1.0
 */

namespace Errigal\Hook;

defined( 'ABSPATH' ) || exit;

use Errigal\Admin\{
	Errigal_Admin as Admin,
	Errigal_Notice as Notice,
};
use Errigal\Constant\{
	Errigal_Admin_Hook as Admin_Hook,
	Errigal_WP_Hook as WP_Hook,
	Errigal_WP_Page as WP_Page,
};
use Errigal\Customiser\Errigal_Custom_Typography as Typography;
use Errigal\Shared\{
	Errigal_Asset as Asset,
	Errigal_Theme_Info as Theme_Info,
	Errigal_Utility as Utility,
	Errigal_Validator as Validator,
};

/**
 * This class is in charge of adding admin hooks.
 *
 * @since 1.1.0
 */
class Errigal_Admin_Hooks extends Errigal_Hooks_Base {
	/**
	 * Stylesheet name: Admin.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	private const STYLE_ADMIN = 'admin-style';

	/**
	 * Stylesheet name: Admin widgets.
	 *
	 * @since 1.2.0
	 *
	 * @var string
	 */
	private const STYLE_ADMIN_WIDGETS = 'admin-widgets-style';

	/**
	 * Nonce name: Admin.
	 *
	 * @since 1.0.4
	 *
	 * @var string
	 */
	private const NONCE_NAME_ADMIN = 'admin_nonce';

	/**
	 * User meta key name: Welcome admin notice dismissed.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	private const KEY_NAME_NOTICE_DISMISSED_WELCOME = 'admin_notice_dismissed_welcome';

	/**
	 * Notice pages: Welcome.
	 *
	 * @since 1.1.0
	 *
	 * @var string[]
	 */
	private const NOTICE_PAGES_WELCOME = array(
		WP_Page::INDEX,
		WP_Page::THEMES,
	);

	/**
	 * Template part slug: Welcome admin notice.
	 *
	 * @since 1.2.0
	 *
	 * @var string
	 */
	private const TEMPLATE_PART_WELCOME_ADMIN_NOTICE = 'template-parts/admin/notice-welcome';

	/**
	 * Nonce action.
	 *
	 * @since 1.0.4
	 *
	 * @var string
	 */
	private $nonce_action;

	/**
	 * User metadata key: Welcome admin notice dismissed.
	 *
	 * @since 1.0.4
	 *
	 * @var string
	 */
	private $key_notice_dismissed_welcome;

	/**
	 * Constructor.
	 *
	 * @since 1.1.0
	 */
	public function __construct() {
		parent::__construct();

		$theme_name_prefix = sanitize_title( Theme_Info::get_theme_name() );

		$this->nonce_action                 = sanitize_key(
			Utility::join(
				Utility::SEPARATOR_UNDERSCORE,
				$theme_name_prefix,
				self::NONCE_NAME_ADMIN
			)
		);
		$this->key_notice_dismissed_welcome = sanitize_key(
			Utility::join(
				Utility::SEPARATOR_UNDERSCORE,
				$theme_name_prefix,
				self::KEY_NAME_NOTICE_DISMISSED_WELCOME
			)
		);
	}

	/**
	 * Enqueues admin scripts.
	 *
	 * @since 1.0.4
	 *
	 * @link https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/
	 *
	 * @return void
	 */
	public function enqueue_admin_scripts() {
		Asset::enqueue_style(
			self::STYLE_ADMIN,
			Asset::get_stylesheet_uri( self::STYLE_ADMIN )
		);

		if ( WP_Page::WIDGETS === Admin::get_current_page() ) {
			Asset::enqueue_style(
				self::STYLE_ADMIN_WIDGETS,
				Asset::get_stylesheet_uri( self::STYLE_ADMIN_WIDGETS )
			);
		}

		$script_handle_admin = Asset::enqueue_script(
			'admin',
			array(
				Asset::DEP_SCRIPT_JQUERY,
				Asset::DEP_SCRIPT_WP_I18N,
			)
		);

		Asset::localise_script(
			$script_handle_admin,
			'ERRIGAL_ADMIN',
			array(
				'admin_media_modal_title'              => esc_html__( 'Select or upload an image file', 'errigal' ),
				'admin_media_modal_button_text_select' => esc_html__( 'Select', 'errigal' ),
				'nonce'                                => Admin::create_nonce( $this->nonce_action ),
				'font_size_class'                      => Typography::get_font_size_class(),
			)
		);
	}

	/**
	 * Shows notice: Welcome.
	 *
	 * @since 1.0.4
	 *
	 * @link https://developer.wordpress.org/reference/hooks/admin_notices/
	 *
	 * @return void
	 */
	public function show_notice_welcome() {
		/**
		 * Filters welcome admin notice pages.
		 *
		 * @since 1.2.0
		 *
		 * @param string $welcome_admin_notice_pages Welcome admin notice pages.
		 */
		$welcome_admin_notice_pages = apply_filters(
			Admin_Hook::WELCOME_ADMIN_NOTICE_PAGES,
			self::NOTICE_PAGES_WELCOME
		);

		if (
			! Validator::is_string_list( $welcome_admin_notice_pages ) ||
			! Notice::can_show( self::NOTICE_PAGES_WELCOME )
		) {
			return;
		}

		$dismissed_version = Admin::get_user_meta( $this->key_notice_dismissed_welcome );

		if ( Validator::is_non_empty_string( $dismissed_version ) ) {
			if ( Theme_Info::get_theme_version() === $dismissed_version ) {
				return;
			}

			Admin::delete_user_meta( $this->key_notice_dismissed_welcome );
		}

		/**
		 * Filters welcome admin notice template part slug.
		 *
		 * @since 1.2.0
		 *
		 * @param string $welcome_admin_notice_template_part_slug Slug name for welcome admin notice.
		 */
		$welcome_admin_notice_template_part_slug = apply_filters(
			Admin_Hook::WELCOME_ADMIN_NOTICE_TEMPLATE_PART_SLUG,
			self::TEMPLATE_PART_WELCOME_ADMIN_NOTICE
		);

		if ( ! Validator::is_non_empty_string( $welcome_admin_notice_template_part_slug ) ) {
			return;
		}

		get_template_part( $welcome_admin_notice_template_part_slug );
	}

	/**
	 * Dismisses notice: Welcome.
	 *
	 * @since 1.0.4
	 *
	 * @link https://developer.wordpress.org/reference/hooks/wp_ajax_action/
	 *
	 * @return void
	 */
	public function dismiss_notice_welcome() {
		if ( ! Admin::is_valid_nonce_action( $this->nonce_action ) ) {
			wp_die();
		}

		Admin::update_user_meta(
			$this->key_notice_dismissed_welcome,
			Theme_Info::get_theme_version()
		);

		wp_die();
	}

	/**
	 * Adds hooks.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	protected function add_hooks() {
		add_action( WP_Hook::ADMIN_ENQUEUE_SCRIPTS, array( $this, 'enqueue_admin_scripts' ) );
		add_action( WP_Hook::ADMIN_NOTICES, array( $this, 'show_notice_welcome' ) );
		add_action( Admin_Hook::WP_AJAX_DISMISS_ADMIN_NOTICE_WELCOME, array( $this, 'dismiss_notice_welcome' ) );
	}
}
