local M = {}
M.type = "auxiliary"
M.relevantDevice = nil

local function init(jbeamData)

    electrics.values.fpvup = 0
    electrics.values.fpvrot = 0
    electrics.values.fpvright = 0
    electrics.values.fpvleft = 0
    electrics.values.fpvbackward = 0
    electrics.values.fpvforward = 0

    electrics.values.fpvpropfr = 0
    electrics.values.fpvpropfl = 0
    electrics.values.fpvproprr = 0
    electrics.values.fpvproprl = 0

    maxpower = 7
end

local clamp = clamp or function(x, min, max)
  if x < min then return min end
  if x > max then return max end
  return x
end


-- =========================
-- STATE
-- =========================
local yawRate = 0

-- =========================
-- TUNING
-- =========================
local rollFactor  = 0.8
local pitchFactor = 0.8

local yawMaxRate  = 2.0
local yawAccel    = 6.0
local yawDamping  = 4.0

local motorIdle   = 1.945
local airmode     = true

-- 0 = pure acro, >0 = self level
local angleStrength = 0.5

-- =========================
-- UPDATE
-- =========================
local function updateGFX(dt)
  -- INPUTS
  local throttle = electrics.values.fpvup or 0
  local rollIn   = electrics.values.fpvroll or 0
  local pitchIn  = electrics.values.fpvpitch or 0
  local yawIn    = electrics.values.fpvrot or 0

  -- =========================
  -- ANGULAR VELOCITIES (BeamNG)
  -- =========================
  local rollAV, pitchAV, yawAV = obj:getRollPitchYawAngularVelocity()

  -- =========================
  -- VEHICLE ANGLES (for angle mode)
  -- =========================
  local rot = quat(obj:getRotation()):toEulerYXZ()
  local roll  = rot.z
  local pitch = rot.y
  -- yaw angle not needed for control

  -- =========================
  -- ANGLE / ACRO BLEND
  -- =========================
  rollIn  = rollIn  - roll  * angleStrength + rollAV  * angleStrength * 0.2
  pitchIn = pitchIn + pitch * angleStrength - pitchAV * angleStrength * 0.2

  -- =========================
  -- YAW RATE LIMIT + DAMPING
  -- =========================
  local targetYawRate = yawIn * yawMaxRate
  yawRate = yawRate + (targetYawRate - yawRate) * clamp(yawAccel * dt, 0, 1)
  yawRate = yawRate - yawAV * yawDamping * dt
  yawRate = clamp(yawRate, -yawMaxRate, yawMaxRate)

  -- =========================
  -- MOTOR MIX (NO YAW)
  -- =========================
  local baseThrottle = throttle
  if airmode then
    baseThrottle = math.max(throttle, motorIdle)
  end

  local fl = baseThrottle + pitchIn * pitchFactor - rollIn * rollFactor --
  local fr = baseThrottle + pitchIn * pitchFactor + rollIn * rollFactor 
  local rl = baseThrottle - pitchIn * pitchFactor - rollIn * rollFactor --
  local rr = baseThrottle - pitchIn * pitchFactor + rollIn * rollFactor

  electrics.values.fpvpropfl = clamp(fl, motorIdle, 7)
  electrics.values.fpvpropfr = clamp(fr, motorIdle, 7)
  electrics.values.fpvproprl = clamp(rl, motorIdle, 7)
  electrics.values.fpvproprr = clamp(rr, motorIdle, 7)

  -- =========================
  -- YAW THRUSTER (BIDIRECTIONAL)
  -- =========================
  electrics.values.fpvyaw = (yawRate / yawMaxRate)
end

M.init = init
M.updateGFX = updateGFX

return M
