-----------------------------------------------------------
-- VARIABLE PANEL
-----------------------------------------------------------
function editor.defineVariablePanel()
	variablePanel = 
	{ 
		name 		= "Variables",
	 	height 		= 100,
	 	open 		= false,
	 	docked  	= true,
	 	y 			= 0,
	 	row 		= sidebar.row,
	 	currentTab 	= "Level",
	 	vars 		= {},
	 	tabs 		= {
			{	name="Game", 		hovered=false, selected=false 	},
			{	name="World", 	 	hovered=false, selected=false 	},
			{	name="Level", 		hovered=false, selected=true 	},
			{	name="Object", 		hovered=false, selected=false 	}
	  	},

	 	update 	= function(x) editor.variablePanelUpdate(x) end
	}
end

function editor.variablePanelUpdate(panel)
	panel.height=sidebar.titleH

	editor.togglePanel(panel)
	
	if not panel.open then  return 0  end 
	
	if #selectedEntities == 1 and #panel.tabs==3 then 
		table.insert(panel.tabs , {	name="Object", 		hovered=false, selected=false 	})
		-- temp auto switch variable tab to instance on select
		-- panel.currentTab = "Object"
	end

	if #selectedEntities ~= 1 and #panel.tabs==4 then 
		table.remove(panel.tabs,4)
		panel.currentTab = "Level"
	end

	local tab = panel.tabs[editor.getVariableTab(panel,true)]
	if tab~=nil then 
		panel.height 	= 	panel.height + tab.height+2 
	end

	if 		panel.currentTab == "Object" then 
		panel.vars 	=	selectedEntities[1]
	elseif 	panel.currentTab == "Level" then 
		panel.vars 	=	level.variables
	elseif 	panel.currentTab == "World" then 
		panel.vars 	= worlds[currentWorld]
	elseif 	panel.currentTab == "Game" then 
		panel.vars 	= global
	end	

	if panel.vars.variables == nil then 
		panel.vars.variables = {}
	end

	table.sort(panel.vars.variables, function(a, b) return a.name < b.name end)

	for k,var in pairs(panel.vars.variables) do
		
		-- DELETE ITEM 
		-----------------------------------------------------------
		local deleteButton = { 
			x	= panel.x+panel.row*0.3,
			y	= panel.y+panel.height+panel.row*0.1,
			w	= panel.row*0.8,
			h	= panel.row*0.8,
			text= "╳" ,
			col= {a=0,t=255}
		}
		deleteButton.hovered = mouseIn(deleteButton)
		if deleteButton.hovered and mouse.click() then 
			namer:warning("delete this variable?",function() 
				panel.vars.variables[k]=nil
			end)
		end
		editor.drawButton(panel,deleteButton) 

		
		-- TYPE
		-----------------------------------------------------------
		local varType = {
			w	= panel.row*2.4,
			h	= panel.row,
			x	= deleteButton.x+deleteButton.w+4,
			y	= panel.y+panel.height,
			text= var.type,
			col = {r=60,g=80,b=70}
		}
		varType.hovered = mouseIn(varType)
		if varType.hovered then 
			editor.setToolTip("Variable type")
			if mouse.click() then 
				if 		var.type == "Number" 	then var.type = "String" 
				elseif 	var.type == "String" 	then var.type = "Boolean" 
				elseif 	var.type == "Boolean" 	then var.type = "Number" 	end
				editor.setNewVar(var)
			end
		end
		editor.drawButton(panel,varType) 


		-- NAME
		-----------------------------------------------------------
		local varName = { 	
			x 		= varType.x+varType.w,
			y 		= panel.y+panel.height, 
			w 		= (panel.width-panel.row*5)/2, 
			h  		= panel.row,
			col 	= {r=120,g=60,b=60},
			text 	= var.name
		}
		varName.text 	= var.name:sub(1,mf(varName.w/7))
		if #var.name > #varName.text then 
			varName.text = varName.text.."..."
		end
		varName.hovered = mouseIn(varName)
		if varName.hovered then
			editor.setToolTip("Variable name")
			if mouse.click() then 
				namer:typeVar("Variable Name", var.name, panel, k, "name")
			end
		end
		 editor.drawButton(panel,varName) 


		-- VALUE
		-----------------------------------------------------------
		local varValue = { 	
			x 		= varName.x+varName.w,
			y 		= panel.y+panel.height, 
			w 		= (panel.width-panel.row*5)/2, 
			h  		= panel.row
		}
		varValue.text 	= tostring(var.value):sub(1,mf(varValue.w/7)) 
		varValue.hovered = mouseIn(varValue)
		if varValue.hovered then
			editor.setToolTip("Value")
			if mouse.click() then 
				if var.type == "String" or var.type == "Number" then
					namer:typeVar(varName.text, var.value, panel, k, "value", var.type)
				elseif var.type == "Boolean" then
					var.value=not var.value
				end
			end
		end
		 editor.drawButton(panel,varValue) 


		-- ENABLE CHECKBOX
		-----------------------------------------------------------
		local checkButton = { 
			x	= varValue.x+varValue.w,
			y	= panel.y+panel.height,
			w	= panel.row,
			h	= panel.row,
			text= var.save and "ticked" or "unticked" -- untickedticked
		}
		checkButton.hovered = mouseIn(checkButton)
		if checkButton.hovered then 
			editor.setToolTip("Save Value")
			if mouse.click() then 
				var.save=not var.save 
			end
		end
		editor.drawButton(panel,checkButton) 
		

		-----------------------------------------------------------
		panel.height = panel.height + panel.row*1.1
		
	end

	editor.newVariableButton(panel)

	
end

function editor.newVariableButton(panel)
	local newVarButton = { 	x	=	panel.x+(panel.width-panel.row*1.1),
							y	=	panel.y,
							w	=	panel.row,
							h	=	panel.row,
							text=	"+" }
	newVarButton.hovered = mouseIn(newVarButton)
	if newVarButton.hovered and mouse.click() then
		editor.newVariable(panel)
	end
	editor.drawButton(panel,newVarButton) 
end

function editor.newVariable(panel)
	local varID = uuid()
	
	-- types: Number, String, Boolean
	local newVar = {
		name 	= "var"..varID:sub(1,5),
		type	= "Number",
		temp 	= false,
		save 	= false,
	}

	editor.setNewVar(newVar)

	panel.vars.variables[varID] = newVar
	
	--temp
	--table.insert(panel.vars.variables, newVar)
end

function editor.setNewVar(newVar)
	if 		newVar.type=="Number" then 
		newVar.value 	=   10
	elseif 	newVar.type=="String" then
		newVar.value 	= "blank"
	elseif 	newVar.type=="Boolean" then
		newVar.value 	= false
	end
end



function editor.setVariableTab(panel,tab)
	if tab.hovered then 
		lmouse.setCursor(hand)
		mouse.click(function() 
			panel.currentTab=tab.name 
			panel.actualTab =tab.actualName
			tab.selected 	=true 
		end) 
	end
end

function editor.getVariableTab(panel,process)
	local selectedTab
	for i=1, #panel.tabs do 
		local tab 	= panel.tabs[i]
		selectedTab = tab.selected and i or selectedTab
		if process then 
			tab.selected= (panel.currentTab==tab.name)
			tab.t=i
			editor.processTab(panel,tab)
			editor.setVariableTab(panel,tab)
			editor.drawTabTitle(tab) 
		end
	end
	return selectedTab
end





