--[[
 * Copyright (C) Rotorflight Project
 *
 * License GPLv3: https://www.gnu.org/licenses/gpl-3.0.en.html
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Note. Some icons have been sourced from https://www.flaticon.com/
]] --
local core = assert(rfsuite.compiler.loadfile("tasks/msp/api_core.lua"))()

-- Constants for MSP Commands
local API_NAME = "ESC_PARAMETERS_OMP" -- API name (must be same as filename)
local MSP_API_CMD_READ = 217 -- Command identifier 
local MSP_API_CMD_WRITE = 218 -- Command identifier 
local MSP_REBUILD_ON_WRITE = false -- Rebuild the payload on write 
local MSP_SIGNATURE = 0xD0
local MSP_HEADER_BYTES = 2

-- tables used in structure below
local flightMode = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_fmheli)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_fmfw)@"}
local motorDirection = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_cw)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_ccw)@"}
local becLvVoltage = {"6.0V", "7.4V","8.4V"}
local startupPower = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_low)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_medium)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_high)@"}
local fanControl = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_on)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_off)@"}
local ledColor = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_red)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_yellow)@","@i18n(api.ESC_PARAMETERS_OMP.tbl_orange)@","@i18n(api.ESC_PARAMETERS_OMP.tbl_green)@","@i18n(api.ESC_PARAMETERS_OMP.tbl_jadegreen)@","@i18n(api.ESC_PARAMETERS_OMP.tbl_blue)@","@i18n(api.ESC_PARAMETERS_OMP.tbl_cyan)@","@i18n(api.ESC_PARAMETERS_OMP.tbl_purple)@","@i18n(api.ESC_PARAMETERS_OMP.tbl_pink)@","@i18n(api.ESC_PARAMETERS_OMP.tbl_white)@"}
local becHvVoltage = {"6.0V", "6.2V", "6.4V", "6.6V", "6.8V", "7.0V", "7.2V", "7.4V", "7.6V", "7.8V", "8.0V", "8.2V", "8.4V", "8.6V", "8.8V", "9.0V", "9.2V", "9.4V", "9.6V", "9.8V", "10.0V", "10.2V", "10.4V", "10.6V", "10.8V", "11.0V", "11.2V", "11.4V", "11.6V", "11.8V", "12.0V"}
local lowVoltage = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_off)@", "2.7V", "3.0V", "3.2V", "3.4V", "3.6V", "3.8V"}
local timing = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_auto)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_low)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_medium)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_high)@"}
local accel = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_fast)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_normal)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_slow)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_vslow)@"}
local brakeType = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_normal)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_reverse)@"}
local autoRestart = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_off)@", "90s"}
local srFunc = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_on)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_off)@"}
local govMode = {"@i18n(api.ESC_PARAMETERS_OMP.tbl_escgov)@", "@i18n(api.ESC_PARAMETERS_OMP.tbl_extgov)@" , "@i18n(api.ESC_PARAMETERS_OMP.tbl_fwgov)@"}

-- api structure
local MSP_API_STRUCTURE_READ_DATA = {
    {field = "esc_signature",       type = "U8",  apiVersion = 12.07, simResponse = {208}, help = "@i18n(api.ESC_PARAMETERS_OMP.esc_signature)@"},
    {field = "esc_command",         type = "U8",  apiVersion = 12.07, simResponse = {0}, help = "@i18n(api.ESC_PARAMETERS_OMP.esc_command)@"},
    {field = "esc_model",           type = "U8",  apiVersion = 12.07, simResponse = {23}, help = "@i18n(api.ESC_PARAMETERS_OMP.esc_model)@"},
    {field = "esc_version",         type = "U8",  apiVersion = 12.07, simResponse = {3}, help = "@i18n(api.ESC_PARAMETERS_OMP.esc_version)@"},
    {field = "governor",            type = "U16", apiVersion = 12.07, simResponse = {0, 0}, tableIdxInc = -1, table = govMode, help = "@i18n(api.ESC_PARAMETERS_OMP.governor)@"},
    {field = "cell_cutoff",         type = "U16", apiVersion = 12.07, simResponse = {0, 0}, tableIdxInc = -1, table = lowVoltage, help = "@i18n(api.ESC_PARAMETERS_OMP.cell_cutoff)@"},
    {field = "timing",              type = "U16", apiVersion = 12.07, simResponse = {0, 0}, tableIdxInc = -1, table = timing, help = "@i18n(api.ESC_PARAMETERS_OMP.timing)@"},
    {field = "lv_bec_voltage",      type = "U16", apiVersion = 12.07, simResponse = {0, 0}, tableIdxInc = -1, table = becLvVoltage, help = "@i18n(api.ESC_PARAMETERS_OMP.lv_bec_voltage)@"},
    {field = "motor_direction",     type = "U16", apiVersion = 12.07, simResponse = {0, 0}, tableIdxInc = -1, table = motorDirection, help = "@i18n(api.ESC_PARAMETERS_OMP.motor_direction)@"},
    {field = "gov_p",               type = "U16", apiVersion = 12.07, simResponse = {4, 0}, min = 1, max = 10, default = 5, offset = 1, help = "@i18n(api.ESC_PARAMETERS_OMP.gov_p)@"},
    {field = "gov_i",               type = "U16", apiVersion = 12.07, simResponse = {3, 0}, min = 1, max = 10, default = 5, offset = 1, help = "@i18n(api.ESC_PARAMETERS_OMP.gov_i)@"},
    {field = "acceleration",        type = "U16", apiVersion = 12.07, simResponse = {0, 0}, tableIdxInc = -1, table = accel, help = "@i18n(api.ESC_PARAMETERS_OMP.acceleration)@"},
    {field = "auto_restart_time",   type = "U16", apiVersion = 12.07, simResponse = {0, 0}, tableIdxInc = -1, table = autoRestart, help = "@i18n(api.ESC_PARAMETERS_OMP.auto_restart_time)@"},
    {field = "hv_bec_voltage",      type = "U16", apiVersion = 12.07, simResponse = {0, 0}, tableIdxInc = -1, table = becHvVoltage, help = "@i18n(api.ESC_PARAMETERS_OMP.hv_bec_voltage)@"},
    {field = "startup_power",       type = "U16", apiVersion = 12.07, simResponse = {0, 0}, table = startupPower, tableIdxInc = -1, help = "@i18n(api.ESC_PARAMETERS_OMP.startup_power)@"},
    {field = "brake_type",          type = "U16", apiVersion = 12.07, simResponse = {0, 0}, tableIdxInc = -1, table = brakeType, help = "@i18n(api.ESC_PARAMETERS_OMP.brake_type)@"},
    {field = "brake_force",         type = "U16", apiVersion = 12.07, simResponse = {0, 0}, min = 0, max = 100, default = 0, unit = "%", help = "@i18n(api.ESC_PARAMETERS_OMP.brake_force)@"},
    {field = "sr_function",         type = "U16", apiVersion = 12.07, simResponse = {0, 0}, table = srFunc, tableIdxInc = -1, help = "@i18n(api.ESC_PARAMETERS_OMP.sr_function)@"},
    {field = "capacity_correction", type = "U16", apiVersion = 12.07, simResponse = {0, 0}, min = 0, max = 20, default = 10, offset = -10, unit = "%", help = "@i18n(api.ESC_PARAMETERS_OMP.capacity_correction)@"},
    {field = "motor_poles",         type = "U16", apiVersion = 12.07, simResponse = {9, 0}, min = 1, max = 55, default = 10, step = 1 , offset = 1, help = "@i18n(api.ESC_PARAMETERS_OMP.motor_poles)@"},
    {field = "led_color",           type = "U16", apiVersion = 12.07, simResponse = {0, 0}, tableIdxInc = -1, table = ledColor, help = "@i18n(api.ESC_PARAMETERS_OMP.led_color)@"},
    {field = "smart_fan",           type = "U16", apiVersion = 12.07, simResponse = {0, 0}, tableIdxInc = -1, table = fanControl, help = "@i18n(api.ESC_PARAMETERS_OMP.smart_fan)@"},
    {field = "activefields",        type = "U32", apiVersion = 12.07, simResponse = {238, 255, 1, 0}, help = "@i18n(api.ESC_PARAMETERS_OMP.activefields)@"},
}

-- Process structure in one pass
local MSP_API_STRUCTURE_READ, MSP_MIN_BYTES, MSP_API_SIMULATOR_RESPONSE =
    core.prepareStructureData(MSP_API_STRUCTURE_READ_DATA)

-- set read structure
local MSP_API_STRUCTURE_WRITE = MSP_API_STRUCTURE_READ


-- Variable to store parsed MSP data
local mspData = nil
local mspWriteComplete = false
local payloadData = {}
local defaultData = {}

-- Create a new instance
local handlers = core.createHandlers()

-- Variables to store optional the UUID and timeout for payload
local MSP_API_UUID
local MSP_API_MSG_TIMEOUT

-- Track write completion without closures
local lastWriteUUID = nil
-- weak keys/values so finished entries don't pin memory
local writeDoneRegistry = setmetatable({}, { __mode = "kv" })


local function processReplyStaticRead(self, buf)
  core.parseMSPData(API_NAME, buf, self.structure, nil, nil, function(result)
    mspData = result
    if #buf >= (self.minBytes or 0) then
      local getComplete = self.getCompleteHandler
      if getComplete then
        local complete = getComplete()
        if complete then complete(self, buf) end
      end
    end
  end)
end

local function processReplyStaticWrite(self, buf)
  mspWriteComplete = true
  -- mark this UUID as completed (no module locals touched)
  if self.uuid then writeDoneRegistry[self.uuid] = true end

  local getComplete = self.getCompleteHandler
  if getComplete then
    local complete = getComplete()
    if complete then complete(self, buf) end
  end
end

local function errorHandlerStatic(self, buf)
  local getError = self.getErrorHandler
  if getError then
    local err = getError()
    if err then err(self, buf) end
  end
end

-- Function to initiate MSP read operation
local function read()
  if MSP_API_CMD_READ == nil then
    rfsuite.utils.log("No value set for MSP_API_CMD_READ", "debug")
    return
  end

  local message = {
    command           = MSP_API_CMD_READ,
    structure         = MSP_API_STRUCTURE_READ,   -- add this
    minBytes          = MSP_MIN_BYTES,            -- and this
    processReply      = processReplyStaticRead,
    errorHandler      = errorHandlerStatic,
    simulatorResponse = MSP_API_SIMULATOR_RESPONSE,
    uuid              = MSP_API_UUID,
    timeout           = MSP_API_MSG_TIMEOUT,
    getCompleteHandler = handlers.getCompleteHandler,
    getErrorHandler    = handlers.getErrorHandler,
    -- optional: place to stash parsed data if you want it here:
    mspData           = nil,
  }
  rfsuite.tasks.msp.mspQueue:add(message)
end

local function write(suppliedPayload)
  if MSP_API_CMD_WRITE == nil then
    rfsuite.utils.log("No value set for MSP_API_CMD_WRITE", "debug")
    return
  end

  -- Build payload eagerly (no capture)
  local payload = suppliedPayload or
    core.buildWritePayload(API_NAME, payloadData, MSP_API_STRUCTURE_WRITE, MSP_REBUILD_ON_WRITE)

  -- Choose a UUID for this write; if you already set MSP_API_UUID elsewhere, we’ll reuse it
  local uuid = MSP_API_UUID or rfsuite.utils and rfsuite.utils.uuid and rfsuite.utils.uuid() or tostring(os.clock())
  lastWriteUUID = uuid  -- track the most recent write without a closure

  local message = {
    command            = MSP_API_CMD_WRITE,
    payload            = payload,
    processReply       = processReplyStaticWrite, -- static, no upvalues
    errorHandler       = errorHandlerStatic,      -- static, no upvalues
    simulatorResponse  = {},

    uuid               = uuid,
    timeout            = MSP_API_MSG_TIMEOUT,

    -- provide handler getters so static callbacks can resolve at runtime
    getCompleteHandler = handlers.getCompleteHandler,
    getErrorHandler    = handlers.getErrorHandler,
  }

  rfsuite.tasks.msp.mspQueue:add(message)
end

-- Function to get the value of a specific field from MSP data
local function readValue(fieldName)
    if mspData and mspData['parsed'][fieldName] ~= nil then return mspData['parsed'][fieldName] end
    return nil
end

-- Function to set a value dynamically
local function setValue(fieldName, value)
    payloadData[fieldName] = value
end

-- Function to check if the read operation is complete
local function readComplete()
    return mspData ~= nil and #mspData['buffer'] >= MSP_MIN_BYTES
end

-- Function to check if the write operation is complete
local function writeComplete()
    return mspWriteComplete
end

-- Function to reset the write completion status
local function resetWriteStatus()
    mspWriteComplete = false
end

-- Function to return the parsed MSP data
local function data()
    return mspData
end

-- set the UUID for the payload
local function setUUID(uuid)
    MSP_API_UUID = uuid
end

-- set the timeout for the payload
local function setTimeout(timeout)
    MSP_API_MSG_TIMEOUT = timeout
end
-- Return the module's API functions
return {
    read = read,
    write = write,
    readComplete = readComplete,
    writeComplete = writeComplete,
    readValue = readValue,
    setValue = setValue,
    resetWriteStatus = resetWriteStatus,
    setCompleteHandler = handlers.setCompleteHandler,
    setErrorHandler = handlers.setErrorHandler,
    data = data,
    setUUID = setUUID,
    setTimeout = setTimeout,
    mspSignature = MSP_SIGNATURE,
    mspHeaderBytes = MSP_HEADER_BYTES,
    simulatorResponse = MSP_API_SIMULATOR_RESPONSE
}