<?php

namespace Planaday\Html\Tags\Test\TestTag;

use Planaday\Html\HtmlParser;
use Planaday\Html\Tags\Tag;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;

class TestTag extends Tag {
	const TAG = 'tag-test';
	const DEFAULT_TEMPLATE = 'testTag';

	/**
	 * @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->setAttributeDataItem($parser);
		$this->checkAttributes($parser);
		return $parser->parse($this->getTemplate());
	}

	private function checkAttributes(HtmlParser $parser):void {
		if (array_key_exists('displayGlobalData', $this->attributes)){
			$contents = file_get_contents(__DIR__ . '/Templates/' . 'displayGlobalData.html');
			if ($contents === false) return;
			$parser->setUnsafeData('displayGlobalDataAttribute', $contents);
		}
	}

	private function setAttributeDataItem(HtmlParser $parser):void {
		$attributeListString = '';

		foreach ($this->attributes as $attribute => $value){
			$attributeListString .= $attribute;
			$attributeListString .= ', ';
		}

		$parser->setData('testTagAttributes', $attributeListString);
	}
}