-----------------------------------------------------------
-- HEADER PANEL
-----------------------------------------------------------
function editor.defineHeaderPanel()
	local file = debug.getinfo(1).source:sub(2,999)
	local line = debug.getinfo(1).currentline

	headerPanel = { 	
		name 	= "Level:"..level.name,
	 	height 	= 80,
	 	open 	= true,
	 	docked  = true,
	 	y 		= 0,
		tools 	=	{	
			{ 	
				{ 
					size=1.5, name="Level" ,  func = function () 
						editor.openContextMenu(editor.contextLevels())
					end
				},
				{ size=3, 	func = function() editor.openContextMenu( editor.editBackground(level) ) end} 
			},
			{ 	
				{ 
					size=1.5, name="Room" ,  func = function () 
						editor.openContextMenu( {
							{	  name="Rename",  func  = function () 
								namer:typeString("Rename Room",room.name,nil,function(x)room.name=x end) 
							end},
						})
					end
				},
				{ size=3, 	func = function() editor.openContextMenu( editor.editBackground(room)  )end} 
			},
			{ 
				{ name="Shapes" },
				{ name="Port", 		 func = editor.editPort 	},
				{ name="Floor", 	 func = editor.editFloor	},  
			},
			{ 	
				{ name="Camera" },
				{ name="Set", 		 func = editor.setCamera 	},
				{ name="Reset", 	 func = editor.resetCamera	},  
			},
			{
				{ name="Play", 		 func = editor.playLevel },
				{ name="Save", 		 func = editor.save  },
				{ name="Quit", 	 	 func = editor.exitEditor  }
			}	
		},
	 	update 	= editor.headerPanelUpdate
	}
end

function editor.contextLevels()
	local items = {
	}

	for w,world in ipairs(worlds) do 
		local worldMenu = {
			{ 	
				size  	= 8,
				name 	= "",
				update 	= function(x) 
					x.name = world.name 
					x.col  = game.world==w and {r=0.4,g=0.4,b=0.4,a=0.5} or {a=0}
				end,
				items 	= {}
			},
			{
				size  = 1,
				col = {a=120},
				name  = "···",  
				func  = function() namer:typeString("Rename World",world.name,getMouse(),function(x) world.name=x end) return "rename" end
			}
		}
		for l,level in ipairs(world.levels) do 
			table.insert(worldMenu[1].items,{
				{
					size=8,
					name="",
					update=function(x) 
						x.name = levels[level.key].name 
						x.col  = game.level.key==level.key and {r=0.4,g=0.4,b=0.4,a=0.5} or {a=0}
					end,
					func=function() game.enterGame(level.key) return "" end
				},
				{
					size=1,
					col = {a=120},
					name  = "···",  
					func  = function() namer:typeString("Rename Level",levels[level.key].name,getMouse(),function(x) levels[level.key].name=x end) return "rename" end
				}
			})
		end
		table.insert(worldMenu[1].items,{name="New Level", col = {r=0.2,g=0.7,b=0.3,a=0.3}, func=function()
			namer:typeString("New Level","",{x=mouse.x,y=mouse.y},function(x)
				loader:addLevel(x,w)
				local selected = contextMenu.selected
				editor.openContextLevels()
				contextMenu.selected = selected
			end)
			return "new"
		end})
		table.insert(items,worldMenu)
	end
	table.insert(items,{name="New World", col = {r=0.2,g=0.7,b=0.3,a=0.3}, func=function()
		namer:typeString("New World","World "..(lume.count(worlds)+1),{x=mouse.x,y=mouse.y},function(x)
			table.insert(worlds,{
				name 	= x,
				levels 	= {},
				index 	= #worlds+1
			})
			loader:saveDef("worlds",worlds) 
			editor.openContextLevels()
		end)
		return "new"
	end})
	return items
end

function editor.openContextLevels()
	editor.openContextMenu(editor.contextLevels(),{x=contextMenu.x,y=contextMenu.y,width=180})
end

function editor.headerPanelUpdate(panel)

	panel.hide = (animationPanel.open or pathsPanel.open or not room)

	if  panel.hide then  return 0  end 

	panel.limit = panel.width
	panel.name 	= ellipses(level.name,panel.limit)

	editor.togglePanel(panel)

	if not panel.open or not sidebar.visible then  return 0  end 

end

function editor.autoSaveGraphic(panel)
	gfx.setColor(1,1,1,0.5)
	--editor.drawContextBack(editor.titlebar)
	--gfx.rectangle("fill",0,0,screenWidth,20)
	if not editor.autoSaveEnabled then
		gfx.setColor(1,1,1,1)
		gfx.line(8+panel.x,7,8+panel.x,12)
		gfx.line(8+panel.x+4,7,8+panel.x+4,12)
	end
	if editor.saveStep<30 then 
		gfx.setColor(1,1,1,panel.open and 1 or 0.5)  
		gfx.draw(getImage("reload"),panel.x+panel.width/2,9, (editor.saveStep/20)*pi*2 , 0.2, 0.2, 64, 64) 
	else 
		gfx.setLineWidth(1)
		local progress = (editor.saveStep/editor.saveTime)*2*pi-pi/2
		local col = panel.open and 1 or 0.4
		gfx.setColor(1,1,1,col)  
		if not panel.line then 
			gfx.arc( "fill", panel.x+panel.width/2,10, 7, -pi/2, (editor.saveStep/editor.saveTime)*2*pi-pi/2, 20 )
			gfx.arc( "fill", panel.x+panel.width/2,10, 7, 0, pi, 20 )
		end
		gfx.arc( "line", (progress>pi+pi/3 or progress<-pi/2.2 or panel.line) and "open" or "pie", panel.x+panel.width/2,10, 7, -pi/2, progress, 32 )
		gfx.setColor(0,0,0,col)  
		gfx.arc( "line", (progress>pi+pi/3 or progress<-pi/2.2 or panel.line) and "open" or "pie", panel.x+panel.width/2,10, 8, -pi/2, progress, 32 )
	end
end

function editor.editPort()
	roomsView = true
end

function editor.editFloor()
	roomsView = false
end

function editor.setCamera(index)
	room.cameras[index] = deepcopy(currentRoom.camera)
	currentRoom.cameraID = index
	currentRoom.camera = deepcopy(room.cameras[index])
	editor.tempTip("Set camera "..index)
end

function editor.resetCamera(index,notip)
	if room.cameras[index] then 
		currentRoom.cameraID = index
		currentRoom.camera = room.cameras[index] and deepcopy(room.cameras[index]) or currentRoom.camera
		if not notip then editor.tempTip("Switched to camera "..index) end
	end
end

function editor.setViewport(index)
	room.viewports[index] = deepcopy(currentRoom.viewport)
	currentRoom.viewportID = index
	currentRoom.viewport = room.viewports[index]
	editor.tempTip("Set viewport "..index)
end

function editor.resetViewport(index)
	if room.viewports[index] then 
		currentRoom.viewportID = index
		currentRoom.viewport = room.viewports[index] and deepcopy(room.viewports[index]) or currentRoom.viewport
		editor.tempTip("Switched to viewport "..index)
	end
end

function editor.editBackground(target)
	return editor.contextColor(target.sky.color)
end
