<?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\HTMLControls\Input;
	use Phast\HTMLControls\InputType;
	use Phast\System;
	use Phast\WebControl;
	use Phast\WebControlAttribute;
	use Phast\WebControls\AdditionalDetailWidget;
	use Phast\WebControls\HiddenField;
	use Phast\WebControls\Menu;
	use Phast\WebControls\MenuItemCommand;
	use Phast\WebControls\TextBox;
	use Phast\WebControls\WebActionListCommand;

	class InstanceBrowser extends WebControl
	{
		public $Name;
		public $Text;
		public ?string $AutoCompleteUrl;

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

		public array $SelectedInstances;
		public array $ValidClasses;
		public string $Title;
		public $TargetUrl;

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

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

			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";
			}
			if ($this->DisplayInline)
			{
				$this->ClassList[] = "mcx-inline";
			}

			$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)));
			if ($this->AutoCompleteUrl !== null)
			{
				$this->Attributes[] = new WebControlAttribute("data-autocomplete-url", $this->AutoCompleteUrl);
			}

			/*
			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);
			*/
			
			$mobileExpansionTitleLabel = new WebControl();
			$mobileExpansionTitleLabel->TagName = "div";
			$mobileExpansionTitleLabel->ClassList[] = "uwt-title";
			if ($this->Title !== null)
			{
				$mobileExpansionTitleLabel->Content = $this->Title;
			}
			$this->Controls[] = $mobileExpansionTitleLabel;

			$hidden = new HiddenField();
			if ($this->Name !== null)
			{
				$hidden->Name = $this->Name;
			}
			$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);
				}
			}
			*/

			$txt = new Input();
			$txt->Type = InputType::Text;
			$this->Controls[] = $txt;

			$ul = new WebControl();
			$ul->TagName = "ul";
			$ul->ClassList[] = "mcx-selected-items";
			foreach ($this->SelectedInstances as $inst)
			{
				$li = new WebControl();
				$li->TagName = "li";

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

				$adw = new AdditionalDetailWidget("");
				$adw->RelatedActionButtonUrl = "~/d/inst/" . $inst->InstanceKey . "/rel-tasks.htmld";

				if (mocha_asset_exists("uic-assets", "1.1.5", "zq/icons/1$" . $inst->InstanceKey->ClassIndex . ".png")
				|| mocha_asset_exists("uic-assets", "1.1.5", "zq/icons/1$" . $inst->InstanceKey->ClassIndex . ".svg"))
				{
					$adw->ImageUrl = "~~/madi/asset/uic-assets/1.1.5/zq/icons/1$" . $inst->InstanceKey->ClassIndex;
				}
				$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;
				}
				
				if ($this->Text !== null)
				{
					$adw->Text = $this->Text;
				}
				else
				{
					$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;

				$aClose = new WebControl();
				$aClose->TagName = "a";
				$aClose->ClassList[] = "uwt-delete-button";
				$li->Controls[] = $aClose;

				$ul->Controls[] = $li;
			}
			$this->Controls[] = $ul;

			$div = new WebControl();
			$div->TagName = "div";
			$div->ClassList[] = "uwt-popup";
			$div->ClassList[] = "mcx-search-results";

			$ul = new Menu();
			$ul->MultiSelect = true;
			
			$li = new MenuItemCommand();
			$li->Title = "Test 1";

			$adw = new AdditionalDetailWidget("");
			$adw->ShowText = false;
			$li->Controls[] = $adw;

			$li->Selected = true;
			$ul->Items[] = $li;

			$li = new MenuItemCommand();
			$li->Title = "Test 2";
			$ul->Items[] = $li;
			
			$li = new MenuItemCommand();
			$li->Title = "Test 3";
			$ul->Items[] = $li;

			$div->Controls[] = $ul;

			$spinner = new WebControl();
			$spinner->TagName = "div";
			$spinner->ClassList[] = "uwt-spinner";
			$div->Controls[] = $spinner;

			$placeholder = new WebControl();
			$placeholder->TagName = "div";
			$placeholder->ClassList[] = "uwt-placeholder";
			$placeholder->Content = "type to search the list"; // Too many results. Please type to search.
			$div->Controls[] = $placeholder;

			$this->Controls[] = $div;

			/*
			$span = new WebControl();
			$span->TagName = "span";
			$span->ClassList[] = "mcx-empty-text";
			$span->Content = "(empty)";
			$this->Controls[] = $span;
			*/
			
			parent::OnInitialize();
		}

		
	}