-- -----------------------------------------------------------------------------
-- SimNetwork
-- widgets_ortho.lua
-- Experiment game scene using orthogonal camera for 3D scene
-- -----------------------------------------------------------------------------
if not SimNetwork_GLOBAL	then dofile("global.lua") end
if not SimNetwork_INPUT	then dofile("input.lua") end

--turn debuggin on/off
debug = false

--------------------------------------------------------------------------------
-- Global variables, game state, and scene graph entities:
curr_state = nil
time_state = 0

digTool = nil
--
--------------------------------------------------------------------------------      

--------------------------------------------------------------------------------
-- Function: dprint(string,entity)
-- Inputs: string, entity
-- Returns: none
-- write debugging info to the console
function dprint(string,entity)
	if debug then
		E.print_console(string,entity,"\n")
	end
end                       

----------------------------------------------------------------------------------------------------
-- Function: create_scene_2d()
-- Inputs: none
-- Returns: none
--
-- Sets up 2D scene to be used for game's toolbar interface (and maybe 
-- other things in the future)
-- 
function create_scene_2d()
    
	-- the following is used to position sprites on the scene
	local width = 0

	-- create 2D camera, place it in the center of screen
	camera2d = E.create_camera(E.camera_type_orthogonal)
	E.set_entity_position(camera2d, -viewport.k * viewport.w/2, 
				-viewport.k * viewport.h/2, 0)
	E.set_entity_scale(scene2d, viewport.k, viewport.k, viewport.k)
	
	-- create 2D scene
	scene2d = E.create_pivot()
	E.parent_entity(scene2d, camera2d)

	-- create Cable 1 Mbps Tool
	spriteDemolish		= create_overlay("data/demolish.png", 1, 0,0,0)
	spriteDig		= create_overlay("data/dig.png", 1, 0,0,0)
	spriteCable1Mbps	= create_overlay("data/cable1mbps.png", 1, 0,0,0)
	spriteCable10Mbps	= create_overlay("data/cable10mbps.png", 1, 0,0,0)
	spriteCable100Mbps	= create_overlay("data/cable100mbps.png", 1, 0,0,0)
	spriteCable1000Mbps	= create_overlay("data/cable1000mbps.png", 1, 0,0,0)
	sprite2portRouter	= create_overlay("data/router2ports.png", 1, 0,0,0)
	sprite4portRouter	= create_overlay("data/router4ports.png", 1, 0,0,0)
	sprite8portRouter	= create_overlay("data/router8ports.png", 1, 0,0,0)
	sprite16portRouter	= create_overlay("data/router16ports.png", 1, 0,0,0)
	
	E.parent_entity(spriteDemolish,scene2d)
	E.parent_entity(spriteDig,scene2d)
	
	--get size of the spripes
	local x0, y0, z0, x1, y1, z1 = E.get_entity_bound(spriteDemolish)
	spriteDemolishW = x1 - x0
	spriteDemolishH = y1 - y0
	x0, y0, z0, x1, y1, z1 = E.get_entity_bound(spriteDig)
	spriteDigW = x1 - x0
	spriteDigH = y1 - y0
	x0, y0, z0, x1, y1, z1 = E.get_entity_bound(spriteCable1Mbps)
	spriteCable1MbpsW = x1 - x0
	spriteCable1MbpsH = y1 - y0
	x0, y0, z0, x1, y1, z1 = E.get_entity_bound(spriteCable10Mbps)
	spriteCable10MbpsW = x1 - x0
	spriteCable10MbpsH = y1 - y0
	x0, y0, z0, x1, y1, z1 = E.get_entity_bound(spriteCable100Mbps)
	spriteCable100MbpsW = x1 - x0
	spriteCable100MbpsH = y1 - y0
	x0, y0, z0, x1, y1, z1 = E.get_entity_bound(spriteCable1000Mbps)
	spriteCable1000MbpsW = x1 - x0
	spriteCable1000MbpsH = y1 - y0
	x0, y0, z0, x1, y1, z1 = E.get_entity_bound(sprite2portRouter)
	sprite2portRouterW = x1 - x0
	sprite2portRouterH = y1 - y0
	x0, y0, z0, x1, y1, z1 = E.get_entity_bound(sprite4portRouter)
	sprite4portRouterW = x1 - x0
	sprite4portRouterH = y1 - y0
	x0, y0, z0, x1, y1, z1 = E.get_entity_bound(sprite8portRouter)
	sprite8portRouterW = x1 - x0
	sprite8portRouterH = y1 - y0
	x0, y0, z0, x1, y1, z1 = E.get_entity_bound(sprite16portRouter)
	sprite16portRouterW = x1 - x0
	sprite16portRouterH = y1 - y0

	xPos = viewport.L
	yPos = viewport.T
	--move the sprites to the right positions
	E.set_entity_position(spriteDemolish,
		xPos+spriteDemolishW/2+width,
		yPos-spriteDemolishH/2,0)
	width = width + spriteDemolishW
	E.set_entity_position(spriteDig,xPos+spriteDigW/2+width,yPos-spriteDigH/2,0)
	width = width + spriteDigW
	E.set_entity_position(spriteCable1Mbps,xPos+spriteCable1MbpsW/2+width,yPos-spriteCable1MbpsH/2,0)
	width = width + spriteCable1MbpsW
	E.set_entity_position(spriteCable10Mbps,xPos+spriteCable10MbpsW/2+width,yPos-spriteCable10MbpsH/2,0)
	width = width + spriteCable10MbpsW
	E.set_entity_position(spriteCable100Mbps,xPos+spriteCable100MbpsW/2+width,yPos-spriteCable100MbpsH/2,0)
	width = width + spriteCable100MbpsW
	E.set_entity_position(spriteCable1000Mbps,xPos+spriteCable1000MbpsW/2+width,yPos-spriteCable1000MbpsH/2,0)
	width = width + spriteCable1000MbpsW
	E.set_entity_position(sprite2portRouter,xPos+sprite2portRouterW/2+width,yPos-sprite2portRouterH/2,0)
	width = width + sprite2portRouterW
	E.set_entity_position(sprite4portRouter,xPos+sprite4portRouterW/2+width,yPos-sprite4portRouterH/2,0)
	width = width + sprite4portRouterW
	E.set_entity_position(sprite8portRouter,xPos+sprite8portRouterW/2+width,yPos-sprite8portRouterH/2,0)
	width = width + sprite8portRouterW
	E.set_entity_position(sprite16portRouter,xPos+sprite16portRouterW/2+width,yPos-sprite16portRouterH/2,0)
	width = width + sprite16portRouterW
	
end

-- ----------------------------------------------------------------------------
-- Function create_scene_3d()
-- Inputs: none
-- Returns: none
-- Description: loads 3D models, positions objects
-- and creats a perspective camera
-- ----------------------------------------------------------------------------
function create_scene_3d()

	-- create entities
	scene3d = E.create_pivot()
	camera3d = E.create_camera(E.camera_type_orthogonal)
	
	--E.set_camera_range(camera3d, 1, 100)
	light3d  = E.create_light(E.light_type_positional)
	
	--set camera in the center of the screen
	E.set_entity_position(camera3d, -viewport.k * viewport.w/2, 
	-viewport.k * viewport.h/2, 0)
	
	-- print camera's coordinates
	local x, y, z = E.get_entity_position(camera3d)
	print("CameraX: ", x, ", CameraY: ", y)
	
	E.set_entity_scale(scene3d, 5 * viewport.k, 5 * viewport.k, 5 * viewport.k)               
	zoom = 5 * viewport.k
	
	local building1 = E.create_object("data/building1.obj")
	local building2 = E.create_object("data/building1.obj")
	local dig = E.create_object("data/dig.obj")
	digTool = E.create_object("data/dig.obj")
	local floor = E.create_object("data/tile.obj") 
	local lightL = E.create_light(E.light_type_positional)
	local lightR = E.create_light(E.light_type_positional)
	
	E.set_entity_scale(floor,100,1,100)

	-- bind entities
	E.parent_entity(lightL, camera3d)
	E.parent_entity(lightR, lightL)

	buildings = E.create_pivot()
	E.parent_entity(building1, buildings)
	E.parent_entity(building2, buildings)
	E.parent_entity(dig, buildings)
	E.parent_entity(digTool,buildings)
	
	-- group all 3d entities under scene3d
	E.parent_entity(buildings, scene3d)
	E.parent_entity(floor, scene3d)
	E.parent_entity(scene3d, lightR)
	
	-- position elements               
	E.set_entity_position(lightL, -viewport.w / 2, 0, viewport.h / 2)
	E.set_entity_position(lightR,  viewport.w / 2, 0, viewport.h / 2)
	E.set_entity_position(lightUpper, 0, viewport.h / 2, 0)
	
	E.set_entity_position(building1, 20, 0, 0)
	E.set_entity_position(building2, -30, 0, 16)
	E.set_entity_position(dig,18,0,0)
	E.set_entity_position(digTool,0,0,0)
	--E.set_entity_flags(digTool,E.entity_flag_track_pos, true)
	--E.set_entity_tracking(digTool,4,E.tracking_mode_world)
	E.turn_entity(scene3d, 20, 0, 0)
	RotationX = 20
	
	dig2 = E.create_clone(dig)
	E.parent_entity(dig2,buildings)
	E.set_entity_position(dig2,17,0,0) 
	                  
	dig3 = E.create_clone(dig)
	E.parent_entity(dig3,buildings)
	E.set_entity_position(dig3,16,0,0) 

end

-- -----------------------------------------------------------------------------
-- Callbacks
-- Keyboard
function do_keyboard(k, s)
	
	if s then
		-- key pressed
		-- see what key has been pressed
		if k == E.key_up then
			--E.move_entity(camera3d, 0, 0, -0.75)
			E.move_entity(digTool,0,0,-1)
			return true
		elseif k == E.key_down then
			--E.move_entity(camera3d, 0, 0, 0.75)
			E.move_entity(digTool,0,0,1)
			return true
		elseif k == E.key_left then
			--E.move_entity(camera3d, -0.75, 0, 0)
			E.move_entity(digTool,-1,0,0)
			return true
		elseif k == E.key_right then
			--E.move_entity(camera3d, 0.75, 0, 0)
			E.move_entity(digTool,1,0,0)
			return true
		else
			-- print("Unrecognized key: ", k)
		end
	end

end

--[[
function do_timer(dt)
	--E.print_console("\n","MouseY:",MouseY)
	posX = MouseX;
	poxZ = MouseY;
	
	--this will enable collision detection
	posY = 0 
	
	local x0, y0, z0, x1, y1, z1 = E.get_entity_bound(floor)
	
	--if MouseX > x0 and MouseX < x1 and MouseY > z0 and MouseY < z1 then
	--	show(digTool)
		x, y, z = E.get_camera_vector(camera3d, MouseX, MouseY)

		dprint("x:",100*x)
		dprint("y:",y)
		dprint("z:",100*z+100)
		--E.set_entity_position(digTool,x,1,z)
	--else
	--	hide(digTool)
	--end
		
	return false
end
--]]
----------------------------------------------------------------------------------------------------

init_viewport()
create_scene_2d()
create_scene_3d()
E.enable_timer(false)
