<?php

namespace Planaday\Entities\Booking\Form\Company;

use Planaday\Config\BasicEnumMappedSetting;
use Planaday\Config\EnumMappedSettings;
use Planaday\Config\FormGroupable;
use Planaday\Config\SettingsLoader;
use Planaday\Config\SettingsWithForm;
use Planaday\Entities\Booking\Form\Company\ContactPerson\ContactPersonSettings;
use Planaday\Entities\Booking\Form\Company\ContactPerson\ContactPersonSettingsEnum;
use Planaday\Entities\Booking\Form\Company\Invoice\InvoiceSettings;
use Planaday\Entities\Booking\Form\Company\Invoice\InvoiceSettingsEnum;
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 CompanySettings implements EnumMappedSettings, SettingsWithForm {
	use FormGroupable;
	use BasicEnumMappedSetting;
	protected bool $allowCompanies = true;
	protected bool $asCompanyDefault = false;
	protected bool $askPhoneNumber = false;
	protected bool $phoneNumberRequired = false;

	public function __construct(
		protected LocalizationController $translator,
		protected InvoiceSettings $invoiceSettings,
		protected ContactPersonSettings $contactPersonSettings,
		SettingsLoader $settingsLoader
	) {
		$settingsLoader->reloadData($this, CompanySettingsEnum::class);
	}

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

		return ( new CollapsibleGroup() )
			->startCollapsed()
			->setIndex( CompanySettingsEnum::class )
			->setHeader( $t( "Bedrijven" ) )
			->setDescription( $t( "De instellingen voor de registratie van bedrijven in het boeking formulier." ))
			->addContents(
				$allowCompanies = (new BoolInput())
					->setIndex( CompanySettingsEnum::AllowCompanyBooking->value )
					->setName( $t( "Bedrijven toestaan?" ) )
					->setDescription( $t( "Moeten bedrijven worden toegestaan?" ) )
					->setDefaultValue( $this->allowCompanies )
					->addSetCallback( function ( $value ) {
						$this->allowCompanies = $value ?? $this->allowCompanies;
					}),
				(new BasicGroup())
					->addConditions(
						(new InputValueCondition($allowCompanies))->equals()->value(true)
					)->addContents(
						(new BoolInput())
							->setIndex( CompanySettingsEnum::CompanyBookingIsDefault->value )
							->setName( $t( "Bedrijven als standaard instellen?" ) )
							->setDescription( $t( "Moeten bedrijven als standaard worden ingesteld?" ) )
							->setDefaultValue( $this->asCompanyDefault )
							->addSetCallback( function ( $value ) {
								$this->asCompanyDefault = $value ?? $this->asCompanyDefault;
							}),
						$askPhoneNumber = (new BoolInput())
							->setIndex( CompanySettingsEnum::AskPhoneNumber->value )
							->setName( $t( "Telefoonnummer vragen?" ) )
							->setDescription( $t( "Moet er een telefoonnummer bij het bedrijf gevraagd worden?" ) )
							->setDefaultValue( $this->askPhoneNumber )
							->addSetCallback( function ( $value ) {
								$this->askPhoneNumber = $value ?? $this->askPhoneNumber;
							}),
						(new BasicGroup())
							->addConditions(
								(new InputValueCondition($askPhoneNumber))->equals()->value(true)
							)->addContents(
								(new BoolInput())
									->setIndex( CompanySettingsEnum::PhoneNumberRequired->value )
									->setName( $t( "Telefoonnummer verplicht?" ) )
									->setDescription( $t( "Moet dit telefoonnummer verplicht zijn?" ) )
									->setDefaultValue( $this->phoneNumberRequired )
									->addSetCallback( function ( $value ) {
										$this->phoneNumberRequired = $value ?? $this->phoneNumberRequired;
									})
							)->addClasses("pad-subgroup"),
						$this->contactPersonSettings->getFormGroup(),
						$this->invoiceSettings->getFormGroup(),
					)
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [
				CompanySettingsEnum::AllowCompanyBooking->value => &$this->allowCompanies,
				CompanySettingsEnum::CompanyBookingIsDefault->value => &$this->asCompanyDefault,
				CompanySettingsEnum::AskPhoneNumber->value => &$this->askPhoneNumber,
				CompanySettingsEnum::PhoneNumberRequired->value => &$this->phoneNumberRequired,
			],
			"subGroups"       => [
				CompanySettingsEnum::Invoice->value => $this->invoiceSettings->getEnumMapping(),
				CompanySettingsEnum::ContactPerson->value => $this->contactPersonSettings->getEnumMapping(),
			],
			"subGroupAliases" => [
				InvoiceSettingsEnum::class => CompanySettingsEnum::Invoice->value,
				ContactPersonSettingsEnum::class => CompanySettingsEnum::ContactPerson->value,
			]
		];
	}

	public function isAllowCompanies(): bool {
		return $this->allowCompanies;
	}

	public function isAsCompanyDefault(): bool {
		return $this->asCompanyDefault;
	}

	public function isAskPhoneNumber(): bool {
		return $this->askPhoneNumber;
	}

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