<?php

namespace Planaday\Entities\Booking\Form\Company\ContactPerson;

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

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

	protected bool $askContactPerson = true;
	protected bool $askForAddress = true;
	protected bool $addressRequired = false;
	protected bool $askForPhoneNumber = true;
	protected bool $phoneNumberRequired = false;

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

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

		return ( new CollapsibleGroup() )
			->startCollapsed()
			->setIndex( ContactPersonSettingsEnum::class )
			->setHeader( $t( "Contactpersoon" ) )
			->setDescription( $t( "Instellingen voor het vragen naar een contactpersoon" ))
			->startCollapsed()
			->addContents(
				$contactPersonShown = ( new BoolInput() )
					->setIndex(ContactPersonSettingsEnum::AskContactPerson->value)
					->setName( $t( "Contactpersoon vragen?" ) )
					->setDescription( $t( "Moet de optie voor een contactpersoon toevoegen weergeven worden?" ) )
					->setDefaultValue( true )
					->addSetCallback( function ( $value ) {
						$this->askContactPerson = $value ?? $this->askContactPerson;
					}),
				(new BasicGroup())
					->addConditions(
						( new InputValueCondition( $contactPersonShown ) )->equals()->value( true )
					)->addContents(
						$addressAsked = ( new BoolInput() )
							->setIndex( ContactPersonSettingsEnum::AskAddress->value )
							->setName( $t( "Adres vragen?" ) )
							->setDescription( $t( "Moet er een adres gevraagd worden?" ) )
							->setDefaultValue( $this->askForAddress )
							->addSetCallback( function ( $value ) {
								$this->askForAddress = $value ?? $this->askForAddress;
							} ),
						( new BasicGroup() )
							->addConditions(
								( new InputValueCondition( $addressAsked ) )->equals()->value( true )
							)->addContents(
								( new BoolInput() )
									->setIndex( ContactPersonSettingsEnum::AddressRequired->value )
									->setName( $t( "Adres verplicht?" ) )
									->setDescription( $t( "Moet het adres verplicht zijn?" ) )
									->setDefaultValue( $this->addressRequired )
									->addSetCallback( function ( $value ) {
										$this->addressRequired = $value ?? $this->addressRequired;
									} )
							)->addClasses( "pad-subgroup" ),
						$phoneNumberAsked = ( new BoolInput() )
							->setIndex( ContactPersonSettingsEnum::AskPhoneNumber->value )
							->setName( $t( "Telefoonnummer vragen?" ) )
							->setDescription( $t( "Moet er een telefoonnummer gevraagd worden?" ) )
							->setDefaultValue( $this->askForPhoneNumber )
							->addSetCallback( function ( $value ) {
								$this->askForPhoneNumber = $value ?? $this->askForPhoneNumber;
							} ),
						( new BasicGroup() )
							->addConditions(
								( new InputValueCondition( $phoneNumberAsked ) )->equals()->value( true )
							)->addContents(
								( new BoolInput() )
									->setIndex( ContactPersonSettingsEnum::PhoneNumberRequired->value )
									->setName( $t( "Telefoonnummer verplicht?" ) )
									->setDescription( $t( "Moet het telefoonnummer verplicht zijn?" ) )
									->setDefaultValue( $this->phoneNumberRequired )
									->addSetCallback( function ( $value ) {
										$this->phoneNumberRequired = $value ?? $this->phoneNumberRequired;
									} )
							)->addClasses( "pad-subgroup" )
					)->addClasses("pad-subgroup")
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [
				ContactPersonSettingsEnum::AskContactPerson->value => &$this->askContactPerson,
				ContactPersonSettingsEnum::AskAddress->value      => &$this->askForAddress,
				ContactPersonSettingsEnum::AddressRequired->value => &$this->addressRequired,
				ContactPersonSettingsEnum::AskPhoneNumber->value        => &$this->askForPhoneNumber,
				ContactPersonSettingsEnum::PhoneNumberRequired->value   => &$this->phoneNumberRequired,
			],
			"subGroups"       => [],
			"subGroupAliases" => []
		];
	}

	public function isAskContactPerson(): bool {
		return $this->askContactPerson;
	}

	public function isAskForAddress(): bool {
		return $this->askForAddress;
	}

	public function isAddressRequired(): bool {
		return $this->addressRequired;
	}

	public function isAskForPhoneNumber(): bool {
		return $this->askForPhoneNumber;
	}

	public function isPhoneNumberRequired(): bool {
		return $this->phoneNumberRequired;
	}
}