-----------------
-- Some Helpers
wxluxfrontend = {}
do
	local lx = luxfrontend
	wxluxfrontend.mwheel = 0
	
	wxluxfrontend.keys = {}
	local k = wxluxfrontend.keys
	
	k[wx.WXK_ESCAPE 		] = lx.LUXI_KEY_ESC
	k[wx.WXK_RETURN			] = lx.LUXI_KEY_ENTER
	k[wx.WXK_SHIFT			] = lx.LUXI_KEY_LSHIFT
	k[wx.WXK_ALT			] = lx.LUXI_KEY_LALT
	k[wx.WXK_CONTROL		] = lx.LUXI_KEY_LCTRL
	k[wx.WXK_BACK			] = lx.LUXI_KEY_BACKSPACE
	k[wx.WXK_MENU			] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_PAUSE		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_CAPITAL		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_END			] = lx.LUXI_KEY_END  
	k[wx.WXK_HOME			] = lx.LUXI_KEY_HOME  
	k[wx.WXK_LEFT			] = lx.LUXI_KEY_LEFT  
	k[wx.WXK_UP			] = lx.LUXI_KEY_UP  
	k[wx.WXK_RIGHT		] = lx.LUXI_KEY_RIGHT  
	k[wx.WXK_DOWN			] = lx.LUXI_KEY_DOWN  
	k[wx.WXK_SELECT		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_PRINT		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_EXECUTE		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_SNAPSHOT		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_INSERT		] = lx.LUXI_KEY_INSERT  
	k[wx.WXK_DELETE		] = lx.LUXI_KEY_DELETE 
	k[wx.WXK_HELP			] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_TAB			] = lx.LUXI_KEY_TAB 
	k[wx.WXK_NUMPAD0		] = lx.LUXI_KEY_KP_0  
	k[wx.WXK_NUMPAD1		] = lx.LUXI_KEY_KP_1  
	k[wx.WXK_NUMPAD2		] = lx.LUXI_KEY_KP_2  
	k[wx.WXK_NUMPAD3		] = lx.LUXI_KEY_KP_3  
	k[wx.WXK_NUMPAD4		] = lx.LUXI_KEY_KP_4  
	k[wx.WXK_NUMPAD5		] = lx.LUXI_KEY_KP_5  
	k[wx.WXK_NUMPAD6		] = lx.LUXI_KEY_KP_6  
	k[wx.WXK_NUMPAD7		] = lx.LUXI_KEY_KP_7  
	k[wx.WXK_NUMPAD8		] = lx.LUXI_KEY_KP_8  
	k[wx.WXK_NUMPAD9		] = lx.LUXI_KEY_KP_9  
	k[wx.WXK_MULTIPLY		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_ADD			] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_SEPARATOR	] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_SUBTRACT		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_DECIMAL		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_DIVIDE		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_F1			] = lx.LUXI_KEY_F1  
	k[wx.WXK_F2			] = lx.LUXI_KEY_F2  
	k[wx.WXK_F3			] = lx.LUXI_KEY_F3  
	k[wx.WXK_F4			] = lx.LUXI_KEY_F4  
	k[wx.WXK_F5			] = lx.LUXI_KEY_F5  
	k[wx.WXK_F6			] = lx.LUXI_KEY_F6  
	k[wx.WXK_F7			] = lx.LUXI_KEY_F7  
	k[wx.WXK_F8			] = lx.LUXI_KEY_F8  
	k[wx.WXK_F9			] = lx.LUXI_KEY_F9  
	k[wx.WXK_F10			] = lx.LUXI_KEY_F10  
	k[wx.WXK_F11			] = lx.LUXI_KEY_F11  
	k[wx.WXK_F12			] = lx.LUXI_KEY_F12 
	k[wx.WXK_F13			] = lx.LUXI_KEY_F13  
	k[wx.WXK_F14			] = lx.LUXI_KEY_F14  
	k[wx.WXK_F15			] = lx.LUXI_KEY_F15  
	k[wx.WXK_F16			] = lx.LUXI_KEY_F16  
	k[wx.WXK_F17			] = lx.LUXI_KEY_F17  
	k[wx.WXK_F18			] = lx.LUXI_KEY_F18  
	k[wx.WXK_F19			] = lx.LUXI_KEY_F19  
	k[wx.WXK_F20			] = lx.LUXI_KEY_F20  
	k[wx.WXK_F21			] = lx.LUXI_KEY_F21  
	k[wx.WXK_F22			] = lx.LUXI_KEY_F22  
	k[wx.WXK_F23			] = lx.LUXI_KEY_F23  
	k[wx.WXK_F24			] = lx.LUXI_KEY_F24  
	k[wx.WXK_NUMLOCK		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_SCROLL		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_PAGEUP		] = lx.LUXI_KEY_PAGEUP 
	k[wx.WXK_PAGEDOWN		] = lx.LUXI_KEY_PAGEDOWN  
	
	k[wx.WXK_NUMPAD_SPACE		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_NUMPAD_TAB		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_NUMPAD_ENTER		] = lx.LUXI_KEY_KP_ENTER  
	k[wx.WXK_NUMPAD_F1		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_NUMPAD_F2		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_NUMPAD_F3		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_NUMPAD_F4		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_NUMPAD_HOME		] = lx.LUXI_KEY_HOME  
	k[wx.WXK_NUMPAD_LEFT		] = lx.LUXI_KEY_LEFT  
	k[wx.WXK_NUMPAD_UP		] = lx.LUXI_KEY_UP  
	k[wx.WXK_NUMPAD_RIGHT		] = lx.LUXI_KEY_RIGHT  
	k[wx.WXK_NUMPAD_DOWN		] = lx.LUXI_KEY_DOWN  
	k[wx.WXK_NUMPAD_PAGEUP	] = lx.LUXI_KEY_PAGEUP  
	k[wx.WXK_NUMPAD_PAGEDOWN	] = lx.LUXI_KEY_PAGEDOWN  
	k[wx.WXK_NUMPAD_END		] = lx.LUXI_KEY_END  
	k[wx.WXK_NUMPAD_BEGIN		] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_NUMPAD_INSERT	] = lx.LUXI_KEY_INSERT  
	k[wx.WXK_NUMPAD_DELETE	] = lx.LUXI_KEY_DELETE  
	k[wx.WXK_NUMPAD_EQUAL		] = lx.LUXI_KEY_KP_EQUAL  
	k[wx.WXK_NUMPAD_MULTIPLY	] = lx.LUXI_KEY_KP_MULTIPLY  
	k[wx.WXK_NUMPAD_ADD		] = lx.LUXI_KEY_KP_ADD  
	k[wx.WXK_NUMPAD_SEPARATOR	] = lx.LUXI_KEY_UNKNOWN  
	k[wx.WXK_NUMPAD_SUBTRACT	] = lx.LUXI_KEY_KP_SUBTRACT  
	k[wx.WXK_NUMPAD_DECIMAL	] = lx.LUXI_KEY_KP_DECIMAL  
	k[wx.WXK_NUMPAD_DIVIDE	] = lx.LUXI_KEY_KP_DIVIDE  
	
	-- reversed
	wxluxfrontend.rkeys = {}
	for i,key in pairs(k) do
		if (key ~= lx.LUXI_KEY_UNKNOWN) then
			wxluxfrontend.rkeys[key] = i
		end
	end
	-- a few specials
	wxluxfrontend.rkeys[lx.LUXI_KEY_RALT] = wx.WXK_ALT
	wxluxfrontend.rkeys[lx.LUXI_KEY_RCTRL] = wx.WXK_CTRL
	wxluxfrontend.rkeys[lx.LUXI_KEY_RSHIFT] = wx.WXK_SHIFT
	
end

function wxluxfrontend.connectinput(glcanvas)
	print ("GLC",glcanvas:GetContext())

	glcanvas:Connect(wx.wxEVT_SIZE,
		function(event)
			local size = glcanvas:GetClientSize()
			local w,h = size:GetWidth(),size:GetHeight()
			wxluxfrontend.resizer = true
			luxfrontend.lwindowsize(w,h)
			wxluxfrontend.resizer = false
		end)
	glcanvas:Connect(wx.wxEVT_KEY_DOWN,
		function (event)
			local keycode = event:GetKeyCode()
			local lxkeycode = wxluxfrontend.keys[keycode]
	
			event:Skip()
				luxfrontend.lkeyinput(lxkeycode or keycode,true,false)
		end)
	glcanvas:Connect(wx.wxEVT_KEY_UP,
			function (event)
			local keycode = event:GetKeyCode()
			local lxkeycode = wxluxfrontend.keys[keycode]
	
				event:Skip()
				luxfrontend.lkeyinput(lxkeycode or keycode,false,false)
			end)
	glcanvas:Connect(wx.wxEVT_CHAR,
			function (event)
			local keycode = event:GetKeyCode()
			local lxkeycode = wxluxfrontend.keys[keycode] or keycode
				
			if (lxkeycode < luxfrontend.LUXI_KEY_SPECIAL) then
					luxfrontend.lkeyinput(keycode,true,true)
					luxfrontend.lkeyinput(keycode,false,true)
				end
			end)
	glcanvas:Connect(wx.wxEVT_MOTION,
			function(event)
				local p = event:GetPosition()
				local x,y = p:GetX(),p:GetY()
				luxfrontend.lmousepos(x,y)
			end)
	glcanvas:Connect(wx.wxEVT_LEFT_DOWN,
			function(event)
				glcanvas:SetFocus()
				luxfrontend.lmousebutton(0,true)
			end)
	glcanvas:Connect(wx.wxEVT_LEFT_UP,
			function(event)
				luxfrontend.lmousebutton(0,false)
			end)
	glcanvas:Connect(wx.wxEVT_RIGHT_DOWN,
			function(event)
				luxfrontend.lmousebutton(2,true)
			end)
	glcanvas:Connect(wx.wxEVT_RIGHT_UP,
			function(event)
				luxfrontend.lmousebutton(2,false)
			end)
	glcanvas:Connect(wx.wxEVT_MIDDLE_DOWN,
			function(event)
				luxfrontend.lmousebutton(1,true)
			end)
	glcanvas:Connect(wx.wxEVT_MIDDLE_UP,
			function(event)
				luxfrontend.lmousebutton(1,false)
			end)
	
	glcanvas:Connect(wx.wxEVT_MOUSEWHEEL,
			function(event)
				local rot = math.floor(event:GetWheelRotation()*1.001/event:GetWheelDelta())
				wxluxfrontend.mwheel = wxluxfrontend.mwheel + rot
			end)

	---------------
	-- Input/Output
	
	luxfrontend.setSetCallbacks(
		function ()
			-- can be ignored
		end
	)

	luxfrontend.setGetKey(
		function (key)
			local wxkey = wxluxfrontend.rkeys[key]
			local kstate = wx.wxGetKeyState(wxkey or key)
			
			return kstate
		end
	)
	luxfrontend.setGetMouseButton(
		function (btn)
			local mstate = wx.wxGetMouseState()
			local state = {
				mstate:LeftDown(),
				mstate:MiddleDown(),
				mstate:RightDown(),
			}
			return state[btn+1]
		end
	)
	luxfrontend.setGetMousePos(
		function (btn)
			local p = glcanvas:ScreenToClient(wx.wxGetMousePosition())
			return p:GetXY()
		end
	)
	luxfrontend.setSetMousePos(
		function (x,y)
			--local p = glcanvas:ClientToScreen(wx.wxPoint(x,y))
			--local ms = wx.wxGetMouseState()
			--ms:SetX(p:GetX())
			--ms:SetY(p:GetY())
			glcanvas:WarpPointer(x,y)
		end
	)
	luxfrontend.setGetMouseWheel(
		function ()
			return wxluxfrontend.mwheel
		end
	)
	luxfrontend.setSetMouseWheel(
		function (val)
			wxluxfrontend.mwheel = val
		end
	)
	
	-- token
	-- LUXI_MOUSE_CURSOR, 
	-- LUXI_MOUSE_FIX (mouse trapped inside), 
	-- LUXI_KEY_REPEAT
	
	luxfrontend.setEnable(
		function (token)
			
		end
	)
	
	luxfrontend.setDisable(
		function (token)
			
		end
	)
end


