<?php

namespace Planaday\Html\NodeTags\DayPart\ListItem;

use Planaday\Config\BasicEnumMappedSetting;
use Planaday\Config\BasicSettingsLog;
use Planaday\Config\EnumMappedSettings;
use Planaday\Config\FormGroupable;
use Planaday\Config\SettingsLoader;
use Planaday\Config\SettingsWithForm;
use Planaday\Config\SettingsWithUsageLog;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\Conditions\InputValueCondition;
use Planaday\Utility\FormBuilder\InputFields\BoolInput;
use Planaday\Utility\FormBuilder\InputFields\TextEditorInput;
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 DayPartListItemSettings implements EnumMappedSettings, SettingsWithForm, SettingsWithUsageLog {
	use FormGroupable;
	use BasicEnumMappedSetting;
	use BasicSettingsLog;

	protected bool $showLabels = false;
	protected bool $showLocation = false;
	protected string $locationTemplate = " in {{city}}";
	protected bool $showList = false;
	protected bool $showDescription = false;
	protected bool $showIcons = false;
	protected bool $showELearningLabel = false;

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

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values" => [
				DayPartListItemSettingsEnum::ShowLabels->value         => &$this->showLabels,
				DayPartListItemSettingsEnum::ShowLocation->value       => &$this->showLocation,
				DayPartListItemSettingsEnum::LocationTemplate->value   => &$this->locationTemplate,
				DayPartListItemSettingsEnum::ShowList->value           => &$this->showList,
				DayPartListItemSettingsEnum::ShowDescription->value    => &$this->showDescription,
				DayPartListItemSettingsEnum::ShowIcons->value          => &$this->showIcons,
				DayPartListItemSettingsEnum::ShowELearningLabel->value => &$this->showELearningLabel,
			],
			"subGroups" => [],
			"subGroupAliases" => []
		];
	}

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

		return ( new CollapsibleGroup() )
			->startCollapsed()
			->setIndex( DayPartListItemSettingsEnum::class )
			->setHeader( $t( "Dagdelen lijst instellingen" ) )
			->setDescription( $t( "Instellingen voor het tonen van de dagdelen" ) )
			->addContents(
				$showListQuestion = ( new BoolInput() )
					->setIndex( DayPartListItemSettingsEnum::ShowList->value )
					->setName( $t( "Lijst tonen" ) )
					->setDescription( $t( "Moet de lijst met dagdelen worden getoond?" ) )
					->setDefaultValue( $this->showList )
					->addSetCallback( function ( $value ) {
						$this->showList = $value ?? $this->showList;
					}),
				( new BasicGroup() )
					->addConditions(
						( new InputValueCondition( $showListQuestion ) )->equals()->value(true)
					)->addContents(
						(new BoolInput())
							->setIndex(DayPartListItemSettingsEnum::ShowLabels->value)
							->setName($t("Labels tonen"))
							->setDescription($t("Toon de labels van de dagdelen in de lijst."))
							->setDefaultValue($this->showLabels)
							->addSetCallback(function ($value) {
								$this->showLabels = $value ?? $this->showLabels;
							}),
						$showLocation = (new BoolInput())
							->setIndex(DayPartListItemSettingsEnum::ShowLocation->value)
							->setName($t("Locatie tonen"))
							->setDescription($t("Toon de locatie van de dagdelen in de lijst."))
							->setDefaultValue($this->showLocation)
							->addSetCallback(function ($value) {
								$this->showLocation = $value ?? $this->showLocation;
							}),
						( new BasicGroup() )
							->addContents(
								( new TextEditorInput() )
									->setIndex( DayPartListItemSettingsEnum::LocationTemplate->value )
									->setName( $t("Locatie template") )
									->setDescription(
										$t("Hoe moet de locatie worden weergegeven? Gebruik de planaday knop in de editor om de tekst te vervangen door de juiste dynamische waarden.")
									)
									->setDefaultValue( $this->locationTemplate )
									->addSetCallback( function ( $value ) {
										$this->locationTemplate = $value ?? $this->locationTemplate;
									})
									->addValidator(
										new Required( $t("Dit veld is verplicht"))
									)
									->setButtonLayout([
										"Naam" => "{{name}}",
										"Code" => "{{code}}",
										"Straat" => "{{street}}",
										"Straat 2" => "{{street_2}}",
										"Huisnummer" => "{{house_number}}",
										"Huisnummer toevoeging" => "{{house_number_extension}}",
										"Postcode" => "{{postal_code}}",
										"Stad" => "{{city}}",
										"Land" => "{{country}}"
									])
							)
							->addConditions(
								( new InputValueCondition( $showLocation ) )->equals()->value(true)
							),
						(new BoolInput())
							->setIndex( DayPartListItemSettingsEnum::ShowIcons->value)
							->setName( $t("Toon icoon"))
							->setDescription( $t("Toon iconen bij de labels") )
							->setDefaultValue( $this->showIcons )
							->addSetCallback( function ( $value ) {
								$this->showIcons = $value ?? $this->showIcons;
							}),
						(new BoolInput())
							->setIndex( DayPartListItemSettingsEnum::ShowELearningLabel->value )
							->setName( $t("Toon e-learning label"))
							->setDescription( $t("Toon een e-learning label bij het dagdeel als deze dit heeft") )
							->setDefaultValue( $this->showELearningLabel )
							->addSetCallback( function ( $value ) {
								$this->showELearningLabel = $value ?? $this->showELearningLabel;
							}),
						(new BoolInput())
							->setIndex( DayPartListItemSettingsEnum::ShowDescription->value )
							->setName( $t("Toon omschrijving dagdeel"))
							->setDescription( $t("Toon de omschrijving bij het dagdeel") )
							->setDefaultValue( $this->showDescription )
							->addSetCallback( function ( $value ) {
								$this->showDescription = $value ?? $this->showDescription;
							}),
					)
			);
	}

	public function isShowLabels(): bool {
		$this->logUse(DayPartListItemSettingsEnum::ShowLabels);
		return $this->showLabels;
	}

	public function isShowLocation(): bool {
		$this->logUse(DayPartListItemSettingsEnum::ShowLocation);
		return $this->showLocation;
	}

	public function isShowList(): bool {
		$this->logUse(DayPartListItemSettingsEnum::ShowList);
		return $this->showList;
	}

	public function isShowDescription(): bool {
		$this->logUse(DayPartListItemSettingsEnum::ShowDescription);
		return $this->showDescription;
	}

	public function isShowIcons(): bool {
		$this->logUse(DayPartListItemSettingsEnum::ShowIcons);
		return $this->showIcons;
	}

	public function isShowELearningLabel(): bool {
		$this->logUse(DayPartListItemSettingsEnum::ShowELearningLabel);
		return $this->showELearningLabel;
	}

	public function getLocationTemplate(): string {
		$this->logUse(DayPartListItemSettingsEnum::LocationTemplate);
		return $this->locationTemplate;
	}
}