<?php

namespace Planaday\Platform\WordpressConfig\AdminGUI;

use Planaday\Html\HtmlParserFactory;
use Planaday\Html\Parsers\ParseFailedException;
use Planaday\Html\Parsers\StdDOMParser;
use Planaday\Html\Tags\Admin\Pages\CSS\CSS;
use Planaday\Html\Tags\Admin\Pages\Database\Database;
use Planaday\Html\Tags\Admin\Pages\Support\Support;
use Planaday\Html\Tags\Admin\Pages\Welcome\Welcome;
use Planaday\Html\Tags\Tag;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;
use Planaday\Utility\PluginData;

class AdminPages {
	public function __construct(
		protected HtmlParserFactory $htmlParserFactory,
		protected PluginData $pluginData,
		protected Container $container
	) {}

	public function registerMenus(): void {
		add_menu_page(
			'Planaday Connector',
			'Planaday Connector',
			'manage_options',
			$this->pluginData->getFullPrefix() . AdminPageEnum::PLANADAY_ADMIN->value,
			function () {
				echo $this->getPage( AdminPageEnum::PLANADAY_ADMIN->value );
			},
			plugins_url( '../../../../../assets/plugin.png', __FILE__ )
		);

		// changes the name of the first submenu item
		add_submenu_page(
			$this->pluginData->getFullPrefix() . AdminPageEnum::PLANADAY_ADMIN->value,
			'Planaday - Welkom',
			'Welkom',
			'manage_options',
			$this->pluginData->getFullPrefix() . AdminPageEnum::PLANADAY_ADMIN->value
		);

		add_submenu_page(
			$this->pluginData->getFullPrefix() . AdminPageEnum::PLANADAY_ADMIN->value,
			'Planaday - Algemene Instellingen',
			'Algemene Instellingen',
			'manage_options',
			$this->pluginData->getFullPrefix() . AdminPageEnum::GENERAL_SETTINGS->value,
			function () {
				echo $this->getPage( AdminPageEnum::GENERAL_SETTINGS->value );
			}
		);

		add_submenu_page(
			$this->pluginData->getFullPrefix() . AdminPageEnum::PLANADAY_ADMIN->value,
			'Planaday - Cursus Instellingen',
			'Cursus Instellingen',
			'manage_options',
			$this->pluginData->getFullPrefix() . AdminPageEnum::COURSE_SETTINGS->value,
			function () {
				echo $this->getPage( AdminPageEnum::COURSE_SETTINGS->value );
			}
		);

		add_submenu_page(
			$this->pluginData->getFullPrefix() . AdminPageEnum::PLANADAY_ADMIN->value,
			'Planaday - Boeking Instellingen',
			'Boeking Instellingen',
			'manage_options',
			$this->pluginData->getFullPrefix() . AdminPageEnum::BOOKING_SETTINGS->value,
			function () {
				echo $this->getPage( AdminPageEnum::BOOKING_SETTINGS->value );
			}
		);

		add_submenu_page(
			$this->pluginData->getFullPrefix() . AdminPageEnum::PLANADAY_ADMIN->value,
			'Planaday - Betaling',
			'Betaling Instellingen',
			'manage_options',
			$this->pluginData->getFullPrefix() . AdminPageEnum::PAYMENT->value,
			function () {
				echo $this->getPage( AdminPageEnum::PAYMENT->value );
			}
		);

		add_submenu_page(
			$this->pluginData->getFullPrefix() . AdminPageEnum::PLANADAY_ADMIN->value,
			'Planaday - Database',
			'Database',
			'manage_options',
			$this->pluginData->getFullPrefix() . AdminPageEnum::DATABASE->value,
			function () {
				echo $this->getPage( AdminPageEnum::DATABASE->value );
			}
		);

		add_submenu_page(
			$this->pluginData->getFullPrefix() . AdminPageEnum::PLANADAY_ADMIN->value,
			'Planaday - Stijl',
			'Stijl',
			'manage_options',
			$this->pluginData->getFullPrefix() . AdminPageEnum::CSS->value,
			function () {
				echo $this->getPage( AdminPageEnum::CSS->value );
			}
		);

		add_submenu_page(
			$this->pluginData->getFullPrefix() . AdminPageEnum::PLANADAY_ADMIN->value,
			'Planaday - Support',
			'Support',
			'manage_options',
			$this->pluginData->getFullPrefix() . AdminPageEnum::SUPPORT->value,
			function () {
				echo $this->getPage( AdminPageEnum::SUPPORT->value );
			}
		);

		add_submenu_page(
			$this->pluginData->getFullPrefix() . AdminPageEnum::PLANADAY_ADMIN->value,
			'Planaday - Functionaliteit',
			'Functionaliteit',
			'manage_options',
			$this->pluginData->getFullPrefix() . AdminPageEnum::FEATURES->value,
			function () {
				echo $this->getPage( AdminPageEnum::FEATURES->value );
			}
		);

		add_submenu_page(
			null,
			'Planaday - Logs',
			null,
			'manage_options',
			$this->pluginData->getFullPrefix() . AdminPageEnum::Logs->value,
			function () {
				echo $this->getPage( AdminPageEnum::Logs->value );
			}
		);
	}

	public function getPage( string $page ): string {
		$parser = $this->htmlParserFactory->getParser();

		switch ( AdminPageEnum::tryFrom($page) ) {
			case AdminPageEnum::PLANADAY_ADMIN:
				return $parser->parse( Tag::getParsableTag( Welcome::TAG, [] ) );
			case AdminPageEnum::GENERAL_SETTINGS:
				try{
					return $this->container->getInstance(StdDOMParser::class)->parse( "<pad-admin-page-general/>" );
				} catch (ParseFailedException|InjectionImpossibleException $e) {
					return "<!-- Error parsing general settings page: " . htmlspecialchars($e->getMessage()) . " -->";
				}
			case AdminPageEnum::BOOKING_SETTINGS:
				try{
					return $this->container->getInstance(StdDOMParser::class)->parse( "<pad-admin-page-booking/>" );
				} catch (ParseFailedException|InjectionImpossibleException $e) {
					return "<!-- Error parsing course settings page: " . htmlspecialchars($e->getMessage()) . " -->";
				}
			case AdminPageEnum::COURSE_SETTINGS:
				try{
					return $this->container->getInstance(StdDOMParser::class)->parse( "<pad-admin-page-course/>" );
				} catch (ParseFailedException|InjectionImpossibleException $e) {
					return "<!-- Error parsing course settings page: " . htmlspecialchars($e->getMessage()) . " -->";
				}
			case AdminPageEnum::PAYMENT:
				try{
					return $this->container->getInstance(StdDOMParser::class)->parse( "<pad-admin-page-payment/>" );
				} catch (ParseFailedException|InjectionImpossibleException $e) {
					return "<!-- Error parsing payment settings page: " . htmlspecialchars($e->getMessage()) . " -->";
				}
			case AdminPageEnum::FEATURES:
				try{
					return $this->container->getInstance(StdDOMParser::class)->parse( "<pad-admin-page-feature_toggles/>" );
				} catch (ParseFailedException|InjectionImpossibleException $e) {
					return "<!-- Error parsing Feature Toggle settings page: " . htmlspecialchars($e->getMessage()) . " -->";
				}
			case AdminPageEnum::Logs:
				try{
					return $this->container->getInstance(StdDOMParser::class)->parse( "<pad-admin-page-logs/>" );
				} catch (ParseFailedException|InjectionImpossibleException $e) {
					return "<!-- Error parsing logs page: " . htmlspecialchars($e->getMessage()) . " -->";
				}
			case AdminPageEnum::DATABASE:
				return $parser->parse( Tag::getParsableTag( Database::TAG, [] ) );
			case AdminPageEnum::SUPPORT:
				return $parser->parse( Tag::getParsableTag( Support::TAG, [] ) );
			case AdminPageEnum::CSS:
				return $parser->parse( Tag::getParsableTag( CSS::TAG, [] ) );
			default:
				return '';
		}
	}
}