-- -----------------------------------------------------------------------------
-- SimNetwork
-- toolbar.lua
-- Toolbar handling code
-- -----------------------------------------------------------------------------

if not SimNetwork_GLOBAL	then dofile("global.lua")	end
if not SimNetwork_INPUT	then dofile("input.lua")	end
SimNetwork_TOOLBAR = true

-- Toolbar geometry
toolCount = 9				-- number of tools
toolWidth = 64				-- width of single tool
toolHeight = 32				-- height of single tool sprite

toolbarWidth = toolCount * toolWidth	-- total width of toolbar
toolbarHeight = toolHeight		-- total height of toolbar

selectedTool = nil			-- currently selected tool


toolbar = {				-- holds sprites of toolbar
	demolish	= {
				imgNormal 	= "data/demolish.png",
				imgHover 	= "data/demolishHover.png",
				imgSelected	= "data/demolishOver.png",
				
				normal = nil,
				hover = nil,
				selected = nil
			  },
	
	cable1		= { 
				imgNormal 	= "data/cable1mbps.png",
				imgHover 	= "data/cable1mbpsHover.png",
				imgSelected	= "data/cable1mbpsOver.png",
				
				normal = nil,
				hover = nil,
				selected = nil
			  },
	cable10		= {
				imgNormal 	= "data/cable10mbps.png",
				imgHover 	= "data/cable10mbpsHover.png",
				imgSelected	= "data/cable10mbpsOver.png",
				
				normal = nil,
				hover = nil,
				selected = nil
			  },
	cable100	= {
				imgNormal 	= "data/cable100mbps.png",
				imgHover 	= "data/cable100mbpsHover.png",
				imgSelected	= "data/cable100mbpsOver.png",
								
				normal = nil,
				hover = nil,
				selected = nil
			  },
	cable1000	= {
				imgNormal 	= "data/cable1000mbps.png",
				imgHover 	= "data/cable1000mbpsHover.png",
				imgSelected	= "data/cable1000mbpsOver.png",
				
				normal = nil,
				hover = nil,
				selected = nil
			  },
	router2		= {
				imgNormal 	= "data/router2ports.png",
				imgHover 	= "data/router2portsHover.png",
				imgSelected	= "data/router2portsOver.png",
				
				image = "data/router2ports.png",
				normal = nil,
				hover = nil,
				selected = nil
			  },
	router4		= {
				imgNormal 	= "data/router4ports.png",
				imgHover 	= "data/router4portsHover.png",
				imgSelected	= "data/router4portsOver.png",

				normal = nil,
				hover = nil,
				selected = nil
			  },
	router8		= {
				imgNormal 	= "data/router8ports.png",
				imgHover 	= "data/router8portsHover.png",
				imgSelected	= "data/router8portsOver.png",
				
				normal = nil,
				hover = nil,
				selected = nil
			  },
	router16	= {
				imgNormal 	= "data/router16ports.png",
				imgHover 	= "data/router16portsHover.png",
				imgSelected	= "data/router16portsOver.png",
				
				normal = nil,
				hover = nil,
				selected = nil
			  }
}

toolMap = {			-- maps tool number to name (e.g. 1: demolish,
				-- 2: cable1, ...
	"demolish",
	"cable1",
	"cable10",
	"cable100",
	"cable1000",
	"router2",
	"router4",
	"router8",
	"router16"
}

-- -----------------------------------------------------------------------------
-- Function: showUnselectedTools()
-- Inputs/Returns: none
-- Resets the state of all toolbar sprites to normal (unselected, not hovered)
-- -----------------------------------------------------------------------------
function showUnselectedTools()
	
	for key in toolbar do
		show(toolbar[key].normal)
		hide(toolbar[key].hover)
		hide(toolbar[key].selected)
	end
end

-- -----------------------------------------------------------------------------
-- Function: showSelectedTool()
-- Inputs/Returns: none
-- Displays currently selected tool in "selected" state
-- -----------------------------------------------------------------------------
function showSelectedTool()
	if selectedTool ~= nil then
		hide(toolbar[selectedTool].normal)
		hide(toolbar[selectedTool].hover)
		show(toolbar[selectedTool].selected)
	end
end

-- -----------------------------------------------------------------------------
-- Function: handle_toolbar_hover()
-- Inputs: none
-- Returns: boolean indicating whether screen needs refresh
-- Determines what tool is currently under mouse pointer and changes its sprite
-- to hover. Function should be called with every mouse move
-- -----------------------------------------------------------------------------
function handle_toolbar_hover() 		
	
	-- see if mouse pointer is within toolbar
	if (input.MouseX > 0) and (input.MouseX < toolbarWidth) and (input.MouseY > 0) and (input.MouseY < toolbarHeight) then
		showUnselectedTools()
				
		-- determine mouse offset releative to tool
		local tool = math.floor(input.MouseX / 64 + 1)
		hide(toolbar[toolMap[tool]].normal)
		show(toolbar[toolMap[tool]].hover)
		if selectedTool ~= nil then
			showSelectedTool()
		end
	else
		-- mouse pointer out of toolbar, reset all tools to normal
		showUnselectedTools()
		--show selected tool in its over state
		showSelectedTool()
	end
	
	return true
end

-- -----------------------------------------------------------------------------
-- Function: handle_toolbar_click()
-- Inputs: none
-- Returns: boolean indicating whether screen needs refresh
-- Called when ever left mouse button clicked if mouse pointer is within toolbar
-- -----------------------------------------------------------------------------
function handle_toolbar_click()
	
	-- reset toolbar
	showUnselectedTools()
	
	-- determine mouse offset relative to tool
	local tool = math.floor(input.MouseX / 64 + 1)
	
	if selectedTool == toolMap[tool] then
		-- clicked tool is same as selected one, deselect it
		selectedTool = nil		
	else
		-- new tool is clicked
		selectedTool = toolMap[tool]
		showSelectedTool()
		gameScene.state = SCENE_STATE.cable_point_a
		if isSelectedCable() then
			print("Game state to cable_point_a")
			gameScene.state = SCENE_STATE.cable_point_a
		end
	end
	return true
end

-- -----------------------------------------------------------------------------
-- Function: create_toolbar()
-- Inputs: none
-- Returns: pivot representing the toolbar
-- Loads toolbar sprites and positions them. Called at initialization
-- -----------------------------------------------------------------------------		
function create_toolbar()

	local theToolbar = E.create_pivot()
	local width = 0
	
	local xPos = viewport.L
	local yPos = viewport.T
	
	-- load toolbar sprites: loop through tools in toolbar, load their
	-- normal, hover, and selected sprites, then position them correctly
	for key in toolMap do
		local toolName = toolMap[key]
		
		toolbar[toolName].normal = create_overlay(toolbar[toolName].imgNormal)
		toolbar[toolName].hover = create_overlay(toolbar[toolName].imgHover)
		toolbar[toolName].selected = create_overlay(toolbar[toolName].imgSelected)
		
		E.parent_entity(toolbar[toolName].normal, theToolbar)
		E.parent_entity(toolbar[toolName].hover, theToolbar)
		E.parent_entity(toolbar[toolName].selected, theToolbar)

		-- position tool
		local x0, y0, z0, x1, y1, z1 = E.get_entity_bound(toolbar[toolName].normal)
		local toolW = x1 - x0
		local toolH = y1 - y0
	
		E.set_entity_position(toolbar[toolName].normal, xPos+toolW/2+width, yPos-toolH/2,0)
		E.set_entity_position(toolbar[toolName].hover, xPos+toolW/2+width, yPos-toolH/2,0)
		E.set_entity_position(toolbar[toolName].selected, xPos+toolW/2+width, yPos-toolH/2,0)
		
		width = width + toolWidth
	end
	
	-- reset toolbar: all tools are in normal state (unselected, not hovered)
	showUnselectedTools()
	
	return theToolbar
end

--------------------------------------------------------------------------------
-- Function: toolbar.isSelectedRouter()
-- Inputs: none
-- Returns: boolean indicating whether selected tool is a router or not
function isSelectedRouter()
	if 	selectedTool == toolbar.router2 or
		selectedTool == toolbar.router4 or
		selectedTool == toolbar.router8 or
		selectedTool == toolbar.router16 then
		return true
	else
		return false
	end
end

function isSelectedCable()
	if	selectedTool == toolbar.cable1 or
		selectedTool == toolbar.cable10 or
		selectedTool == toolbar.cable100 or
		selectedTool == toolbar.cable1000 then
		return true
	else
		return false
	end
end



