<?php

namespace Planaday\Entities\Booking\Form\Address;

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\BoolInput;
use Planaday\Utility\FormBuilder\Structures\CollapsibleGroup;

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

	protected bool $askForNumberExtension = true;
	protected bool $askForCountry = true;

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

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

		return ( new CollapsibleGroup() )
			->startCollapsed()
			->setIndex( AddressSettingsEnum::class )
			->setHeader( $t( "Adres" ) )
			->setDescription( $t( "De instellingen voor de adres invoervelden." ))
			->addContents(
				(new BoolInput())
					->setIndex( AddressSettingsEnum::AskHouseNumberExtension->value )
					->setName( $t( "Huisnummer extensie vragen?" ) )
					->setDescription( $t( "Moet er bij een adres, een huisnummer extensie gevraagd worden?" ) )
					->setDefaultValue( $this->askForNumberExtension )
					->addSetCallback( function ( $value ) {
						$this->askForNumberExtension = $value ?? $this->askForNumberExtension;
					}),
				(new BoolInput())
					->setIndex( AddressSettingsEnum::AskCountry->value )
					->setName( $t( "Land vragen?" ) )
					->setDescription( $t( "Moet er een land gevraagd worden?" ) )
					->setDefaultValue( $this->askForCountry )
					->addSetCallback( function ( $value ) {
						$this->askForCountry = $value ?? $this->askForCountry;
					})
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [
				AddressSettingsEnum::AskHouseNumberExtension->value => &$this->askForNumberExtension,
				AddressSettingsEnum::AskCountry->value => &$this->askForCountry,
			],
			"subGroups"       => [],
			"subGroupAliases" => []
		];
	}

	public function isAskForNumberExtension(): bool {
		return $this->askForNumberExtension;
	}

	public function isAskForCountry(): bool {
		return $this->askForCountry;
	}
}