-----------------------------------------------------------
-- GENERAL PANEL FUNCTIONS
-----------------------------------------------------------

-----------------------------------------------------------
-- DEFINE PANELS
-----------------------------------------------------------
function editor.definePanels()
	
	editor.defineHeaderPanel()
	editor.defineOverviewPanel()
	editor.defineScriptsPanel()
	editor.defineVariablePanel()
	editor.definePathsPanel()
	editor.defineDrawingPanel()
	editor.defineAnimationPanel()
	editor.defineGalleryPanel()
		
	panels 	= { 
		  headerPanel
		, overviewPanel
		, variablePanel
		, scriptsPanel
		, pathsPanel
		, drawingPanel
		, animationPanel
		, galleryPanel	
	}
end

-----------------------------------------------------------
-- UPDATE PANELS
-----------------------------------------------------------
function editor.updatePanels()
	--game.startProfiling 	()

	editor.onPanelClick()
	editor.positionSidebar()
	editor.toggleSidebar()
	editor.drawSidebarBase()
	
	local totalHeight = 4
	local panelWidth  = sidebar.width-18

	-- if not animationPanel.open then 
		sidebar.visible = false
	-- end
	sidebar.x=screenWidth+1000

	for i,panel in ipairs(panels) do 
		panel.isPanel = true
		panel.x 		= panel.x 		or 0
		if panel.docked and not panel.bottom then 

			panel.oldY 		= 	panel.y
			panel.oldHeight	=  	panel.height
			panel.y 		= 	easeTo(panel.y,totalHeight,2)
			panel.x 		= 	sidebar.x+7

			if not panel.hide then 
				panelheight 	=	panel.open and (panel.height) or sidebar.titleH 
				totalHeight 	= 	totalHeight + (panel.open and panel.height+2 or sidebar.titleH) 
			end
		end
	end
	

	sidebar.height 	  = 2-- easeTo(sidebar.height, totalHeight , 2)

	--editor.titlebar = {x=-10,y=0,width=screenWidth+20,height=20,alpha=1,frost=0}
	editor.autoSaveButton = {x=screenWidth-20,y=0,w=20,h=20,corner=10,col={a=0.4},func=function() editor.autoSaveEnabled = not editor.autoSaveEnabled end }
	if mouseIn (editor.autoSaveButton) then 
		stageHovered=false
		editor.drawButton(editor.autoSaveButton)
	end
	editor.autoSaveGraphic 	({x=screenWidth-20,y=10,width=20,line=true})


	local preHovered = true
	if not stageHovered then 
		mouse.set(nothing)
		mouse.setR(nothing)
		mouse.setM(nothing)
	end
	gfx.fontSize(12)
	
	for i,panel in ipairs(panels) do

		panel.width = panel.docked and panelWidth or panel.width
		if not panel.docked and panel.drag then 
			editor.dragPanel(panel)
		end
		if not panel.hide then
			editor.drawPanel(panel)
		end
		if not panel.docked then 
			gfx.setShader 	(shaders.alpha) 
			shaders.alpha:send 		("alpha",panel.alpha or 1) 
			panel.x 		= mc(16,panel.x or 0,screenWidth-panel.width-16) 
	end
		panel.update(panel)
		if not panel.hide and (panel.open or not panel.docked) and mouseIn(panel.border or panel) then 
			panel.hovered = true
			preHovered = false
		else
			if  (contextMenu.open and mouseIn(contextMenu))  then
		 		preHovered=false
			elseif  panel.docked==false and panel~=animationPanel then 
				if not mouseIn(panel.border or panel) then 
					mouse.pushDown(function() 
						panel.focused=false
					end)
				end
				if not contextMenu.open and not panel.stayFloat or sidebar.visible==true  then 
					mouse.pushDown(function() 
						 editor.dockPanel(panel)
					end)
				end 
			end
		end
	end
	if mouse.locked or (roomsView and altDown and not animationPanel.open) or  mouseIn({x=sidebar.x-sidebar.resizeSpace/2,y=sidebar.y,w=sidebar.width+sidebar.resizeSpace,h=sidebar.height+8}) then 
		preHovered = false
	end
	stageHovered = preHovered



	if not stageHovered then 
		lmouse.setVisible(true)
	end
	--game.stopProfiling()
end

function editor.dockPanel(panel,func)
	effects.tween(panel,{y=panel.y+16,alpha=0},0.1,"easeIn",0.5,function() 
		panel.x 		= screenWidth 
		panel.y 		= screenHeight 
		panel.docked 	= true 
		(func or nothing)()
	end)
end

function editor.floatPanel(panel,target)
	if not panel.docked then 
		--editor.dockPanel(panel)
	else
		target 			= target or mouse
		panel.docked 	= false
		panel.x 		= target.x 
		panel.y 		= math.min((target.y)+16,screenHeight-200)
		panel.alpha 	= 0.005
		editor.drawingPanelUpdate(drawingPanel)
		effects.tween 	(panel,{y=panel.y-16,alpha=1},0.1,"easeOut",1)
	end
end

function editor.toggleSidebar()
	if keypressed("tab") and editingLevel then 
		sidebar.visible = not sidebar.visible 
		sidebar.touched = true
	end
	sidebar.width 	= mc(sidebar.minWidth, sidebar.width, sidebar.maxWidth)
end

function editor.positionSidebar()
	sidebar.minWidth= animationPanel.open and 260 or sidebar.splitWidth
	local targetX 	= sidebar.visible and screenWidth-sidebar.width+4 or screenWidth+50 
	sidebar.x 		= easeTo(sidebar.x, targetX, 4)
	sidebar.alpha 	= 1 - (sidebar.x-(screenWidth-sidebar.width+4)) / ((screenWidth+50) - (screenWidth-sidebar.width+4))
end

function editor.activateSidebar()
	sidebar.touched = false
	sidebar.wasOpen = sidebar.visible
	sidebar.visible = true
end

function editor.deactivateSidebar()
	--if not sidebar.touched then 
		sidebar.visible = sidebar.wasOpen
	--end
end


function editor.saveSize()
	sidebar.oldwidth 	= sidebar.width 	
	sidebar.oldheight	= sidebar.height 	
end

function editor.resizePanel()
	
	--sidebar.width 		= mc(sidebar.minWidth, screenWidth-mouse.x, sidebar.maxWidth)
	
	sidebar.width 	= mf(easeTo(sidebar.width, mc(sidebar.minWidth, screenWidth-mouse.x+sidebar.resizeSpace, sidebar.maxWidth),2) )

	sidebar.x 		= mf(easeTo(sidebar.x, mc(screenWidth-sidebar.maxWidth, mouse.x-sidebar.resizeSpace, screenWidth-sidebar.minWidth)+4,2))

	lmouse.setCursor(resizeCursor)

end

-----------------------------------------------------------
-- PANEL CLICKY THINGS
-----------------------------------------------------------
function editor.onPanelClick()
	if mouseIn ({x=sidebar.x-sidebar.resizeSpace/3, w=sidebar.resizeSpace, y=sidebar.y,h=sidebar.height}) then 
		trace("ready to resize")
		lmouse.setCursor(resizeCursor)
		mouse.pushClick(function ()
			prevState = editState..""
			editState = "Resizing Panel"
			log("Resizing Panel")
			mouse.pushRelease(function()
				editState = prevState
			end)
		end)
	end
end

function editor.togglePanelWithSelected(panel,one)
	if #selectedEntities==0 or anySelectedSimple or (one and #selectedEntities>1) then 
		panel.hide=true  
	elseif (one and #selectedEntities==1) or (not one and #selectedEntities>0) then 
		panel.hide=false
		editor.togglePanel(panel,false)
	end
end

function editor.togglePanel(panel,closeOnly,func)
	local closeOnly = closeOnly or false

	if not (not panel.open and panel.hide)and mouseIn({x=panel.x, y=panel.y, width=math.max(panel.width/4,50), height=sidebar.titleH}) then 
		mouse.set(function()
			if closeOnly then 
				panel.open = false 
			else
				panel.open = not panel.open 
			end
			local funced = func and func() or nil
		end)
	end
end

function editor.dragPanel(panel)
	if not (not panel.open and panel.hide) and not panel.docked and mouseIn(lume.merge(panel.border or panel, {h=sidebar.titleH})) then
		mouse.set(function()
			panel.dragOffset = {x=mouse.x-panel.x ,y=mouse.y-panel.y}
			panel.dragging = true
			editState  		 = "Dragging"
		end)
	end

	if editState == "Dragging" and panel.dragging then 
		panel.x = mc(16,mouse.x-panel.dragOffset.x,screenWidth-(panel.border and panel.border.width or panel.width)-16)
		panel.y = mc(16,mouse.y-panel.dragOffset.y,screenHeight-(panel.border and panel.border.height or panel.height)-16)
		mouse.pushRelease(function()
			editState  		 = "Normal"
		end)
	end
end

-----------------------------------------------------------
-- DRAW 
-----------------------------------------------------------
function editor.drawSidebarBase()
	if sidebar.height<30 or sidebar.x>screenWidth then return end
	sidebar.darkness = easeTo(sidebar.darkness, sidebar.darkMode and sidebar.darkShade or sidebar.lightShade , 4)
	gfx.setBlendMode('alpha') 
	editor.drawContextBack({x=sidebar.x+2,y=sidebar.y,width=sidebar.width+20,height=sidebar.height+6,frost=1,shadow=0})
end

function editor.drawPanel(panel)
	
	gfx.setBlendMode('alpha') 

	if (not panel.docked) or panel.drawBack or panel.backTex  then
		if panel.drawBack==true or type(panel.drawBack)=="nil"   then
			editor.drawContextBack({x=panel.x-2,y=panel.y-4,width=panel.width+8,height=panel.height+8+(panel.bonusHeight or 0),alpha=panel.alpha or 1,color=panel.color,frost=0,shadow=0.2,sharp=true})
		elseif panel.backTex then 
			gfx.setColor(1,1,1,panel.alpha)
			gfx.draw(panel.backTex,0,0,0,panel.width/panel.backTex:getWidth(),panel.height/panel.backTex:getHeight())
		end
	else 
		
	end
	
	editor.drawPanelTitle(panel)
	
end

function editor.drawPanelTitle(panel)
	gfx.setFont(fonts.inconsolata,12)
	gfx.setColor(1,1,1,panel.open and 1        or 0.4)
	-- gfx.print(panel.name,panel.x+4,mf(panel.y-2));
end



