<?php

namespace Planaday\Config\Booking\Form\Materials;

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\StringInput;
use Planaday\Utility\FormBuilder\InputFields\TextEditorInput;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;
use Planaday\Utility\FormBuilder\Validators\Required;

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

	protected bool $askMaterialsBookingForm = false;
	protected bool $showDescriptionMaterials = false;
	protected bool $showBothWithAndWithoutTaxPrice = false;
	protected string $materialsFormTitle = "Welke opties zou je willen meebestellen?";
	protected ?string $materialsFormDescription = null;

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

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

		return ( new BasicGroup() )
			->setIndex( MaterialsOptionsEnum::class )
			->addContents(
				$askMaterialsBookingForm = (new BoolInput())
					->setIndex( MaterialsOptionsEnum::ASK_MATERIALS_BOOKING_FORM->value )
					->setName( $t( "Toon materialen in boekingsformulier" ) )
					->setDescription( $t( "Toon materialen indien er materialen in een dagdeel bij een cursus " .
					                      "gekoppeld zijn en beschikbaar zijn voor de api. Dan kan de bezoeker deze " .
					                      "meteen meebestellen.'" ) )
					->setDefaultValue( $this->askMaterialsBookingForm )
					->addSetCallback( function ( $value ) {
						$this->askMaterialsBookingForm = $value ?? $this->askMaterialsBookingForm;
					}),
				( new BasicGroup() )
					->addConditions(
						(new InputValueCondition($askMaterialsBookingForm))->equals()->value(true)
					)->addContents(
						(new BoolInput())
							->setIndex( MaterialsOptionsEnum::SHOW_DESCRIPTION_MATERIALS->value )
							->setName( $t( "Toon omschrijving van materiaal" ) )
							->setDescription( $t( "Toon bij de materialen een omschrijving van het materiaal." ) )
							->setDefaultValue( $this->showDescriptionMaterials )
							->addSetCallback( function ( $value ) {
								$this->showDescriptionMaterials = $value ?? $this->showDescriptionMaterials;
							}),
						( new BoolInput() )
							->setIndex( MaterialsOptionsEnum::SHOW_BOTH_WITH_AND_WITHOUT_TAX_PRICE->value )
							->setName( $t( "Toon prijs met en zonder BTW" ) )
							->setDescription( $t( "Toon bij de materialen de prijs met en zonder BTW. Anders " .
							                      "wordt alleen de standaardprijs getoond." ) )
							->setDefaultValue( $this->showBothWithAndWithoutTaxPrice )
							->addSetCallback( function ( $value ) {
								$this->showBothWithAndWithoutTaxPrice = $value ?? $this->showBothWithAndWithoutTaxPrice;
							}),
						( new StringInput() )
							->setIndex( MaterialsOptionsEnum::MATERIALS_FORM_TITLE->value )
							->setName( $t( "Titel in formulier" ) )
							->setDescription( $t( "Welke titel moet in het formulier getoond worden?" ) )
							->setDefaultValue( $this->materialsFormTitle )
							->addSetCallback( function ( $value ) {
								$this->materialsFormTitle = $value ?? $this->materialsFormTitle;
							})
							->addValidator(
								new Required($t("Titel van formulier is verplicht"))
							),
						( new TextEditorInput() )
							->setIndex( MaterialsOptionsEnum::MATERIALS_FORM_DESCRIPTION->value )
							->setName( $t( "Omschrijving in formulier" ) )
							->setDescription( $t( "Welke omschrijving over de materialen moet in het formulier getoond worden?" ) )
							->setDefaultValue( $this->materialsFormDescription )
							->addSetCallback( function ( $value ) {
								$this->materialsFormDescription = $value ?? $this->materialsFormDescription;
							})
					)
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [
				MaterialsOptionsEnum::ASK_MATERIALS_BOOKING_FORM->value => &$this->askMaterialsBookingForm,
				MaterialsOptionsEnum::SHOW_DESCRIPTION_MATERIALS->value => &$this->showDescriptionMaterials,
				MaterialsOptionsEnum::SHOW_BOTH_WITH_AND_WITHOUT_TAX_PRICE->value => &$this->showBothWithAndWithoutTaxPrice,
				MaterialsOptionsEnum::MATERIALS_FORM_TITLE->value => &$this->materialsFormTitle,
				MaterialsOptionsEnum::MATERIALS_FORM_DESCRIPTION->value => &$this->materialsFormDescription,
			],
			"subGroups"       => [],
			"subGroupAliases" => []
		];
	}

	public function isAskMaterialsBookingForm(): bool {
		$this->logUse( MaterialsOptionsEnum::ASK_MATERIALS_BOOKING_FORM );
		return $this->askMaterialsBookingForm;
	}

	public function isShowDescriptionMaterials(): bool {
		$this->logUse( MaterialsOptionsEnum::SHOW_DESCRIPTION_MATERIALS );
		return $this->showDescriptionMaterials;
	}

	public function isShowBothWithAndWithoutTaxPrice(): bool {
		$this->logUse( MaterialsOptionsEnum::SHOW_BOTH_WITH_AND_WITHOUT_TAX_PRICE );
		return $this->showBothWithAndWithoutTaxPrice;
	}

	public function getMaterialsFormTitle(): string {
		$this->logUse( MaterialsOptionsEnum::MATERIALS_FORM_TITLE );
		return $this->materialsFormTitle;
	}

	public function getMaterialsFormDescription(): ?string {
		$this->logUse( MaterialsOptionsEnum::MATERIALS_FORM_DESCRIPTION );
		return $this->materialsFormDescription;
	}
}