<?php

namespace Planaday\Html\Tags\Admin\ConnectorCheck;

use Planaday\Html\Tags\Tag;
use Planaday\Utility\Debug\Checking\ConnectorChecker;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;

class ConnectorCheck extends Tag {
	const TAG = 'admin-check';
	const DEFAULT_TEMPLATE = 'infoResult';
	const CHECK_TYPE_ATTRIBUTE = 'data-check-type';

	protected ConnectorChecker $checker;

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

	public function buildTag(): string {
		$parser = $this->getNewParser();
		$check = $this->checker->getCheck($this->attributes[self::CHECK_TYPE_ATTRIBUTE] ?? '');

		if ($check === null){
			$parser->setData('message', 'Check type not found or failed to check');
			return $parser->parse($this->getTemplate('failedResult'));
		}

		$checkResult = $check->check($message);
		$parser->setUnsafeData( 'message', $message );

		return $parser->parse( $this->getTemplate('', $checkResult . 'Result') );
	}
}