--[[
  @package    eg4-crank-ui-2
  @file       configured-equipment-list.lua
  @brief      Populates the configured equipment list
]]

local CONNECTED_EQUIPMENT_STATUS_LIST_LAYER = "configured_equipment_list_layer"
local CONNECTED_EQUIPMENT_STATUS_CONTROL = CONNECTED_EQUIPMENT_STATUS_LIST_LAYER..".menu.items"

---
--- @brief  Add information to data about sign mapping.
--- @param  data The data table to add the information to.
--- @param  row  The grid row to add the information to.
--- @param  hcp_address  The sign address
--- @param  info  The sign height and width
local function add_configured_device_item(data, row, hcp_address, info)
  local control = CONNECTED_EQUIPMENT_STATUS_CONTROL

  data[control..".alpha_pressed."..row..".1"] = 0
  data[control..".text."..row..".1"] = "#"..hcp_address
  data[control..".value."..row..".1"] = ""
  data[control..".sizing."..row..".1"] = "---"
  data[control..".selectable."..row..".1"] = 0
  data[control..".status_ok."..row..".1"] = 0
  data[control..".status_info."..row..".1"] = 0
  data[control..".status_warning."..row..".1"] = 0
  data[control..".status_error."..row..".1"] = 0

  if info == nil then return end

  if info.sign_page_lookup ~= nil and tonumber(info.sign_page_lookup) ~= hcp_address then
    data[control..".text."..row..".1"] = data[control..".text."..row..".1"].." ["..info.sign_page_lookup.."]"
  end
  if info.type then
    data[control..".value."..row..".1"] = gre.get_value(CONNECTED_EQUIPMENT_STATUS_LIST_LAYER..".device_type."..(info.type + 1)..".1") or ""
  end

  local height = info.height
  local width = info.width
  if height or width then
    if width == nil or width == -1 then width = "?" end
    if height == nil or height == -1 then height = "?" end
    data[control..".sizing."..row..".1"] = string.format("%s x %s",  tostring(width), tostring(height))
  else
    data[control..".sizing."..row..".1"] = "---"
  end

  local status,control_id = check_hcp_address_has_status(hcp_address)
  if not status then return end

  data[control.."."..status.."."..row..".1"] = 255
  data[control..".selectable."  ..row..".1"] = 255
  data[control..".control_id."  ..row..".1"] = control_id
end

---
--- @brief  Populate the screen with configured equipment
function cb_populate_configured_equipment(mapargs)
  local context_screen = mapargs and mapargs.context_screen or gre.env("active_screen")
  if context_screen ~= "configured_equipment_list" then return end

  local info = sbsettings.read_configured_devices()['*'] or {}
  local data = {}
  local row = 0
  for hcp_address=0,14 do
    row = row + 1
    add_configured_device_item(data, row, hcp_address, info[hcp_address])
  end

  gre.set_data(data)

  local height = gre.get_table_cell_attrs(CONNECTED_EQUIPMENT_STATUS_CONTROL, 1, 1, "height").height
  gre.set_table_attrs(CONNECTED_EQUIPMENT_STATUS_CONTROL, { height = row * height, rows = row })

  cb_update_menu(mapargs)
end

---
--- @brief  Handle select of configured item.
--- @param  mapargs The mapargs from the callback.
function cb_configured_equipment_select(mapargs)
  -- TODO: Fix this function
  local row = mapargs.context_row
  if gre.get_value(mapargs.context_control..".selectable."..row..".1") == 0 then return end
  if mapargs.context_event == "gre.press" then
    gre.set_value(mapargs.context_control..".alpha_pressed."..row..".1", 255)
    cb_click()
    return
  elseif mapargs.context_event == "gre.outbound" or mapargs.context_event == "gre.release" then
    gre.set_value(mapargs.context_control..".alpha_pressed."..row..".1", 0)
    return
  end
  if mapargs.context_event ~= "gre.touch" then return end

  local control_id = gre.get_value(mapargs.context_control..".control_id."..row..".1")
  connected_equipement_status_select(control_id)
end

---
--- @brief  Fetch the active and mapped configured item count
--- @return The configured and mapped item counts
function fetch_configured_equipment_counts()
  return {
    configured = #sbsettings.read_preset_hcp_address_list(),
    mapped = #sbsettings.read_sign_address_list()
  }
end
