<?php
	namespace Mocha\UI\Controls;

	use Mocha\Core\InstanceKey;
	use Mocha\Core\InstanceReference;
	use Mocha\Core\KnownInstanceGuids;
	use Mocha\Core\KnownRelationshipGuids;
	use Mocha\Oms\MySQLDatabaseOms;

	use Phast\HTMLControl;
	use Phast\System;
	use Phast\WebControl;
	use Phast\WebControlAttribute;
	use Phast\WebControls\AdditionalDetailWidget;
	use Phast\WebControls\HiddenField;
	use Phast\WebControls\MenuItemCommand;

	class InstanceBrowser extends WebControl
	{
		public $Name;

		public bool $Editable;
		public bool $MultiSelect;
		public bool $DisplayAsCount;
		public bool $DisableLink;

		public array $SelectedInstances;
		public array $ValidClasses;

		public function __construct(array $instanceReferences = null)
		{
			parent::__construct();

			$this->Name = null;
			$this->TagName = "div";
			$this->ClassList[] = "mcx-instancebrowser";
			$this->Editable = false;
			$this->MultiSelect = false;
			$this->DisplayAsCount = false;
			$this->DisableLink = false;
			$this->ValidClasses = array();

			if ($instanceReferences === null)
			{
				$instanceReferences = array();
			}
			$this->SelectedInstances = $instanceReferences;
		}

		protected function OnInitialize()
		{
			/**
			 * @var MySQLDatabaseOms
			 */
			$oms = mocha_get_oms();

			if ($this->Editable) $this->ClassList[] = "mcx-editable";
			if ($this->MultiSelect) $this->ClassList[] = "mcx-multiselect";
			if (count($this->SelectedInstances) == 0)
			{
				$this->ClassList[] = "mcx-empty";
			}
			if ($this->DisplayAsCount)
			{
				$this->ClassList[] = "mcx-display-as-count";
			}

			$selectedInstanceIds = implode(",", InstanceReference::toInstanceKeys($this->SelectedInstances));
			$this->Attributes[] = new WebControlAttribute("data-instance-ids", $selectedInstanceIds);
			$this->Attributes[] = new WebControlAttribute("data-valid-class-ids", implode(",", InstanceReference::toInstanceKeys($this->ValidClasses)));

			/*
			System.Web.UI.WebControls.TextBox txt = new System.Web.UI.WebControls.TextBox();
			txt.AutoCompleteType = System.Web.UI.WebControls.AutoCompleteType.Disabled;
			txt.ID = String.Format("{0}_TextBox", this.ID);
			if (Text != null)
				txt.Text = Text;
			this.Controls.Add(txt);
			*/
			
			$hidden = new HiddenField();
			if ($this->Name !== null)
			{
				$hidden->Name = $this->Name . "_hidden";
			}
			$hidden->Value = $selectedInstanceIds;
			$this->Controls[] = $hidden;

			/*
			Oms oms = Page.GetOMS();

			System.Web.UI.HtmlControls.HtmlGenericControl ul = new System.Web.UI.HtmlControls.HtmlGenericControl("ul");

			if (DisplayAsCount)
			{
				System.Web.UI.HtmlControls.HtmlGenericControl li = new System.Web.UI.HtmlControls.HtmlGenericControl("li");
				System.Web.UI.HtmlControls.HtmlAnchor a = new System.Web.UI.HtmlControls.HtmlAnchor();
				a.AddCssClass("mcx-count-link");
				a.InnerText = InstanceReferences.Count.ToString();
				li.Controls.Add(a);
				ul.Controls.Add(li);
			}
			else
			{
				for (int i = 0; i < InstanceReferences.Count; i++)
				{
					Instance inst = oms.GetInstance(InstanceReferences[i]);

					System.Web.UI.HtmlControls.HtmlGenericControl li = new System.Web.UI.HtmlControls.HtmlGenericControl("li");

					MBS.Web.Controls.ActionPreviewButton apb = new MBS.Web.Controls.ActionPreviewButton();

					apb.PreviewContent.Controls.Add(new System.Web.UI.HtmlControls.HtmlGenericControl("h1") { InnerHtml = oms.GetInstanceText(inst) });

					apb.Attributes["data-preview-url"] = String.Format("~/api/preview/{0}", InstanceReferences[i]);
					apb.Attributes["data-instance-id"] = InstanceReferences[i].ToString();

					string tenantName = Page.GetCurrentTenantName();
					apb.TargetUrl = String.Format("~/{0}/d/inst/{1}.htmld", tenantName, InstanceReferences[i]);

					if (Text != null)
					{
						apb.Text = Text;
					}
					else if (oms != null)
					{
						if (inst != null)
						{
							apb.Text = oms.GetInstanceText(inst);
							if (String.IsNullOrEmpty(apb.Text))
							{
								apb.Text = oms.GetTTC(inst);
							}
						}
						else
						{
							apb.Text = String.Format("(NULL INSTANCE: {0})", InstanceReferences[i]);
						}
					}
					else
					{
						apb.Text = InstanceReferences[i].ToString();
					}

					li.Controls.Add(apb);
					ul.Controls.Add(li);
				}
			}
			*/


			$ul = new WebControl();
			$ul->TagName = "ul";
			foreach ($this->SelectedInstances as $inst)
			{
				$li = new WebControl();
				$li->TagName = "li";

				$instParent = $oms->getParentClass($inst);
				$parentClassName = $oms->getInstanceText($instParent);

				$adw = new AdditionalDetailWidget("");
				$adw->Attributes[] = new WebControlAttribute("data-instance-id", $inst->InstanceKey);
				$adw->ClassList[] = "mcx-moniker";
				$adw->ClassTitle = $parentClassName;

				$defaultTaskUrl = $oms->getDefaultTaskUrl($inst);
				if ($defaultTaskUrl !== null)
				{
					$adw->TargetURL = $defaultTaskUrl;
					$adw->ShowURL = true;
				}
				else
				{
					$adw->ShowURL = false;
				}
				
				$adw->Text = $oms->getInstanceText($inst);
				/*
				$this->MenuItems = array(

					new MenuItemCommand("Element Tests Testing Class 1", null, null, null, array(
						new MenuItemCommand("Delete")
					)),
					new MenuItemCommand("Debugging", null, null, null, array(
						new MenuItemCommand("Show in Code Editor")
					)),
					new MenuItemCommand("Favorite", null, null, null, array(
						new MenuItemCommand("Add to Favorites"),
						new MenuItemCommand("Manage Favorites")
					)),
					new MenuItemCommand("Integration IDs", null, null, null, array(
						new MenuItemCommand("View IDs"),
						new MenuItemCommand("Edit External IDs"),
						new MenuItemCommand("Edit Reference ID"),
						new MenuItemCommand("Maintain Reference IDs")
					)),
					new MenuItemCommand("Method Binding", null, null, null, array(
						new MenuItemCommand("View"),
						new MenuItemCommand("Edit")
					))
				);
				*/
				
				$li->Controls[] = $adw;
				$ul->Controls[] = $li;
			}
			$this->Controls[] = $ul;
			
			parent::OnInitialize();
		}

		
	}