<?php

namespace Planaday\Html\NodeTags\Course\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\NumberInput;
use Planaday\Utility\FormBuilder\InputFields\StringInput;
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\Structures\QuestionCluster;
use Planaday\Utility\FormBuilder\Validators\Required;

#[Scoped]
class CourseListItemSettings implements EnumMappedSettings, SettingsWithForm, SettingsWithUsageLog {
	use FormGroupable;
	use BasicEnumMappedSetting;
	use BasicSettingsLog;

	protected bool $showCode95Icon = false;
	protected bool $showSoobIcon = false;
	protected bool $showELearningIcon = false;
	protected bool $showMoneyBackNotice = false;
	protected bool $showStartGuaranteeNotice = false;
	protected bool $showFinancialInfo = false;
	protected bool $showPrice = false;
	protected bool $showDayPartCount = false;
	protected bool $showLabels = false;
	protected bool $showIcons = true;
	protected bool $showFirstLocation = false;
	protected string $firstLocationTemplate = "{{city}}";
	protected bool $showCourseButton = true;
	protected string $courseButtonText = "Bekijk cursus";
	protected bool $showDescription = true;
	protected int $maxLengthDescription = 200;
	protected bool $showAvailability = false;

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

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

		return ( new CollapsibleGroup() )
			->startCollapsed()
			->setIndex( CourseListItemSettingsEnum::class )
			->setHeader( $t( "Cursuslijst instellingen" ) )
			->setDescription( $t( "Instellingen voor het tonen van de cursussen" ) )
			->addContents(
				(new QuestionCluster())
					->setName( $t('Overzicht Eigenschappen') )
					->setDescription( $t("Welke eigenschappen moeten worden getoond?"))
					->addContents(
						( new BoolInput() )
							->setIndex( CourseListItemSettingsEnum::ShowCode95Icon->value )
							->setName( $t("Code 95 icoon") )
							->setDescription( $t("Moet er een icoon worden getoond voor de code 95?") )
							->setDefaultValue( $this->showCode95Icon )
							->addSetCallback( function ( $value ) {
								$this->showCode95Icon = $value ?? $this->showCode95Icon;
							}),
						( new BoolInput() )
							->setIndex( CourseListItemSettingsEnum::ShowSoobIcon->value )
							->setName( $t("SOOB icoon") )
							->setDescription( $t("Moet er een icoon worden getoond voor Soob?") )
							->setDefaultValue( $this->showSoobIcon )
							->addSetCallback( function ( $value ) {
								$this->showSoobIcon = $value ?? $this->showSoobIcon;
							}),
						( new BoolInput() )
							->setIndex( CourseListItemSettingsEnum::ShowELearningIcon->value )
							->setName( $t("E-learning icoon") )
							->setDescription( $t("Moet er een icoon worden getoond voor e-learning?") )
							->setDefaultValue( $this->showELearningIcon )
							->addSetCallback( function ( $value ) {
								$this->showELearningIcon = $value ?? $this->showELearningIcon;
							}),
						( new BoolInput() )
							->setIndex( CourseListItemSettingsEnum::ShowMoneyBackNotice->value )
							->setName( $t("Geld terug garantie") )
							->setDescription( $t("Moet er een notitie worden getoond voor de geld terug garantie?") )
							->setDefaultValue( $this->showMoneyBackNotice )
							->addSetCallback( function ( $value ) {
								$this->showMoneyBackNotice = $value ?? $this->showMoneyBackNotice;
							}),
						( new BoolInput() )
							->setIndex( CourseListItemSettingsEnum::ShowStartGuaranteeNotice->value )
							->setName( $t("Startgarantie") )
							->setDescription( $t("Moet er een notitie worden getoond voor start garantie?") )
							->setDefaultValue( $this->showStartGuaranteeNotice )
							->addSetCallback( function ( $value ) {
								$this->showStartGuaranteeNotice = $value ?? $this->showStartGuaranteeNotice;
							}),
						( new BoolInput() )
							->setIndex( CourseListItemSettingsEnum::ShowFinancialInfo->value )
							->setName($t("Financiële toelichting (Betalingsinstellingen)"))
							->setDescription($t("Moet er financiële informatie worden getoond?"))
							->setDefaultValue($this->showFinancialInfo)
							->addSetCallback( function ( $value ) {
								$this->showFinancialInfo = $value ?? $this->showFinancialInfo;
							}),
						( new BoolInput() )
							->setIndex( CourseListItemSettingsEnum::ShowPrice->value )
							->setName( $t("Prijs") )
							->setDescription( $t("Moet er een prijs worden getoond?") )
							->setDefaultValue( $this->showPrice )
							->addSetCallback( function ( $value ) {
								$this->showPrice = $value ?? $this->showPrice;
							}),
						( new BoolInput() )
							->setIndex( CourseListItemSettingsEnum::ShowDayPartCount->value )
							->setName( $t("Aantal dagdelen") )
							->setDescription( $t("Moet het aantal dagdelen worden getoond?") )
							->setDefaultValue( $this->showDayPartCount )
							->addSetCallback( function ( $value ) {
								$this->showDayPartCount = $value ?? $this->showDayPartCount;
							}),
						( new BoolInput() )
							->setIndex( CourseListItemSettingsEnum::ShowAvailability->value )
							->setName( $t("Beschikbare plaatsen") )
							->setDescription( $t("Moet het aantal beschikbare plaatsen worden getoond?") )
							->setDefaultValue( $this->showAvailability )
							->addSetCallback( function ( $value ) {
								$this->showAvailability = $value ?? $this->showAvailability;
							}),
						( new BoolInput() )
							->setIndex( CourseListItemSettingsEnum::ShowLabels->value )
							->setName( $t("Labels") )
							->setDescription( $t("Moeten de labels worden getoond?") )
							->setDefaultValue( $this->showLabels )
							->addSetCallback( function ( $value ) {
								$this->showLabels = $value ?? $this->showLabels;
							}),
						$showFirstLocation = ( new BoolInput() )
							->setIndex( CourseListItemSettingsEnum::ShowFirstLocation->value )
							->setName( $t("Eerste locatie") )
							->setDescription( $t("Moet de eerste locatie worden getoond?") )
							->setDefaultValue( $this->showFirstLocation )
							->addSetCallback( function ( $value ) {
								$this->showFirstLocation = $value ?? $this->showFirstLocation;
							})
					),
				( new BasicGroup() )
					->addContents(
						( new TextEditorInput() )
							->setIndex( CourseListItemSettingsEnum::FirstLocationTemplate->value )
							->setName( $t("Locatie template") )
							->setDescription(
								$t("Hoe moet de eerste locatie worden weergegeven? Gebruik de planaday knop in de editor om de tekst te vervangen door de juiste dynamische waarden.")
							)
							->setDefaultValue( $this->firstLocationTemplate )
							->addSetCallback( function ( $value ) {
								$this->firstLocationTemplate = $value ?? $this->firstLocationTemplate;
							})
							->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( $showFirstLocation ) )->equals()->value(true)
					),
				(new BoolInput())
					->setIndex( CourseListItemSettingsEnum::ShowIcons->value)
					->setName( $t("Toon icoon"))
					->setDescription( $t("Toon iconen bij de labels") )
					->setDefaultValue( $this->showIcons )
					->addSetCallback( function ( $value ) {
						$this->showIcons = $value ?? $this->showIcons;
					}),
				$showButtonQuestion = ( new BoolInput() )
					->setIndex( CourseListItemSettingsEnum::ShowCourseButton->value )
					->setName( $t("Cursus knop") )
					->setDescription( $t("Moet er een detailknop worden getoond voor de cursus?") )
					->setDefaultValue( $this->showCourseButton )
					->addSetCallback( function ( $value ) {
						$this->showCourseButton = $value ?? $this->showCourseButton;
					}),
				( new BasicGroup() )
					->addConditions(
						( new InputValueCondition( $showButtonQuestion ) )->equals()->value(true)
					)->addContents(
						( new StringInput() )
							->setIndex( CourseListItemSettingsEnum::CourseButtonText->value )
							->setName( $t("Cursus knop tekst") )
							->setDescription($t("Welke tekst moet er op de detailknop voor de cursus worden getoond?"))
							->setDefaultValue( $this->courseButtonText )
							->setPlaceholder( $t("Bekijk cursus") )
							->addSetCallback( function ( $value ) {
								$this->courseButtonText = $value ?? $this->courseButtonText;
							})
							->addValidator(
								new Required( $t("Dit veld is verplicht"))
							)
					)->addClasses("pad-subgroup"),
				$showDescriptionQuestion = ( new BoolInput() )
					->setIndex( CourseListItemSettingsEnum::ShowDescription->value )
					->setName( $t("Omschrijving") )
					->setDescription( $t("Moet de omschrijving van een cursus worden getoond?") )
					->setDefaultValue( $this->showDescription )
					->addSetCallback( function ( $value ) {
						$this->showDescription = $value ?? $this->showDescription;
					}),
				( new BasicGroup() )
					->addConditions(
						( new InputValueCondition( $showDescriptionQuestion ) )->equals()->value(true)
					)->addContents(
						(new NumberInput())
							->setIndex( CourseListItemSettingsEnum::MaxLengthDescription->value )
							->setName( $t("Maximale lengte beschrijving") )
							->setDescription($t("Na hoeveel karakters moet de omschrijving worden afgekapt?"))
							->setDefaultValue( $this->maxLengthDescription )
							->setMin( 1 )
							->addSetCallback( function ( $value ) {
								$this->maxLengthDescription = $value ?? $this->maxLengthDescription;
							})
							->addValidator(
								new Required( $t("Dit veld is verplicht"))
							)
					)->addClasses("pad-subgroup")
			);
	}

	public function getEnumMapping(): array {
		return [
			"values" => [
				CourseListItemSettingsEnum::ShowCode95Icon->value => &$this->showCode95Icon,
				CourseListItemSettingsEnum::ShowSoobIcon->value => &$this->showSoobIcon,
				CourseListItemSettingsEnum::ShowELearningIcon->value => &$this->showELearningIcon,
				CourseListItemSettingsEnum::ShowMoneyBackNotice->value => &$this->showMoneyBackNotice,
				CourseListItemSettingsEnum::ShowStartGuaranteeNotice->value => &$this->showStartGuaranteeNotice,
				CourseListItemSettingsEnum::ShowFinancialInfo->value => &$this->showFinancialInfo,
				CourseListItemSettingsEnum::ShowPrice->value => &$this->showPrice,
				CourseListItemSettingsEnum::ShowDayPartCount->value => &$this->showDayPartCount,
				CourseListItemSettingsEnum::ShowLabels->value => &$this->showLabels,
				CourseListItemSettingsEnum::ShowFirstLocation->value => &$this->showFirstLocation,
				CourseListItemSettingsEnum::FirstLocationTemplate->value => &$this->firstLocationTemplate,
				CourseListItemSettingsEnum::ShowCourseButton->value => &$this->showCourseButton,
				CourseListItemSettingsEnum::CourseButtonText->value => &$this->courseButtonText,
				CourseListItemSettingsEnum::ShowDescription->value => &$this->showDescription,
				CourseListItemSettingsEnum::MaxLengthDescription->value => &$this->maxLengthDescription,
				CourseListItemSettingsEnum::ShowAvailability->value => &$this->showAvailability,
				CourseListItemSettingsEnum::ShowIcons->value => &$this->showIcons,
			],
			"subGroups" => [],
			"subGroupAliases" => []
		];
	}

	public function isShowCode95Icon(): bool {
		$this->logUse(CourseListItemSettingsEnum::ShowCode95Icon);
		return $this->showCode95Icon;
	}

	public function isShowSoobIcon(): bool {
		$this->logUse(CourseListItemSettingsEnum::ShowSoobIcon);
		return $this->showSoobIcon;
	}

	public function isShowELearningIcon(): bool {
		$this->logUse(CourseListItemSettingsEnum::ShowELearningIcon);
		return $this->showELearningIcon;
	}

	public function isShowMoneyBackNotice(): bool {
		$this->logUse(CourseListItemSettingsEnum::ShowMoneyBackNotice);
		return $this->showMoneyBackNotice;
	}

	public function isShowStartGuaranteeNotice(): bool {
		$this->logUse(CourseListItemSettingsEnum::ShowStartGuaranteeNotice);
		return $this->showStartGuaranteeNotice;
	}

	public function isShowFinancialInfo(): bool {
		$this->logUse(CourseListItemSettingsEnum::ShowFinancialInfo);
		return $this->showFinancialInfo;
	}

	public function isShowPrice(): bool {
		$this->logUse(CourseListItemSettingsEnum::ShowPrice);
		return $this->showPrice;
	}

	public function isShowDayPartCount(): bool {
		$this->logUse(CourseListItemSettingsEnum::ShowDayPartCount);
		return $this->showDayPartCount;
	}

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

	public function isShowFirstLocation(): bool {
		$this->logUse(CourseListItemSettingsEnum::ShowFirstLocation);
		return $this->showFirstLocation;
	}

	public function getFirstLocationTemplate(): string {
		$this->logUse(CourseListItemSettingsEnum::FirstLocationTemplate);
		return $this->firstLocationTemplate;
	}

	public function isShowCourseButton(): bool {
		$this->logUse(CourseListItemSettingsEnum::ShowCourseButton);
		return $this->showCourseButton;
	}

	public function getCourseButtonText(): string {
		$this->logUse(CourseListItemSettingsEnum::CourseButtonText);
		return $this->courseButtonText;
	}

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

	public function getMaxLengthDescription(): int {
		$this->logUse(CourseListItemSettingsEnum::MaxLengthDescription);
		return $this->maxLengthDescription;
	}

	public function isShowAvailability(): bool {
		$this->logUse(CourseListItemSettingsEnum::ShowAvailability);
		return $this->showAvailability;
	}

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