<?php

namespace Planaday\Entities\Booking\Form\Address;

use Planaday\Entities\Booking\DTO\Address;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\InputFields\StringInput;
use Planaday\Utility\FormBuilder\LayoutItems\Text;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;
use Planaday\Utility\FormBuilder\Validators\Required;

#[Scoped]
class AddressFormFactory {
	public function __construct(
		protected AddressSettings $addressSettings,
		protected LocalizationController $localizationController,
	) {}

	public function getFormForObject(Address $address, bool $isRequired, string $autocompletePrefix = ""): BasicGroup{
		$t = function (string $text) {
			return $this->localizationController->translate($text);
		};

		$outputGroup = (new BasicGroup());

		$street = ( new StringInput() )
			->setIndex( AddressFieldsEnum::Street->value )
			->setName( $t( "Straat" ) . ( $isRequired ? " *" : "") )
			->setDescription( $t( "De straatnaam" ) )
			->setPlaceholder( $t( "Straat" ) )
			->setAutocomplete( $autocompletePrefix . " street-address" )
			->addSetCallback( function ( $value ) use ( $address ) {
				$address->setStreet( $value );
			});

		$number = ( new StringInput() )
			->setIndex( AddressFieldsEnum::HouseNumber->value )
			->setName( $t( "Huisnummer") . ( $isRequired ? " *" : "")  )
			->setDescription( $t( "Het huisnummer" ) )
			->setPlaceholder( $t( "Huisnummer" ) )
			->addSetCallback( function ( $value ) use ( $address ) {
				$address->setHouseNumber( $value );
			});

		$numberExtension = ( new StringInput() )
			->setIndex( AddressFieldsEnum::HouseNumberExtension->value )
			->setName( $t( "Huisnummer extensie" ) )
			->setDescription( $t( "De extensie van het huisnummer" ) )
			->setPlaceholder( $t( "Huisnummer extensie" ) )
			->addSetCallback( function ( $value ) use ( $address ) {
				$address->setHouseNumberExtension( $value );
			});

		$outputGroup->addContents(
			( new BasicGroup() )
				->addContents(
					(new Text( $t( "Straat & huisnummer" ) . ( $isRequired ? " *" : "")))->addClasses("pad-form-subheader"),
					$street,
					$number,
					$this->addressSettings->isAskForNumberExtension() ?
						$numberExtension :
						null
				)->addClasses("pad-street-and-number")
		);

		$postalCode = ( new StringInput() )
			->setIndex( AddressFieldsEnum::PostalCode->value )
			->setName( $t( "Postcode") . ( $isRequired ? " *" : "")  )
			->setDescription( $t( "De postcode" ) )
			->setPlaceholder( $t( "Postcode" ) )
			->setAutocomplete( $autocompletePrefix . " postal-code" )
			->addSetCallback( function ( $value ) use ( $address ) {
				$address->setPostalCode( $value );
			});

		$city = ( new StringInput() )
			->setIndex( AddressFieldsEnum::City->value )
			->setName( $t( "Stad") . ( $isRequired ? " *" : "")  )
			->setDescription( $t( "De stad" ) )
			->setPlaceholder( $t( "Stad" ) )
			->setAutocomplete( $autocompletePrefix . " address-level2" )
			->addSetCallback( function ( $value ) use ( $address ) {
				$address->setCity( $value );
			});

		$country = ( new StringInput() )
			->setIndex( AddressFieldsEnum::Country->value )
			->setName( $t( "Land") . ( $isRequired ? " *" : "")  )
			->setDescription( $t( "Het land" ) )
			->setPlaceholder( $t( "Land" ) )
			->setAutocomplete( $autocompletePrefix . " country-name" )
			->addSetCallback( function ( $value ) use ( $address ) {
				$address->setCountry( $value );
			});

		$outputGroup->addContents(
			( new BasicGroup() )
				->addContents(
					(new Text( $t( "Postcode & plaats" )  . ( $isRequired ? " *" : "")))->addClasses("pad-form-subheader"),
					$postalCode,
					$city,
					...($this->addressSettings->isAskForCountry() ?
						[$country] :
						[])
				)
		);

		if ( $isRequired ){
			$validator = new Required($t("Het volledige adres is verplicht"));
			foreach ([$street, $number, $postalCode, $city, $country] as $input){
				$input->addValidator($validator);
			}
		}

		return $outputGroup;
	}
}