<?php

	namespace Mocha\UI\Pages;
	use Mocha\Core\InstanceKey;
	use Mocha\Core\InstanceReference;
	use Mocha\Core\KnownRelationshipGuids;
	use Phast\CancelEventArgs;
	use Phast\EventArgs;
	use Phast\QuickSort;
	use Phast\RenderingEventArgs;
	use Phast\System;
	use Phast\WebPage;

	class RelatedTaskListPageAPI extends WebPage
	{
		protected function OnInitializing(CancelEventArgs $e)
		{
			$this->Page->ContentType = "application/json";
			$this->Page->MasterPage->ClassReference->RequireLogin = false;
			return true;
		}

		private function createRelatedTask(InstanceReference $inst, InstanceReference $task)
		{
			/** @var \Mocha\Oms\MySQLDatabaseOMS */
			$oms = mocha_get_oms();

			$label = $oms->getInstanceText($task);

			$json = array
			(
				"widget" => "relatedTaskGroup",
				"tasks" => array
				(
					array
					(
						"widget" => "relatedTask",
						"uri" => System::ExpandRelativePath("~/d/inst/" . $inst->InstanceKey . "/rel-task/" . $task->InstanceKey . ".htmld"),
						"view" => true,
						"webService" => false,
						"iid" => $task->InstanceKey->__toString(),
						"label" => $label
					)
				)
			);
			return $json;
		}

		private function createTaskGroup($name, $taskGroup)
		{
			$json = array
			(
				"widget" => "relatedTaskGroup",
				"renderDifferently" => false,
				"taskGroups" => array
				(
					array
					(
						"widget" => "relatedTaskGroup",
						"label" => $name,
						"renderDifferently" => false,
						"taskGroups" => $taskGroup
					)
				)
			);
			return $json;
		}

		protected function OnRendering(RenderingEventArgs $re)
		{
			/** @var \Mocha\Oms\MySQLDatabaseOMS */
			$oms = mocha_get_oms();

			if ($oms->getCurrentUser() === null)
			{
				$json = array
				(
					"result" => "failure",
					"responseCode" => 403,
					"responseText" => "Forbidden",

					"title" => "Not Authorized",
					"message" => "Please log in to perform this action"
				);
				echo (json_encode($json));
				$re->Handled = true;
				return true;
			}

			$tenant = $this->Page->GetPathVariableValue("tenant");
			$iid = $this->Page->GetPathVariableValue("instid");
			$inst = $oms->getInstanceByKey(InstanceKey::Parse($iid));
			$instParent = $oms->getParentClass($inst);

			$tasks = $oms->getRelatedTasks($inst);
			$taskGroupsDefs = [ ];
			
			$names = [ ];
			foreach ($tasks as $task)
			{
				$taskCategory = $oms->getRelatedInstance($task, KnownRelationshipGuids::Task__has__Task_Category);
				$name = $oms->getInstanceText($taskCategory);
				if (!in_array($name, $names))
				{
					$names[] = $name;
				}
				if (!array_key_exists($name, $taskGroupsDefs))
				{
					$taskGroupsDefs[$name] = array();
				}
				$taskGroupsDefs[$name][] = $this->createRelatedTask($inst, $task);
			}

			natsort($names);

			$taskGroups = [ ];
			foreach ($names as $name)
			{
				$taskGroups[] = $this->createTaskGroup($name, $taskGroupsDefs[$name]);
			}

			$json = array
			(
				"widget" => "relatedTasks",
				"taskGroups" => $taskGroups,
				"instance" => array
				(
					"instanceId" => strval($inst->InstanceKey),
					"label" => $oms->getInstanceText($instParent),
					"title" => $oms->getInstanceText($inst)
				)
			);
			$json["result"] = "success";

			$messages = array();
			foreach ($oms->Messages as $message)
			{
				$messages[] = array("title" => $message->Title, "content" => $message->Message, "severity" => $message->Severity);
			}
			$json["messages"] = $messages;

			echo(json_encode($json));
			$re->Handled = true;
		}

	}
?>