<?php
/**
 * PageContextResolver class.
 *
 * Resolves current admin page context in Firevert,
 * such as active tab, module or section.
 *
 * @package Ilabs\Firevert\Core\Services\Resolver
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Services\Resolver;

/**
 * Class PageContextResolver.
 */
final class PageContextResolver {

	/**
	 * Current admin page slug.
	 *
	 * @var string|null
	 */
	private ?string $page;

	/**
	 * Current tab name.
	 *
	 * @var string|null
	 */
	private ?string $tab;

	/**
	 * Current module slug.
	 *
	 * @var string|null
	 */
	private ?string $module;

	/**
	 * Current section identifier.
	 *
	 * @var string|null
	 */
	private ?string $section;

	/**
	 * Constructor.
	 * Parses current admin query context.
	 */
	public function __construct() {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$this->page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : null;

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$this->tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : null;

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$this->module = isset( $_GET['module'] ) ? sanitize_key( wp_unslash( $_GET['module'] ) ) : null;

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$this->section = isset( $_GET['section'] ) ? sanitize_key( wp_unslash( $_GET['section'] ) ) : null;
	}

	/**
	 * Returns current page slug.
	 *
	 * @return string|null
	 */
	public function get_page(): ?string {
		return $this->page;
	}

	/**
	 * Returns current tab.
	 *
	 * @return string|null
	 */
	public function get_tab(): ?string {
		return $this->tab;
	}

	/**
	 * Returns current module slug.
	 *
	 * @return string|null
	 */
	public function get_module(): ?string {
		return $this->module;
	}

	/**
	 * Returns current section slug.
	 *
	 * @return string|null
	 */
	public function get_section(): ?string {
		return $this->section;
	}

	/**
	 * Checks if current page is Firevert settings.
	 *
	 * @return bool
	 */
	public function is_firevert_settings_page(): bool {
		return 'firevert' === $this->page;
	}

	/**
	 * Checks if currently active tab is general settings.
	 *
	 * @return bool
	 */
	public function is_firevert_general_settings(): bool {
		return $this->is_firevert_settings_page() && 'general' === $this->tab;
	}

	/**
	 * Checks if currently active tab is module settings for given slug.
	 *
	 * @param string $slug Module slug.
	 *
	 * @return bool
	 */
	public function is_firevert_module_settings( string $slug ): bool {
		return $this->is_firevert_settings_page() && 'modules' === $this->tab && $slug === $this->module;
	}
}
