<?php

	namespace Mocha\UI\Renderers\HTML;

	use Mocha\Core\OmsContext;
	use Phast\System;

	class HTMLRenderer2 extends HTMLRenderer
	{
		public function __construct(OmsContext $context, bool $withoutOms = false)
		{
			parent::__construct($context, $withoutOms);
		}

		public function renderInitialElement($element)
		{
			$json = $this->OMS->getResponse($element);
			$this->renderResponse($json["value"]);
		}

		public function renderResponse(array $json)
		{
			$this->renderBeginPage("Test App");
			$this->renderBeginContent();

			$this->renderFragment($json);

			$this->renderEndContent();
			$this->renderEndForm();
			$this->renderEndPage();
		}

		public function renderFragment(array $json, $fqecidPrefix = "")
		{
			$fqecid = $fqecidPrefix . $json["ecid"];
			if ($json["visible"] === false)
			{
				return;
			}
			
			if ($json["widget"] == "root")
			{
				$this->renderFragment($json["body"]);
			}
			else if ($json["widget"] == "vbox")
			{
				echo ("<div class=\"uwt-layout-box uwt-orientation-vertical\">");
				foreach ($json["children"] as $child)
				{
					$this->renderFragment($child);
				}
				echo ("</div>");
			}
			else if ($json["widget"] == "image")
			{
				echo ("<div class=\"uwt-image\" style=\"background-image: url('" . System::ExpandRelativePath("~/attachment/" . $json["iid"] . "/" . $json["target"]) . "');");
				if (isset($json["width"]))
				{
					echo(" width: " . $json["width"] . ";");
				}
				if (isset($json["height"]))
				{
					echo(" height: " . $json["height"] . ";");
				}
				if (isset($json["horizontalAlignment"]) && $json["horizontalAlignment"] == "center")
				{
					echo(" margin-left: auto; margin-right: auto;");
				}
				echo("\">&nbsp;</div>");
			}
			else if ($json["widget"] == "fieldSet")
			{
				echo ("<div class=\"uwt-formview mcx-element\" data-instance-id=\"" . $json["iid"] . "\" data-ecid=\"" . $fqecid . "\">");
				foreach ($json["children"] as $child)
				{
					$fqecidChild = $fqecidPrefix . $child["ecid"];
					echo ("<div class=\"uwt-formview-item mcx-elementcontent");
					if (!isset($child["label"]))
					{
						echo(" uwt-hide-label");
					}
					if (!isset($child["enabled"]) || $child["enabled"] == true)
					{
						echo(" mcx-editable");
					}
					if (isset($child["required"]) && $child["required"] == true)
					{
						echo(" mcx-required");
					}
					echo ("\" data-instance-id=\"" . $child["iid"] . "\" data-ecid=\"" . $child["ecid"] . "\">");
						
					echo ("<div class=\"uwt-formview-item-label\">");
					echo ("<label for=\"ec_" . $fqecidChild . "\">");
					echo ($child["label"]);
					echo ("</label>");
					echo ("</div>");

					echo ("<div class=\"uwt-formview-item-content\"");
					/*
					if (!isset($child["label"]))
					{
						echo(" colspan=\"2\"");
					}
					*/
					echo (">");
					$this->renderFragment($child, $fqecid . ":");
					echo ("</div>");
					echo ("</div>");
				}
				echo ("</div>");
			}
			else if ($json["widget"] == "text")
			{
				if ($json["enabled"] === false)
				{
					echo ("<p data-ecid=\"" . $json["ecid"] . "\">" . $json["value"] . "</p>");
				}
				else
				{
					echo ("<input id=\"ec_" . $fqecid . "\" name=\"ec_" . $fqecid . "\" type=\"text\" value=\"" . $json["value"] . "\"");
					if (isset($json["maximumLength"]))
					{
						echo ("maxlength=\"" . $json["maximumLength"] . "\"");
					}
					echo (" />");
				}
			}
			else if ($json["widget"] == "obscuredText")
			{
				echo ("<input id=\"ec_" . $fqecid . "\" name=\"ec_" . $fqecid . "\" type=\"password\" value=\"" . $json["value"] . "\" />");
			}
		}
	}

?>