-- -----------------------------------------------------------------------------
-- 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


message = "Message:"
messageText = "Welcome to SimNetwork! Start by placing a new router on a map."

-- 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
			  },
	router4		= {
				imgNormal 	= "data/toolbar/router4ports.png",
				imgHover 	= "data/toolbar/router4portsHover.png",
				imgSelected	= "data/toolbar/router4portsOver.png",
				
				normal = nil,
				hover = nil,
				selected = nil
			  },
	router6		= {
				imgNormal 	= "data/toolbar/router6ports.png",
				imgHover 	= "data/toolbar/router6portsHover.png",
				imgSelected	= "data/toolbar/router6portsOver.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",
	"router4",
	"router6",
	"router8"
}

--------------------------------------------------------------------------------
-- Function:		unselectTool()
-- Inputs/Outputs:	none
-- Called by scene to deactivate the currently active tool
function unselectTool()
	--print("in unselectTool()")
	if selectedTool ~= nil then
		selectedTool = nil
		showUnselectedTools()
		gameScene:toolSelection(nil)
	end
end

-- -----------------------------------------------------------------------------
-- Function: showUnselectedTools()
-- Inputs: none
-- Returns: none
-- Resets the state of all toolbar sprites to normal (unselected, not hovered)
-- -----------------------------------------------------------------------------
function showUnselectedTools()
	--print("in 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()
	--print("in 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() 		
	--print("in 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()
	--print("in 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()
	--print("in 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)
		E.play_sound(toolDeselectSound)		
	else
		-- new tool is clicked
		selectedTool = toolMap[tool]
		showSelectedTool()
		gameScene:toolSelection(selectedTool)
		E.play_sound(toolHoverSound)
	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()
	--print("in create_toolbar()")
	local theToolbar = E.create_pivot()
	local backgroundpivot = 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_alpha(toolbar[toolName].normal, 0.7)
		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()
	
	-- create brush to be used for strings
	brush = E.create_brush()
	E.set_brush_color(brush,0,0,0,1)
	E.set_typeface("data/Coprgtb.TTF")
	
	--create string for balance
	messageString = E.create_string(message)
	E.set_string_fill(messageString, brush)
	
	E.set_entity_position(messageString, viewport.L, viewport.T - 48,0)
	E.set_entity_scale(messageString, 13,13,0)

	-- create string for overall customer satisfaction
	messageTextString = E.create_string(messageText)
	E.set_string_fill(messageTextString, brush)
	
	E.set_entity_position(messageTextString, viewport.L + 70, viewport.T - 48,0)
	E.set_entity_scale(messageTextString, 13,13,0)

	-- push the strings
	E.parent_entity(messageString, theToolbar)
	E.parent_entity(messageTextString, theToolbar)
	
	--show background image for the message
	messageBackground = make_sprite("data/toolbar/messagebar.png")
	E.set_entity_position(messageBackground,viewport.L,viewport.T-45,0)
	E.parent_entity(messageBackground,theToolbar)
	E.set_entity_alpha(messageBackground, 0.9)
	
	--show background image
	background = make_sprite("data/toolbar.png")
	E.set_entity_position(background,viewport.L,viewport.T-32,0)
	E.parent_entity(background,theToolbar)
	E.set_entity_alpha(background, 0.7)
	
	-- create widgets
	
	
	return theToolbar
end

-- -----------------------------------------------------------------------------
-- Function: isRouter()
-- Inputs: tool
-- Returns: boolean indicating whether selected tool is a router or not
-- -----------------------------------------------------------------------------
function isRouter(tool)
	--print("in isRouter()")
	if 	tool == "router4" or
		tool == "router6" or
		tool == "router8" then
		return true
	else
		return false
	end
end

-- -----------------------------------------------------------------------------
-- Function: isCable()
-- Inputs: tool
-- Returns: boolean indicating whether selected tool is a cable or not
-- -----------------------------------------------------------------------------
function isCable(tool)
	--print("in isCable()")
	if	tool == "cable1" or
		tool == "cable10" or
		tool == "cable100" then
		return true
	else
		return false
	end
end

-- -----------------------------------------------------------------------------
-- Function: isDemolish()
-- Inputs: tool
-- Returns: boolean indicating whether selected tool is demolish or not
-- -----------------------------------------------------------------------------
function isDemolish(tool)
	--print("in isDemolish()")
	if tool == "demolish" then
		return true
	else
		return false
	end
end

-- -----------------------------------------------------------------------------
-- Function: setMessage()
-- Inputs: text, format
-- Returns: none
-- -----------------------------------------------------------------------------
function setMessage(text, color)
	if color == "black" then
		brushS = blackBrush
	elseif color == "green" then
		brushS = greenBrush
	elseif color == "red" then 
		brushS = redBrush
	elseif color == "blue" then
		brushS = blueBrush
	end
	--print("in setMessage()")
	E.set_string_fill(messageTextString, brushS)
	E.set_string_text(messageTextString, text)
	E.play_sound(messageSound)
end
