<?php

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

use Planaday\Config\Booking\Form\Company\Invoice\InvoiceSettings;
use Planaday\Entities\Booking\PostRequest\BookingCompanyFieldEnum;
use Planaday\Entities\Booking\PostRequest\BookingInvoiceFieldEnum;
use Planaday\Html\Tags\Form\DataInputFieldTag;
use Planaday\Html\Tags\Form\SelectableDataInputFieldTag;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;

class InvoiceInput extends DataInputFieldTag {
	const TAG = 'invoice-input';
	const DEFAULT_TEMPLATE = 'invoiceInput';
	protected string $fieldName = BookingCompanyFieldEnum::INVOICE;

	protected InvoiceSettings $invoiceSettings;

	/**
	 * @param Container $registry
	 *
	 * @throws InjectionImpossibleException
	 */
	public function __construct(Container $registry ) {
		$this->invoiceSettings = $registry->getInstance(InvoiceSettings::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),
			BookingInvoiceFieldEnum::class
		);

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

		$parser->setCondition('showEmailInvoice', $this->invoiceSettings->isAskForEmail());
		$parser->setCondition('invoiceEmailMandatory', $this->invoiceSettings->isEmailRequired());
		$parser->setCondition('showFinancialInfoCompany', $this->invoiceSettings->isAskForAddress());

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