-- -----------------------------------------------------------------------------
-- 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
mouseOnToolbar = false			-- indicates whether mouse cursor since 
					-- the last do_point() callback is on
					-- toolbar or not
					
toolCount = 7				-- 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/toolbar/demolish.png",
				imgHover 	= "data/toolbar/demolishHover.png",
				imgSelected	= "data/toolbar/demolishOver.png",
				
				normal = nil,
				hover = nil,
				selected = nil
			  },
	
	cable1		= { 
				imgNormal 	= "data/toolbar/cable1mbps.png",
				imgHover 	= "data/toolbar/cable1mbpsHover.png",
				imgSelected	= "data/toolbar/cable1mbpsOver.png",
				
				normal = nil,
				hover = nil,
				selected = nil
			  },
	cable10		= {
				imgNormal 	= "data/toolbar/cable10mbps.png",
				imgHover 	= "data/toolbar/cable10mbpsHover.png",
				imgSelected	= "data/toolbar/cable10mbpsOver.png",
				
				normal = nil,
				hover = nil,
				selected = nil
			  },
	cable100	= {
				imgNormal 	= "data/toolbar/cable100mbps.png",
				imgHover 	= "data/toolbar/cable100mbpsHover.png",
				imgSelected	= "data/toolbar/cable100mbpsOver.png",
								
				normal = nil,
				hover = nil,
				selected = nil
			  },
	router2		= {
				imgNormal 	= "data/toolbar/router2ports.png",
				imgHover 	= "data/toolbar/router2portsHover.png",
				imgSelected	= "data/toolbar/router2portsOver.png",
				
				normal = nil,
				hover = nil,
				selected = nil
			  },
	router4		= {
				imgNormal 	= "data/toolbar/router4ports.png",
				imgHover 	= "data/toolbar/router4portsHover.png",
				imgSelected	= "data/toolbar/router4portsOver.png",

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

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

-- -----------------------------------------------------------------------------
-- 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() 		
	
	mouseInToolbar = true
	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

	return true
end

-- -----------------------------------------------------------------------------
-- Function: handle_toolbar_leave()
-- Inputs: none
-- Returns: boolean indicating whether screen needs refresh
-- Called by do_point() whenever mouse cursor leaves toolbar area
-- -----------------------------------------------------------------------------
function handle_toolbar_leave()
	if mouseInToolbar then
		showUnselectedTools()
		showSelectedTool()
		mouseInToolbar = false
		return true
	else
		return false
	end
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
		gameScene:toolSelection(nil)		
	else
		-- new tool is clicked
		selectedTool = toolMap[tool]
		showSelectedTool()
		gameScene:toolSelection(selectedTool)
	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 isRouter()
	if 	tool == "router2" or
		tool == "router4" or
		tool == "router8" then
		return true
	else
		return false
	end
end

function isCable(tool)
	if	tool == "cable1" or
		tool == "cable10" or
		tool == "cable100" then
		return true
	else
		return false
	end
end



