<?php

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

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 InvoiceSettings implements EnumMappedSettings, SettingsWithForm {
	use FormGroupable;
	use BasicEnumMappedSetting;

	protected bool $askForAddress = true;
	protected bool $addressRequired = false;
	protected bool $askForEmail = true;
	protected bool $emailRequired = false;
	protected bool $showSameAdressOption = false;

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

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

		return ( new CollapsibleGroup() )
			->startCollapsed()
			->setIndex( InvoiceSettingsEnum::class )
			->setHeader( $t( "Financieel" ) )
			->setDescription( $t( "De instellingen voor het vragen van financiële informatie in de boeking." ))
			->addContents(
				$addressAsked = ( new BoolInput() )
					->setIndex( InvoiceSettingsEnum::AskAddress->value )
					->setName( $t( "Factuuradres vragen?" ) )
					->setDescription( $t( "Moet er een factuuradres 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( InvoiceSettingsEnum::AddressRequired->value )
							->setName( $t( "Factuuradres verplicht?" ) )
							->setDescription( $t( "Moet het factuuradres verplicht zijn?" ) )
							->setDefaultValue( $this->addressRequired )
							->addSetCallback( function ( $value ) {
								$this->addressRequired = $value ?? $this->addressRequired;
							} ),
						( new BoolInput() )
							->setIndex( InvoiceSettingsEnum::ShowSameAdressOption->value )
							->setName( $t( "Factuuradres hetzelfde als bedrijfsadres optie" ) )
							->setDescription( $t( "Toon een optie om aan te geven dat het factuuradres hetzelfde is als het bedrijfsadres." ) )
							->setDefaultValue( $this->showSameAdressOption )
							->addSetCallback( function ( $value ) {
								$this->showSameAdressOption = $value ?? $this->showSameAdressOption;
							})
					)->addClasses( "pad-subgroup" ),
				$emailAsked = ( new BoolInput() )
					->setIndex( InvoiceSettingsEnum::AskEmail->value )
					->setName( $t( "Factuur email vragen?" ) )
					->setDescription( $t( "Moet er een factuur email gevraagd worden?" ) )
					->setDefaultValue( $this->askForEmail )
					->addSetCallback( function ( $value ) {
						$this->askForEmail = $value ?? $this->askForEmail;
					} ),
				( new BasicGroup() )
					->addConditions(
						( new InputValueCondition( $emailAsked ) )->equals()->value( true )
					)->addContents(
						( new BoolInput() )
							->setIndex( InvoiceSettingsEnum::EmailRequired->value )
							->setName( $t( "Factuur email verplicht?" ) )
							->setDescription( $t( "Moet de factuur email verplicht zijn?" ) )
							->setDefaultValue( $this->emailRequired )
							->addSetCallback( function ( $value ) {
								$this->emailRequired = $value ?? $this->emailRequired;
							} )
					)->addClasses( "pad-subgroup" )
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [
				InvoiceSettingsEnum::AskAddress->value      => &$this->askForAddress,
				InvoiceSettingsEnum::AddressRequired->value => &$this->addressRequired,
				InvoiceSettingsEnum::AskEmail->value        => &$this->askForEmail,
				InvoiceSettingsEnum::EmailRequired->value   => &$this->emailRequired,
				InvoiceSettingsEnum::ShowSameAdressOption->value => &$this->showSameAdressOption,
			],
			"subGroups"       => [],
			"subGroupAliases" => []
		];
	}

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

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

	public function isAskForEmail(): bool {
		return $this->askForEmail;
	}

	public function isEmailRequired(): bool {
		return $this->emailRequired;
	}

	public function isShowSameAdressOption(): bool {
		return $this->showSameAdressOption;
	}
}