<?php

namespace Planaday\Html\Tags\Form\Booking\CompanyInput;

use Planaday\Config\Booking\Form\Company\CompanySettings;
use Planaday\Entities\Booking\PostRequest\BookingCompanyFieldEnum;
use Planaday\Entities\Booking\PostRequest\BookingFormFieldEnum;
use Planaday\Entities\PostEntities;
use Planaday\Html\Tags\Form\DataInputFieldTag;
use Planaday\Html\Tags\Form\SelectableDataInputFieldTag;
use Planaday\Platform\PlatformAbstraction\DataHandler;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;

class CompanyInput extends DataInputFieldTag {
	const TAG = 'company-input';
	const DEFAULT_TEMPLATE = 'companyInput';
	protected string $fieldName = BookingFormFieldEnum::COMPANY;

	protected DataHandler $dataHandler;
	protected CompanySettings $companySettings;

	/**
	 * @param Container $registry
	 *
	 * @throws InjectionImpossibleException
	 */
	public function __construct(Container $registry) {
		$this->companySettings = $registry->getInstance(CompanySettings::class);
		$this->dataHandler     = $registry->getInstance(DataHandler::class);
		parent::__construct(__DIR__, self::DEFAULT_TEMPLATE, $registry);
	}

	public function buildTag(): string {
		$parser = $this->getNewParser();
		$parser->addStylesheet($this->getStylePath());

		$parser->injectAttribute(
			$this->getRecursiveVersionOfAttribute(SelectableDataInputFieldTag::FIELD_CLASS_ATTRIBUTE),
			BookingCompanyFieldEnum::class
		);

		$parser->injectAttribute(
			SelectableDataInputFieldTag::FIELD_CLASS_ATTRIBUTE,
			BookingCompanyFieldEnum::class
		);

		$parser->setCondition('showCompanyPhoneNumber', $this->companySettings->isAskPhoneNumber());
		$parser->setCondition('companyPhoneNumberManditory', $this->companySettings->isPhoneNumberRequired());

		$showCompanyDetails = $this->dataHandler->getPostField(PostEntities::BOOKING_FORM)[BookingFormFieldEnum::AS_COMPANY] ?? $this->companySettings->isAsCompanyDefault();
		$parser->setData('displayCompanyDetails', $showCompanyDetails ? 'block' : 'none');

		return $parser->parse($this->getTemplate());
	}
}