---
-- @package   eg4-crank-ui
-- @file      can-port-settings.lua
-- @brief     Functionality to related to the viewing and editing of can
--            port settings.
--
-- @copyright Copyright 2021 Hanover Displays Limited.
-- @license   This program is the confidential and proprietary product of
--            Hanover Displays Limited. Any unauthorised use, reproduction or
--            transfer of this program is strictly prohibited. (Subject to
--            limited distribution and restricted disclosure only.) All
--            rights reserved.
--
--

---
--- @brief  Initialises the can port setting screen
--- @param  gre#context mapargs
---
function CBInitCanPortSettingScreen(mapargs)
  local heading = gre.get_value(string.format(mapargs.context_control..".text.%d.1", mapargs.context_row))
  gre.set_value("mo_can_port.banner_heading", heading)

  local port_id = gre.get_value(string.format(mapargs.context_control..".port_id.%d.1", mapargs.context_row))
  gre.set_value("can_port_layer.menu_items.port_id", port_id)
end

---
--- @brief  Initialises the can port list layer with available can ports.
--- @param  gre#context mapargs
---
function CBInitCanPortSettingList(mapargs)

  if mapargs.context_event_data.text == nil then
    return
  end
  
  local settings = assert(loadstring(mapargs.context_event_data.text))()
  if settings == nil then
    return
  end
  
  local layer = mapargs.context_layer
  local group = layer..".group"
  local control = group..".items"

  -- Determine if item is already present.
  local index = 0
  local port_id
  repeat
    index = index + 1
    port_id = gre.get_value(string.format(control..".port_id.%d.1", index))
  until port_id == nil or port_id == "" or settings.port_id == port_id

  if port_id ~= settings.port_id then
    -- Add the new item to the menu list.
    gre.set_value(string.format(control..".port_id.%d.1", index), settings.port_id)

    local fmt = gre.get_value(control..".port_id_format")
    local heading = string.format(fmt, settings.port_id)

    gre.set_value(string.format(control..".text.%d.1", index), heading)

    gre.set_table_attrs(control, {height = index * 81, rows = index})

    SBCInitialiseScrollbar(mapargs)
  end
  gre.set_value(string.format(control..".value.%d.1", index), sbsettings.get_can_linked_protocol(settings.port_id))
end

---
--- @brief  Processes the currently avaialble CAN protocol list, populating the linked protocol screen.
--- @param  gre#context mapargs
---
function CBProcessAvailableCanProtocols(mapargs)
  local protocols = sbsettings.read_available_can_protocols()

  local group = "can_linked_protocol_layer.group"
  local control = group..".items"

  -- Adjust table to number of rows available
  gre.set_table_attrs(control, {height = #protocols * 81})
  gre.set_table_attrs(control, {rows = #protocols})

  gre.set_control_attrs(group..".horizontal_divider", {y = #protocols * 81- 1})

  local data = {}
  for row,value in pairs(protocols) do
    data[string.format(control..".text.%d.1", row)] = value
    data[string.format(control..".checked.%d.1", row)] = 0
  end

  gre.set_data(data)
end

---
--- @brief  Updates the current can port protocol setting
--- @param  gre#context mapargs
---
function CBWriteCanPortLinkedProtocols(mapargs)
  local text = gre.get_value(string.format(mapargs.context_control..".text.%d.1", mapargs.context_row))
  sbsettings.set_can_port_protocol(mapargs.value, text, mapargs.port_id)
end

-- EOF --


