<?php

namespace Planaday\Html\Tags\Form\GenericInput\TextInput;

use Planaday\Html\Tags\Form\SelectableDataInputFieldTag;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;

class TextInput extends SelectableDataInputFieldTag {
	const TAG = 'text-input';
	const DEFAULT_TEMPLATE = 'textInput';

	/**
	 * @param Container $registry
	 *
	 * @throws InjectionImpossibleException
	 */
	public function __construct(Container $registry ) {
		parent::__construct(__DIR__, self::DEFAULT_TEMPLATE, $registry);
	}

	public function buildTag(): string {
		$parser = $this->getNewParser();
		$this->attributes[self::PLACEHOLDER_ATTRIBUTE] = $this->attributes[self::PLACEHOLDER_ATTRIBUTE] ?? $this->getSelectedFieldTitle();
		return $parser->parse($this->getTemplate());
	}

	public static function getTagHtml(string $fieldName, string $label, string $title, string $value):string {
		return '<pad-' . self::TAG . ' ' .
		       self::FIELD_TITLE_ATTRIBUTE . '="' . htmlspecialchars($label) . '" ' .
		       self::FIELD_NAME_ATTRIBUTE . '="' . htmlspecialchars($fieldName) . '" ' .
		       self::PLACEHOLDER_ATTRIBUTE . '="' . htmlspecialchars($title) . '" ' .
		       self::FIELD_SET_VALUE_ATTRIBUTE . '="' . htmlspecialchars($value) . '" />'
			;
	}
}