<?php

namespace Planaday\Platform\WordpressConfig\Widgets\Search;

use Planaday\Html\HtmlParserFactory;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Platform\PlatformAbstraction\UrlHandler;
use Planaday\Platform\WordpressConfig\ShortcodeLinks\CourseShortcodes;
use Planaday\Utility\Debug\Log\Logging;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;
use Planaday\Utility\PluginData;
use WP_Widget;

class Search extends WP_Widget {
	use Logging;

	public function __construct(
		protected PluginData $pluginData,
		LocalizationController $localizationController,
		protected Container $registry
	) {
		parent::__construct(
			$pluginData->getFullPrefix() . '-search-widget',
			$pluginData->getFullPrefix(). ' ' . $localizationController->translate('zoeken'),
			array(
				'customize_selective_refresh' => true,
			)
		);
	}

	public function form( $instance ):void {
		try {
			$parser = $this->registry->getInstance(HtmlParserFactory::class)->getParser();
		} catch (InjectionImpossibleException $e) {
			echo "<div> Iets is niet goed gegaan, probeer het later opnieuw: {$e->getMessage()} </div>";
			return;
		}

		$parser->setBeingParsedName(self::class);
		$parser->setData('titleValue', $instance[SearchSettingsEnum::TITLE] ?? 'Cursus zoeken');

		$parser->setData('titleName', $this->get_field_name(SearchSettingsEnum::TITLE));
		$parser->setData('pageName', $this->get_field_name(SearchSettingsEnum::PAGE));

		$parser->setData('titleToggleName', $this->get_field_name(SearchSettingsEnum::ASK_TITLE));
		$parser->setData('locationToggleName', $this->get_field_name(SearchSettingsEnum::ASK_LOCATION));
		$parser->setData('labelsToggleName', $this->get_field_name(SearchSettingsEnum::ASK_LABELS));
		$parser->setData('soobToggleName', $this->get_field_name(SearchSettingsEnum::ASK_SOOB));
		$parser->setData('code95ToggleName', $this->get_field_name(SearchSettingsEnum::ASK_CODE_95));
		$parser->setData('stapToggleName', $this->get_field_name(SearchSettingsEnum::ASK_STAP));
		$parser->setData('eLearningToggleName', $this->get_field_name(SearchSettingsEnum::ASK_E_LEARNING));

		$parser->setData('titleToggleId', $this->get_field_id(SearchSettingsEnum::ASK_TITLE));
		$parser->setData('locationToggleId', $this->get_field_id(SearchSettingsEnum::ASK_LOCATION));
		$parser->setData('labelsToggleId', $this->get_field_id(SearchSettingsEnum::ASK_LABELS));
		$parser->setData('soobToggleId', $this->get_field_id(SearchSettingsEnum::ASK_SOOB));
		$parser->setData('code95ToggleId', $this->get_field_id(SearchSettingsEnum::ASK_CODE_95));
		$parser->setData('stapToggleId', $this->get_field_id(SearchSettingsEnum::ASK_STAP));
		$parser->setData('eLearningToggleId', $this->get_field_id(SearchSettingsEnum::ASK_E_LEARNING));

		$parser->setData('titleToggleChecked', $instance[SearchSettingsEnum::ASK_TITLE] == '1' ? "data-checked" : "");
		$parser->setData('locationToggleChecked', $instance[SearchSettingsEnum::ASK_LOCATION] == '1' ? "data-checked" : "");
		$parser->setData('labelsToggleChecked', $instance[SearchSettingsEnum::ASK_LABELS] == '1' ? "data-checked" : "");
		$parser->setData('soobToggleChecked', $instance[SearchSettingsEnum::ASK_SOOB] == '1' ? "data-checked" : "");
		$parser->setData('code95ToggleChecked', $instance[SearchSettingsEnum::ASK_CODE_95] == '1' ? "data-checked" : "");
		$parser->setData('stapToggleChecked', $instance[SearchSettingsEnum::ASK_STAP] == '1' ? "data-checked" : "");
		$parser->setData('eLearningToggleChecked', $instance[SearchSettingsEnum::ASK_E_LEARNING] == '1' ? "data-checked" : "");


		$parser->setData('pageValue', $instance[SearchSettingsEnum::PAGE] ?? '0');
		$parser->setData('searchShortcode', $this->pluginData->getShortPrefix() . CourseShortcodes::COURSE_SEARCH_SHORTCODE);

		echo $parser->parse(file_get_contents( __DIR__ . '/SearchSettingsForm.html' ));
	}

	public function update( $new_instance, $old_instance ): array {
		return $new_instance;
	}


	public function widget( $args, $instance ): void {
		try {
			$parser = $this->registry->getInstance(HtmlParserFactory::class)->getParser();
			$urlHandler = $this->registry->getInstance(UrlHandler::class);
		} catch (InjectionImpossibleException $e) {
			$this->logError("Failed to create HtmlParser, error: " . $e->getMessage());
			return;
		}

		$parser->setBeingParsedName(self::class);
		$parser->setData( 'titleText', $instance[ SearchSettingsEnum::TITLE ] ?? 'Cursus zoeken' );
		$pageUrl = $urlHandler->getPageUrl( $instance[ SearchSettingsEnum::PAGE ] ?? '0');

		if ( $pageUrl && 0 != ($instance[ SearchSettingsEnum::PAGE ] ?? '0')) {
			$parser->setCondition( 'pageIsChosen', true);
			$parser->setData('action', $pageUrl);
		} else {
			$parser->setCondition( 'pageIsChosen', false);
			$this->logWarning(
				"Search Page is not set or the url is invalid, url: $pageUrl\n" .
				"Instance page setting: " . ($instance[ SearchSettingsEnum::PAGE ] ?? '"Not Set"')
			);
		}

		$parser->setUnsafeData('beforeTitle', $args['before_title']);
		$parser->setUnsafeData('afterTitle', $args['after_title']);

		$parser->setData('title', $instance[SearchSettingsEnum::ASK_TITLE]);
		$parser->setData('location', $instance[SearchSettingsEnum::ASK_LOCATION]);
		$parser->setData('labels', $instance[SearchSettingsEnum::ASK_LABELS]);
		$parser->setData('soob', $instance[SearchSettingsEnum::ASK_SOOB]);
		$parser->setData('code95', $instance[SearchSettingsEnum::ASK_CODE_95]);
		$parser->setData('stap', $instance[SearchSettingsEnum::ASK_STAP]);
		$parser->setData('eLearning', $instance[SearchSettingsEnum::ASK_E_LEARNING]);

		echo $args['before_widget'];
		echo $parser->parse(file_get_contents(__DIR__ . '/SearchWidget.html'));
		echo $args['after_widget'];
	}

}