<?php


namespace Fliegerlogin\WpAdmin\Pages;


abstract class AbstractPage {

	/** @var array */
	private $messageStack = [];

	public abstract static function getSlug(): string;
	public static function getCapability(): string {
		return 'manage_options';
	}
	public abstract static function getTitle(): string;
	public abstract static function getMenuTitle(): string;

	public abstract static function setup(): void;

	protected static function setupMenuItem($title, $menuTitle, $capability, $slug): void {
		add_submenu_page(WpVereinsflieger::getSlug(), $title, $menuTitle, $capability, $slug, [get_called_class(), 'handleRequest']);
	}

	public static function handleRequest(): void {
		$childClass = get_called_class();

		/** @var self $page */
		$page = new $childClass();

		$page->processRequest();
		$page->printPage();
	}

	public abstract function processRequest(): void;
	public abstract function printPage(): void;

	public function printHeader(string $pageTitle = ''): void {
		?>
		<h1>Fliegerlogin</h1>

		<?php
		if ($pageTitle) {
			?>
			<h2><?php echo esc_html($pageTitle); ?></h2>
			<?php
		}

	}

	/**
	 * @param string $type success|warning|error|info
	 * @param string $message
	 * @param bool   $isDismissible
	 */
	protected function addMessage(string $type, string $message, bool $isDismissible = true): void {
		$this->messageStack[] = [
			'type' => $type,
			'message' => $message,
			'dismissible' => $isDismissible
		];
	}

	protected function printMessages(): void {
		foreach ($this->messageStack as $i => $message) {
			?>
			<div id="message-<?php echo esc_attr((string) $i); ?>" class="notice-<?php echo esc_attr($message['type']); ?> notice <?php echo $message['dismissible'] ? 'is-dismissible' : ''; ?>">

				<p><?php echo wp_kses_post($message['message']); ?></p>

				<?php
				if ($message['dismissible']) {
					?>
					<button class="notice-dismiss" type="button"><span class="screen-reader-text"><?php esc_html_e('Dismiss', 'sso-for-vereinsflieger'); ?></span></button>
					<?php
				}
				?>

			</div>
			<?php
		}

		$this->messageStack = [];
	}

}