--[[
  @package    eg4-crank-ui-2
  @file       information-select.lua
  @brief      Holds the user information selection functions
]]

local info = {}

---
--- @brief  Indicate that the information icon is visible
--- @return true - show information control
function show_information()
  return (sbsettings.get_info_messages_present() == 1) and (sbsettings.get_allow_info_messages_display() == 1)
end

---
--- @brief  Sends message to clear all
--- @param  info_message_id - text for message id.
---
function clear_all_info_messages()
  send_event("clear_all_info_messages")
end

---
--- @brief  Sends message to clear all
--- @param  info_message_id - text for message id.
---
function cb_clear_all_info_messages()
  clear_all_info_messages()
  screen_transition_idle()
end

---
--- @brief  Sends out the selected info message id to be added event to backend
--- @param  value - text for message id.
---
local function send_info_message(value)
  if sbsettings.get_allow_multiple_messages() == 0 then
    cb_clear_all_info_messages()
  end
  send_event("add_info_message", value)
end

---
--- @brief  Sends out the selected info message id event to backend
--- @param  value message code to send
---
function cb_info_message_code_submit(value)
  if not sbsettings.validate_info_code(value) and tonumber(value) ~= 0 then return end
  send_info_message(value)
  screen_transition_idle()
  return true
end

---
--- @brief  Sends out the selected info message id event to backend
--- @param  value message code to send
---
function cb_set_information_message(mapargs)
  local value = gre.get_value(mapargs.context_control..".info_id."..mapargs.context_row..".1")
  send_info_message(value)
  screen_transition_idle()
  return true
end

---
--- @brief  toggles the code/browse button display on the home screen
---         which times out after 5 seconds
function cb_toggle_information_select(mapargs)
  if sbsettings.get_info_messages_present() == 0 then return end
  toggle_select(mapargs, "information", "cb_start_information_entry", "cb_start_information_browse", sbsettings.get_information_selection_mode())
end

---
--- @brief  Start the information code entry
function cb_start_information_entry()
  start_driver_login(sbsettings.get_information_requires_passcode(), "select_information_code")
end

---
--- @brief  Start the information browse
function cb_start_information_browse()
  start_driver_login(sbsettings.get_information_requires_passcode(), "browse_information")
end

---
--- @brief  Start the information code entry or browse
--- @param  mapargs - table containing the map arguments
function cb_icons_menu_information(mapargs)
  if sbsettings.get_information_selection_mode() == 1 then
    cb_start_information_browse(mapargs)
  else
    cb_start_information_entry(mapargs)
  end
end