require "luamodules.strict"
require "luascape.core"
require "luascape.reflect"

reflect.loadReflectors("input")

inputManager = scape.input.InputManager.instance()

reflect.makeNamespaceGlobal("scape::input")

input = {}
  function input.getJoysticks()
    local joysticks = {}
    for i, device in pairs(inputManager.devices) do
      local identifier = Identifier(device:identifier())
      if string.sub(identifier:decode(), 1, 8) == "joystick" then
        table.insert(joysticks, device)
      end
    end
    return joysticks  
  end
  
  function input.getKeyboard()
    local inputManager = scape.input.InputManager.instance()  
    for i, device in pairs(inputManager.devices) do
      local identifier = Identifier(device:identifier())
      if string.sub(identifier:decode(), 1, 8) == "keyboard" then
        return device
      end
    end
    return nil
  end

  function input.getMice()
    local mice = {}
    local inputManager = scape.input.InputManager.instance()  
    for i, device in pairs(inputManager.devices) do
      local identifier = Identifier(device:identifier())
      if string.sub(identifier:decode(), 1, 5) == "mouse" then
        table.insert(mice, device)
      end
    end
    return mice
  end
  
  function input.createScriptInputBinding(sourceFile, functionName, device, event)
    local binding = KeyInputBinding()
    local inputManager = scape.input.InputManager.instance()  
    binding.device = inputManager:getDevice(Identifier(device).code)
    binding.eventName = Identifier(event)
    binding.repeatEvent = false
  
    local action = ScriptAction()
    action.sourceFilename = sourceFile
    action.functionName = functionName
    binding.onAction = action
    
    inputManager.bindings:add(binding)
  
    return binding
  end

  function input.createMessageMiceInputBinding()
    local mice = input.getMice()
    input.messaging.mice={}
    for i, v in ipairs(mice) do
      local mousebindung = MessageMouseInputBinding()
      mousebindung.device = v
      inputManager.bindings:add(mousebindung)
      table.insert(input.messaging.mice,mousebindung)
    end
  end
  
  function input.createMessageKeyboardInputBinding()
  	local binding = MessageKeyboardInputBinding()
	  binding.device = input.getKeyboard()
  	inputManager.bindings:add(binding)
  	input.messaging.keyboard = binding
  end

  function input.createMessageJoystickInputBinding()
  	local sticks = input.getJoysticks()
  	input.messaging.joysticks={}
	  for i, v in ipairs(sticks) do
      local jstickbinding = MessageJoystickInputBinding(tostring(i))
      jstickbinding.device = v
      inputManager.bindings:add(jstickbinding)
      table.insert(input.messaging.joysticks,jstickbinding)
    end
  end

  function input.createMessageInputBindings()
    if not input.messaging then
      input.messaging={}
      input.createMessageMiceInputBinding()
  	  input.createMessageKeyboardInputBinding()
  	  input.createMessageJoystickInputBinding()
  	end
  end
  
  function input.setRelativeMouse(bool)
  	local mice = input.getMice()
  	for k,v in ipairs(mice) do
  	  v.relativeMouse = bool
  	end
  end
