--===================================================
--
--				GHI_ScriptMenu
--  			GHI_ScriptMenu.lua
--
--	         Simple action menu
--
-- 	  (c)2012 The Gryphonheart Team
--			All rights reserved
--===================================================
 local loc = GHI_Loc()
local USE_WIDE_EDITOR = false;
local menus = {};

GHI_ScriptMenu_UseWideEditor = function(useWide)
	if USE_WIDE_EDITOR == useWide then
		return;
	end
	USE_WIDE_EDITOR = useWide;
	for i, menu in pairs(menus) do
		menu.Suspend();
	end
end


local miscAPI;
local icon = "Interface\\Icons\\Trade_Engineering";
function GHI_ScriptMenu(_OnOkCallback, _editAction)
	if not (miscAPI) then miscAPI = GHI_MiscAPI().GetAPI(); end
	local playerGUID = UnitGUID("player");

	for i, menu in pairs(menus) do
		if _editAction and menu.IsInUse() and menu.editAction == _editAction then
			miscAPI.GHI_Message(loc.ACTION_BEING_EDITED);
			return;
		end
	end
	for i, menu in pairs(menus) do
		if not (menu.IsInUse()) and not (menu.IsSuspended()) then
			menu.Show(_OnOkCallback, _editAction)
			return menu
		end
	end
	local class = GHClass("GHI_ScriptMenu");
	table.insert(menus, class);

	local menuFrame, OnOkCallback;
	local suspended = false;
	local inUse = false;
	local menuIndex = 1;
	while _G["GHI_ScriptMenu" .. menuIndex] do menuIndex = menuIndex + 1; end


	class.Show = function(_OnOkCallback, _editAction)
		OnOkCallback = _OnOkCallback;
		inUse = true;
		if (_editAction) then
			class.editAction = _editAction;
			local info = class.editAction.GetInfo();
			menuFrame.ForceLabel("delay", info.delay);
			menuFrame.ForceLabel("code", string.join("", unpack(info.code)));
		else
			class.editAction = nil;
			menuFrame.ForceLabel("delay", "");
			menuFrame.ForceLabel("code", "");
		end

		menuFrame:Show();
	end

	class.IsInUse = function() return inUse; end
	class.IsSuspended = function() return suspended; end
	class.Suspend = function() suspended = true; end

	local OnOk = function()
		local action;
		local code = menuFrame.GetLabelFrame("code"):GetCleanText();
		local delay = menuFrame.GetLabel("delay");
		local t = {
			Type = "script",
			type_name = loc.SCRIPT,
			icon = icon,
			details = string.sub(code, 0, 100),
			code = { code },
			delay = delay,
		};

		if (class.editAction) then
			action = class.editAction;
			action.UpdateInfo(t);
		else
			action = GHI_SimpleAction(t);
		end

		if OnOkCallback then
			OnOkCallback(action);
		end
		inUse = false;
		menuFrame:Hide();
	end

	menuFrame = GHM_NewFrame(class, {
		onOk = function(self) end,
		{
			{
				{
					type = "CodeField",
					height = 430,
					text = loc.SCRIPT .. ":",
					width = USE_WIDE_EDITOR and 780 or 380,
					label = "code",
					align = "c",
				},
			},
			{
				{
					align = "l",
					type = "Editbox",
					text = loc.DELAY;
					label = "delay",
					width = 80,
					texture = "Tooltip",
					numbersOnly = true,
				},

				{
					type = "Dummy",
					height = 10,
					width = 10,
					align = "r",
				},
				{
					type = "Button",
					text = loc.SCRIPT_OPTIONS_NL,
					align = "r",
					compact = false,
					yOff = -5,
					OnClick = function(obj)
						GHI_CodeEditorOptionsMenu().Show();
					end,
				},
				{
					type = "Dummy",
					height = 10,
					width = 10,
					align = "r",
				},
				{
					type = "Button",
					text = loc.SCRIPT_RELOAD_ENV,
					align = "r",
					compact = false,
					yOff = 0,
					OnClick = function(obj)
						local code = menuFrame.GetLabelFrame("code"):GetCleanText();
						local env = GHI_ScriptEnvList().ReloadEnv(playerGUID);
					end,
				},
				{
					type = "Dummy",
					height = 10,
					width = 10,
					align = "r",
				},
				{
					type = "Button",
					text = loc.SCRIPT_TEST,
					align = "r",
					compact = false,
					OnClick = function(obj)
						local code = menuFrame.GetLabelFrame("code"):GetCleanText();
						local env = GHI_ScriptEnvList().GetEnv(playerGUID);
						env.ExecuteScript(code)
					end,
				},
			},
			{
				{
					type = "Dummy",
					height = 10,
					width = 10,
					align = "c",
				},
			},
			{
				{
					type = "Dummy",
					height = 10,
					width = 10,
					align = "l",
				},
				{
					type = "Text",
					fontSize = 11,
					width = USE_WIDE_EDITOR and 780 or 380,
					text = loc.SCRIPT_TEXT,
					color = "white",
					align = "l",
				},
			},
			{
				{
					type = "Dummy",
					height = 10,
					width = 10,
					align = "c",
				},
			},
			{
				{
					type = "Dummy",
					height = 10,
					width = 100,
					align = "l",
				},
				{
					type = "Button",
					text = OKAY,
					align = "l",
					label = "ok",
					compact = false,
					OnClick = OnOk,
				},
				{
					type = "Dummy",
					height = 10,
					width = 100,
					align = "r",
				},
				{
					type = "Button",
					text = CANCEL,
					align = "r",
					label = "cancel",
					compact = false,
					OnClick = function(obj)
						menuFrame:Hide();
					end,
				},
			},
		},
		title = loc.SCRIPT,
		name = "GHI_ScriptMenu" .. menuIndex,
		theme = "BlankTheme",
		width = USE_WIDE_EDITOR and 800 or 400,
		useWindow = true,
		--background = "INTERFACE\\GLUES\\MODELS\\UI_BLOODELF\\bloodelf_mountains",
		OnShow = UpdateTooltip,
		icon = icon,
		OnHide = function()
			if not (menuFrame.window:IsShown()) then
				inUse = false;
			end
		end,
	});

	menuFrame.GetLabelFrame("code").field.executionEnv = GHI_ScriptEnvList().GetEnv(playerGUID);

	class.Show(_OnOkCallback, _editAction)

	return class;
end

