<?php

namespace Planaday\Html\NodeTags\Booking\Form;

use Planaday\Config\BasicEnumMappedSetting;
use Planaday\Config\EnumMappedSettings;
use Planaday\Config\FormGroupable;
use Planaday\Config\SettingsLoader;
use Planaday\Config\SettingsWithForm;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\InputFields\StringInput;
use Planaday\Utility\FormBuilder\Structure;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;
use Planaday\Utility\FormBuilder\Structures\CollapsibleGroup;
use Planaday\Utility\FormBuilder\Validators\Required;

#[Scoped]
class FormTagSettings implements EnumMappedSettings, SettingsWithForm {
	use FormGroupable;
	use BasicEnumMappedSetting;

	protected string $courseStartedMessage = "Deze cursus is al gestart en kan dus niet meer geboekt worden.";
	protected string $courseFullMessage = "Deze cursus is vol en kan dus niet meer geboekt worden.";

	public function __construct(
		protected LocalizationController $translator,
		SettingsLoader $settingsLoader
	) {
		$settingsLoader->reloadData($this, FormTagSettingsEnum::class);
	}

	protected function createFormGroup(): Structure {
		$t = function ( string $string ) {return $this->translator->translate( $string );};

		return (new CollapsibleGroup())
			->startCollapsed()
			->setIndex(FormTagSettingsEnum::class)
			->setHeader($t( "Formulier weergave instellingen"))
			->setDescription($t("Stel in wat er getoond moet worden als het formulier niet getoond kan worden."))
			->addContents(
				( new BasicGroup() )
					->addContents(
						(new StringInput())
							->setIndex(FormTagSettingsEnum::CourseFullMessage->value)
							->setName($t("Volle cursus tekst"))
							->setDescription($t("Deze tekst wordt getoond wanneer de cursus vol is en er dus niet geboekt kan worden."))
							->setDefaultValue($this->courseFullMessage)
							->setPlaceholder($t("Deze cursus is vol en kan dus niet meer geboekt worden."))
							->addSetCallback(function ($value) {
								$this->courseFullMessage = $value ?? $this->courseFullMessage;
							})->addValidator(
								new Required($t("Deze tekst is verplicht"))
							),
						( new StringInput() )
							->setIndex(FormTagSettingsEnum::CourseStartedMessage->value)
							->setName($t("Cursus al gestart tekst"))
							->setDescription($t("Deze tekst wordt getoond wanneer de cursus al gestart is en daardoor niet meer geboekt kan worden."))
							->setDefaultValue($this->courseStartedMessage)
							->setPlaceholder($t("Deze cursus is al gestart en kan dus niet meer geboekt worden."))
							->addSetCallback(function ($value) {
								$this->courseStartedMessage = $value ?? $this->courseStartedMessage;
							})->addValidator(
								new Required($t("Deze tekst is verplicht"))
							),
					)
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values" => [
				FormTagSettingsEnum::CourseStartedMessage->value => &$this->courseStartedMessage,
				FormTagSettingsEnum::CourseFullMessage->value => &$this->courseFullMessage,
			],
			"subGroups" => [],
			"subGroupAliases" => []
		];
	}

	public function getCourseStartedMessage(): string {
		return $this->courseStartedMessage;
	}

	public function getCourseFullMessage(): string {
		return $this->courseFullMessage;
	}
}