---
-- @package   eg4-crank-ui
-- @file      serial-port-settings.lua
-- @brief     Functionality to related to the viewing and editing of serial
--            port settings.
--
-- @copyright Copyright 2019 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  Processes the currently avaialble G4 protocol list; populating the linked protocol screen.
--- @param  gre#context mapargs
---
function CBProcessAvailableG4SerialProtocols(mapargs)
  local protocols = sbsettings.read_available_g4_protocols()
  local group = "serial_port_connection_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 ipairs(protocols) do
    data[string.format(control..".text.%d.1", row)] = value
    data[string.format(control..".checked.%d.1", row)] = 0
    data[string.format(control..".fade.%d.1", row)] = 0
  end

  gre.set_data(data)
end

---
--- @brief  Initialises the serial port list layer with available serial ports.
--- @param  gre#context mapargs
---
function CBInitSerialPortSettingList(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..".serial_ports_list_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("serial_ports_list_layer.serial_ports_list_group.items.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})
    gre.set_table_attrs(control, {rows = index})

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

---
--- @brief  Initialises the serial port setting screen
--- @param  gre#context mapargs
---
function CBInitSerialPortSettingScreen(mapargs)
  local heading = gre.get_value(string.format(mapargs.context_control..".text.%d.1", mapargs.context_row))
  gre.set_value("mo_serial_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("serial_port_layer.menu_items.port_id", port_id)
end

---
--- @brief  Processes the serial port settings and populates the setting screen.
--- @param  gre#context mapargs
---
function CBReadSerialPortSetting(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..".menu_items"

  if settings.port_id == gre.get_value(group..".port_id") then
    local data = {}
    
    if settings.hardware_group == nil or settings.hardware_group == "" then
      data[group .. ".hardware.value"] =  gre.get_value(group..".hardware.not_fitted")
    else
      data[group .. ".hardware.value"] = settings.hardware_group
    end
    data[group .. ".serial_port_connection_linked_protocol.value"] = sbsettings.get_serial_port_connection_linked_protocol(settings.port_id)

    -- Show/Hide flow control option depending on hardware.
    local adv_group = "serial_port_advanced_layer.menu_items" 
    if settings.hardware_group == "RS232" then
      data[adv_group..".flow_control.grd_hidden"] = 0 
      data[adv_group..".flow_control_toggle.grd_hidden"] = 0
    else
      data[adv_group..".flow_control.grd_hidden"] = 1
      data[adv_group..".flow_control_toggle.grd_hidden"] = 1
    end

    gre.set_data(data)
  end
end

-- EOF --


