<?php

namespace Planaday\Html\Tags\Course\CourseSearchForm;

use Planaday\Config\Course\Search\SearchSettings;
use Planaday\Entities\Course\PostRequest\CourseSearchFieldEnum;
use Planaday\Entities\PostEntities;
use Planaday\Html\Tags\Form\DataInputFieldTag;
use Planaday\Html\Tags\Form\SelectableDataInputFieldTag;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;

class CourseSearchForm extends DataInputFieldTag {
	const TAG = 'course-search-form';
	const DEFAULT_TEMPLATE = 'courseSearchForm';
	protected string $fieldName = PostEntities::SEARCH_FORM;
	const ACTION_ATTRIBUTE = 'data-action';
	const ASK_TITLE_ATTRIBUTE = 'data-ask-title';
	const ASK_LOCATION_ATTRIBUTE = 'data-ask-location';
	const ASK_SOOB_ATTRIBUTE = 'data-ask-soob';
	const ASK_CODE_95_ATTRIBUTE = 'data-ask-code-95';
	const ASK_E_LEARNING_ATTRIBUTE = 'data-ask-e-learning';
	const ASK_LABELS_ATTRIBUTE = 'data-ask-labels';
	const ASK_STAP_ATTRIBUTE = 'data-ask-stap';

	protected SearchSettings $searchSettings;

	/**
	 * @param Container $registry
	 *
	 * @throws InjectionImpossibleException
	 */
	public function __construct(Container $registry) {
		$this->searchSettings = $registry->getInstance(SearchSettings::class);
		parent::__construct(__DIR__, self::DEFAULT_TEMPLATE, $registry);
	}

	public function buildTag(): string {
		$parser = $this->getNewParser();
		$template = $this->getTemplate();

		$parser->setUnsafeData('action',
			array_key_exists(self::ACTION_ATTRIBUTE, $this->attributes)
				? 'data-action="' . $this->attributes[self::ACTION_ATTRIBUTE] . '"'
				: ''
		);

		$parser->injectAttribute(
			 $this->getRecursiveVersionOfAttribute(SelectableDataInputFieldTag::FIELD_CLASS_ATTRIBUTE),
			CourseSearchFieldEnum::class
		);
		$parser->injectAttribute(
			SelectableDataInputFieldTag::FIELD_CLASS_ATTRIBUTE,
			CourseSearchFieldEnum::class
		);

		$parser->setCondition(
			'location',
			isset($this->attributes[self::ASK_LOCATION_ATTRIBUTE]) ?
				$this->attributes[self::ASK_LOCATION_ATTRIBUTE] == 1 :
				$this->searchSettings->isAskLocation()

		);
		$parser->setCondition(
			'code95',
			isset($this->attributes[self::ASK_CODE_95_ATTRIBUTE]) ?
				$this->attributes[self::ASK_CODE_95_ATTRIBUTE] == 1 :
			$this->searchSettings->isAskCode95()
		);
		$parser->setCondition(
			'eLearning',
			isset($this->attributes[self::ASK_E_LEARNING_ATTRIBUTE]) ?
				$this->attributes[self::ASK_E_LEARNING_ATTRIBUTE] == 1 :
			$this->searchSettings->isAskELearning()
		);
		$parser->setCondition(
			'soob',
			isset($this->attributes[self::ASK_SOOB_ATTRIBUTE]) ?
				$this->attributes[self::ASK_SOOB_ATTRIBUTE] == 1 :
			$this->searchSettings->isAskSoob()
		);
		$parser->setCondition(
			'label',
			isset($this->attributes[self::ASK_LABELS_ATTRIBUTE]) ?
				$this->attributes[self::ASK_LABELS_ATTRIBUTE] == 1 :
			$this->searchSettings->isAskLabels()
		);
		$parser->setCondition(
			'title',
			isset($this->attributes[self::ASK_TITLE_ATTRIBUTE]) ?
				$this->attributes[self::ASK_TITLE_ATTRIBUTE] == 1 :
			$this->searchSettings->isAskTitle()
		);
		$parser->setCondition(
			'stap',
			isset($this->attributes[self::ASK_STAP_ATTRIBUTE]) ?
				$this->attributes[self::ASK_STAP_ATTRIBUTE] == 1 :
			$this->searchSettings->isAskStap()
		);

		return $parser->parse($template);
	}
}