local Vector3 = require("types.vector3")
local Signal = require("types.signal")
local AABB = require("types.aabb")

local Player = {
  position = Vector3(0, 1, 0),
  velocity = Vector3(),

  speed = 0.07,
  mouse_sensitivity = 0.01,

  yaw = 0,
  yaw_speed = 0.05,
  
  accel = 0.3,
  
  aabb = nil,
  test_intersect = nil,

  ThrowPressed = Signal.new(),
}

local aabb_offset = Vector3(-0.25, -1, -0.25)

---@param aabb AABB
---@param other AABB
---@param velocity Vector3
---@return Vector3, Vector3
local function resolve_collision(aabb, other, velocity)
  local my_min = aabb.min
  local my_max = aabb:get_max()
  local other_min = other.min
  local other_max = other:get_max()
  
  local overlap_x = math.min(my_max.x - other_min.x, other_max.x - my_min.x)
  local overlap_y = math.min(my_max.y - other_min.y, other_max.y - my_min.y)
  local overlap_z = math.min(my_max.z - other_min.z, other_max.z - my_min.z)
  
  local min_overlap = math.min(overlap_x, overlap_y, overlap_z)
  
  local new_pos = my_min:copy()
  local new_vel = velocity:copy()
  
  if min_overlap == overlap_x then
    if velocity.x > 0 then
      new_pos.x = other_min.x - (my_max.x - my_min.x)
    elseif velocity.x < 0 then
      new_pos.x = other_max.x
    else
      if my_min.x < other_min.x then
        new_pos.x = other_min.x - (my_max.x - my_min.x)
      else
        new_pos.x = other_max.x
      end
    end
    new_vel.x = 0
  elseif min_overlap == overlap_y then
    if velocity.y > 0 then
      new_pos.y = other_min.y - (my_max.y - my_min.y)
    elseif velocity.y < 0 then
      new_pos.y = other_max.y
    else
      if my_min.y < other_min.y then
        new_pos.y = other_min.y - (my_max.y - my_min.y)
      else
        new_pos.y = other_max.y
      end
    end
    new_vel.y = 0
  elseif min_overlap == overlap_z then
    if velocity.z > 0 then
      new_pos.z = other_min.z - (my_max.z - my_min.z)
    elseif velocity.z < 0 then
      new_pos.z = other_max.z
    else
      if my_min.z < other_min.z then
        new_pos.z = other_min.z - (my_max.z - my_min.z)
      else
        new_pos.z = other_max.z
      end
    end
    new_vel.z = 0
  end
  
  return new_pos, new_vel
end

function Player:init()
  self.aabb = AABB.new(Vector3(-0.25, 0, -0.25), Vector3(0.5, 1.1, 0.5))
end

function Player:tick(ctx)
  input_action{name = "left", control = "A"}
  input_action{name = "right", control = "D"}
  input_action{name = "forward", control = "W"}
  input_action{name = "back", control = "S"}

  input_action{name = "throw", control = "LCLICK"}

  local camera_forward = Vector3(draw_camera_from_principal_axes(self).direction)
  camera_forward.y = 0
  camera_forward = camera_forward:normalized()
  local camera_right = camera_forward:cross(Vector3.UP)

  local forward_input = util.b2n(input_action_pressed{name = "forward"}) - util.b2n(input_action_pressed{name = "back"})
  local strafe_input = util.b2n(input_action_pressed{name = "right"}) - util.b2n(input_action_pressed{name = "left"})

  local direction = ((camera_forward * forward_input) + (camera_right * strafe_input)):normalized()
  local target_vel = direction * self.speed
  -- self.velocity = self.velocity:lerp(target_vel, self.accel)
  self.velocity = target_vel:copy()
  self.position = self.position + self.velocity
  if self.aabb:intersects(self.test_intersect) then
    local new_pos, new_vel = resolve_collision(self.aabb, self.test_intersect, self.velocity)
    self.position:set(
      new_pos.x - aabb_offset.x,
      new_pos.y - aabb_offset.y,
      new_pos.z - aabb_offset.z
    )
    self.aabb.min:set(
      self.position.x + aabb_offset.x,
      self.position.y + aabb_offset.y,
      self.position.z + aabb_offset.z
    )
    self.velocity:sett(new_vel)
  end
  if input_action_just_pressed{name = "throw"} then
    self.ThrowPressed:emit(self.position:copy(), camera_forward:copy())
  end
  -- self.aabb.min = self.position + aabb_offset
  self.aabb.min:set(
    self.position.x + aabb_offset.x,
    self.position.y + aabb_offset.y,
    self.position.z + aabb_offset.z
  )

  if ctx.mouse_capture then
    self.yaw = self.yaw + self.mouse_sensitivity * ctx.mouse_movement.x
  end

  
  self.aabb:draw()
end

return Player