<?php

	namespace Mocha\UI\Pages;
	
	use Mocha\Core\InstanceKey;
	use Mocha\Core\KnownInstanceGuids;
	use Mocha\Core\KnownRelationshipGuids;

	use Mocha\Core\OmsContext;
	use Mocha\Oms\MySQLDatabaseOms;

	use Mocha\UI\Controls\InstanceBrowser;
	use Mocha\UI\Renderers\HTML\HTMLRenderer;
	use Phast\RenderingEventArgs;
	use Phast\WebPage;

	class InstancePage extends WebPage
	{

		protected function OnRendering(RenderingEventArgs $re)
		{
			parent::OnRendering($re);

			/**
			 * @var MySQLDatabaseOms
			 */
			$oms = mocha_get_oms();
			
			$instkey = InstanceKey::Parse($this->Page->GetPathVariableValue("instid"));

			$castkey = InstanceKey::Parse($this->Page->GetPathVariableValue("castinstid"));
			
			$inst = $oms->getInstanceByKey($instkey);
			if ($inst === null)
			{
				$ctx = new OmsContext();
				$htmlRenderer = new HTMLRenderer($ctx);

				$htmlRenderer->renderBeginPage("Error");
				?>
				<div class="uwt-panel" style="width: 600px; margin-left: auto; margin-right: auto; margin-top: 128px;">
					<div class="uwt-header">
						<h1>Invalid Instance</h1>
					</div>
					<div class="uwt-content">
						<p><?php echo("Invalid inst id " . $instkey . " (inst not found)"); ?></p>
					</div>
					<div class="uwt-footer">
						<a href="#">Back</a>
					</div>
				</div><?php
				$htmlRenderer->renderEndPage();

				exit();

				$instIdCtl = $this->Page->GetControlByID("instId");
				$castIdCtl = $this->Page->GetControlByID("castId");
				
				if ($castIdCtl !== null)
				{
					if ($castkey === null)
					{
						$castIdCtl->Content = "invalid inst id " . $castkey . " (inst not found)";
					}
					else
					{
						$castIdCtl->Content = strval($castkey);
					}
				}

				$instIdCtl->Content = "invalid inst id " . $instkey . " (inst not found)";
			}
			else
			{
				$ctx = new OmsContext();
				$htmlRenderer = new HTMLRenderer($ctx);

				$parentClass = $oms->getParentClass($inst);
				if ($castkey !== null)
				{
					$parentClass = $oms->getInstanceByKey($castkey);
				}

				$rel_Class__has_default__Task = $oms->getInstanceByGlobalIdentifier(KnownRelationshipGuids::Class__has_default__Task);
				if ($rel_Class__has_default__Task !== null)
				{
					$defaultTask = $oms->getRelatedInstance($parentClass, $rel_Class__has_default__Task);
					if ($defaultTask !== null)
					{
						$context = new OmsContext();
						$initiatingElement = $oms->getRelatedInstance($defaultTask, KnownRelationshipGuids::Task__has_initiating__Element);

						$context->setWorkData($oms->normalizeInstanceReference(KnownInstanceGuids::WorkSet__RelatedInstance), $inst);
						$context->setWorkData($parentClass, $inst);

						// echo ("instance for " . $parentClass->InstanceKey . " = " . $inst->InstanceKey);

						$taskRenderer = new HTMLRenderer($context);
						$taskRenderer->TargetInstance = $inst;
						$taskRenderer->IsPostback = $this->Page->IsPostback;
						if ($this->Page->IsPostback)
						{
							$taskRenderer->processPostback(element: $initiatingElement);
						}
						$taskRenderer->renderTask($defaultTask, $inst);
						exit();
					}
					else
					{
						$htmlRenderer->renderBeginPage("View Instance [Hardcoded]");
						
						$htmlRenderer->renderPageHeader("View Instance [Hardcoded]", $inst);
						$htmlRenderer->renderBeginContent();

						$htmlRenderer->renderBeginTag("div", [ ], [ "uwt-formview", "mcx-element" ]);

						$htmlRenderer->renderBeginTag("div", [ ], [ "mcx-elementcontent", "uwt-formview-item" ]);
						
						$htmlRenderer->renderBeginTag("div", [ ], [ "uwt-formview-item-label" ]);
						$htmlRenderer->renderBeginTag("label",[ ], [ ]);
						echo("Instance");
						$htmlRenderer->renderEndTag("label");
						$htmlRenderer->renderEndTag("div");

						$htmlRenderer->renderBeginTag("div", [ ], [ "uwt-formview-item-content" ]);
						$htmlRenderer->renderEndTag("div");

						$adw = new InstanceBrowser();
						$adw->SelectedInstances[] = $inst;
						$adw->Render();
	
						$htmlRenderer->renderEndTag("div");

						$htmlRenderer->renderBeginTag("div", [ ], [ "mcx-elementcontent", "uwt-formview-item" ]);
						
						$htmlRenderer->renderBeginTag("div", [ ], [ "uwt-formview-item-label" ]);
						$htmlRenderer->renderBeginTag("label",[ ], [ ]);
						echo("Parent Class");
						$htmlRenderer->renderEndTag("label");
						$htmlRenderer->renderEndTag("div");

						$htmlRenderer->renderBeginTag("div", [ ], [ "uwt-formview-item-content" ]);
						$htmlRenderer->renderEndTag("div");

						
						$adw = new InstanceBrowser();
						$adw->SelectedInstances[] = $parentClass;
						$adw->Render();

	
						$htmlRenderer->renderEndTag("div");

						$htmlRenderer->renderEndTag("div");

						$htmlRenderer->renderEndContent();
					}
				}

				$htmlRenderer->renderEndPage();

			}
		}

	}
?>