-- core/input.lua :: The input system. This object take care of transforming the
-- differents inputs source in a virtual controller for the game.

--[[
  Copyright © 2019 Kazhnuz

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

local InputManager = Object:extend()

-- INIT FUNCTIONS
-- Initialize and configure the controller system

function InputManager:new(controller)
  self.controller = controller
  self.data       = self.controller.options:getInputData()

  self:initKeys()
end

function InputManager:initKeys()
  self.fakekeys = self:getKeyList(1)

  self.sources      = self:getSources()
  self.fakesources  = self:getSources()
end

-- INFO FUNCTIONS
-- Get functions from the controller object

function InputManager:isDown(sourceid, padkey)
  local isdown = false

  if self.data[sourceid].type == "keyboard" then
    local key = self.data[sourceid].keys[padkey]
    isdown = love.keyboard.isDown(key)
  else
    print("Warning: unsupported input device")
  end

  return isdown
end

function InputManager:getSources()
  local sources = {}
  for i,v in ipairs(self.data) do
    sources[i] = {}
    sources[i].keys = self:getKeyList(i)
  end

  return sources
end

function InputManager:getKeyList(sourceid)
  local keys = {}
  if self.data[sourceid] ~= nil then
    for k,v in pairs(self.data[sourceid].keys) do
      keys[k]  = {}
      keys[k].isDown     = false
      keys[k].isPressed  = false
      keys[k].isReleased = false
      keys[k].test = "ok"
    end
  end

  return keys
end

function InputManager:getKey(sourceid, padkey)
  local padkey = padkey
  for k,v in pairs(self.data[sourceid].keys) do
    if (k == padkey) then key = v end
  end
  return key
end

-- KEY MANAGEMENT FUNCTIONS
-- Manage pressed keys

function InputManager:flushKeys()
  for i,v in ipairs(self.sources) do
    self:flushSourceKeys(i)
  end
end

function InputManager:flushSourceKeys(sourceid)
  self.keys = {}
  for k,v in pairs(self.sources[sourceid].keys) do
    v  = {}
    v.isDown     = false
    v.isPressed  = false
    v.isReleased = false
  end
end


function InputManager:checkKeys(sourceid)
  for k,v in pairs(self.sources[sourceid].keys) do
    local isDown = self:isDown(sourceid, k)
    if (isDown) then
      if not (self.sources[sourceid].keys[k].isDown) then
        self.sources[sourceid].keys[k].isDown     = true
        self.sources[sourceid].keys[k].isPressed  = true
        self.sources[sourceid].keys[k].isReleased = false
      else
        if (self.sources[sourceid].keys[k].isPressed) then
          self.sources[sourceid].keys[k].isPressed = false
        end
      end
    else
      if (self.sources[sourceid].keys[k].isDown) then
        self.sources[sourceid].keys[k].isDown     = false
        self.sources[sourceid].keys[k].isPressed  = false
        self.sources[sourceid].keys[k].isReleased = true
      else
        if (self.sources[sourceid].keys[k].isReleased) then
          self.sources[sourceid].keys[k].isReleased = false
        end
      end
    end
  end
end

-- UPDATE FUNCTIONS
-- Check every step pressed keys

function InputManager:update(dt)
  for i,v in ipairs(self.sources) do
    self:checkKeys(i)
  end
end

return InputManager
