--[[
  @package    eg4-crank-ui-2
  @file       sign-mappings.lua
  @brief      Handles the sign mapping settings screens
]]


--- @brief  Getter function for sign mapping text. Returns more complex text than
---         is available from backend
--- @param  port_id - sign to get mapping text.
---
local context_control
function get_sign_mapping_setting_text(port_id)
  local type = sbsettings.get_sign_default_device_type(port_id)
  local index = sbsettings.get_sign_default_database_index(port_id)
  if type == "DESTINATION_SIGN" and (index == nil or index == "" or index == port_id) then
    return gre.get_value(context_control..".default")
  end
  return "["..type.."] "..index
end

---
--- @brief  Initialises the sign settings mappings list screen.
--- @param  gre#context mapargs
function cb_init_sign_settings_mappings(mapargs)
  if context_control == nil then
    context_control = get_list_control(mapargs.context_screen)
  end

  local rows = 0
  local data = {}
  local format = gre.get_value(context_control..".sign_id_format")
  for i = 0, 14 do
    rows = rows + 1
    local port_id = tostring(i)
    data[context_control..".port_id."..rows..".1"] = port_id
    data[context_control..".text."..rows..".1"] = string.format(format, port_id)
    local text = get_sign_mapping_setting_text(port_id)
    data[context_control..".value."..rows..".1"] = text
    data[context_control..".alpha_pressed."..rows..".1"] = 0
  end
  gre.set_data(data)
  local height = gre.get_table_cell_attrs(context_control.."",1,1,"height").height
  gre.set_table_attrs(context_control, { rows = rows, height = height * rows })

  cb_update_menu(mapargs)
end

---
--- @brief  Populates the sign mapping types list
--- @param gre#context mapargs
local context_control
local context_screen
function cb_start_sign_mapping_type_selection(mapargs)
  context_control = mapargs.context_control
  context_screen = mapargs.context_screen
  mapargs.parameter = "sign_default_device_type"
  cb_start_radio_select(mapargs, sbsettings.get_sign_default_device_type_list())
end

---
--- @brief  Store the device type and initiate numeric entry for index if required.
--- @param  value - the device type to set
--- @param  port_id - the port to set
local device_type
function set_sign_default_device_type(value, port_id)
  if value == "DESTINATION_SIGN" then
    device_type = value
    cb_numeric_entry_start({
      context_control = context_control,
      context_screen = context_screen,
      value = sbsettings.get_sign_default_database_index(port_id)
    })
  else
    sbsettings.set_sign_default_device_type(value, port_id)
    sbsettings.set_sign_default_database_index("", port_id)
  end
end

---
--- @brief  Store the index and and device type, and return to the previous screen.
--- @param  value - the index to set
--- @param  port_id - the port to set
function cb_set_sign_mapping(value, port_id)
  sbsettings.set_sign_default_device_type(device_type, port_id)
  sbsettings.set_sign_default_database_index(value, port_id)
  screen_transition(context_screen)
  return true
end

---
--- @brief  Sets the sign mapping height
--- @param  value - the height to set
--- @param  port_id - the port to set
function cb_set_sign_height(value, port_id)
  sbsettings.set_sign_default_configured_height(tonumber(value), port_id)
  cb_back_screen()
  return true
end

---
--- @brief  Sets the sign mapping width
--- @param  value - the width to set
--- @param  port_id - the port to set
function cb_set_sign_width(value, port_id)
  sbsettings.set_sign_default_configured_width(tonumber(value), port_id)
  cb_back_screen()
  return true
end

---
--- @brief  Sets the sign mapping extra configuration string
--- @param  value - the string to set
--- @param  port_id - the port to set the string for
function cb_set_sign_mapping_config_string(value, port_id)
  sbsettings.set_sign_default_config_string(value, port_id)
  cb_back_screen()
  return true
end
