local main_wrapper = require "love_src.wrapper.lappy.world"
---@class wrappers.Concord.world : lappy.world
local world = main_wrapper:extend()

local reap = require("lib.reap")
local BASE = reap.base_path(...)

function world:new()
  world.super.new(self, BASE, "player")
end

local function handle_input(accel, brake, steer)
  local up = love.keyboard.isDown("up")
  local down = love.keyboard.isDown("down")
  local left = love.keyboard.isDown("left")
  local right = love.keyboard.isDown("right")

  local desire_forward = 0
  local desire_steer = 0
  if (up) then
    desire_forward = accel
  elseif (down) then
    desire_forward = - brake
  end
  if (left) then
    desire_steer = steer
  elseif (right) then
    desire_steer = - steer
  end
  return desire_forward, desire_steer
end

local default = {
  forward = 0.0,
  steer = 0.0
}

function world:load(entity, data)
  self.e = entity
  self.data = data or default
end

function world:update(dt)
  local accel, brake, steer = self.e.components.racer.data.accel, self.e.components.racer.data.brake, self.e.components.racer.data.steer
  local desire_forward, desire_steer = handle_input(accel, brake, steer)

  self.data.forward = desire_forward
  self.data.steer = desire_steer
end

return world
