--[[
  @package    eg4-crank-ui-2
  @file       icon-bar.lua
  @brief      Supports the home screen icon bar.
]]


local MIN_ICONS = 3
local MAX_ICONS = 8
local DEFAULT_ICON_QTY = 4

local LAYER = "icon_bar_layer"
local GROUP = LAYER..".icons"
local SEPARATORS = LAYER..".separators"

---
--- @brief  Update the icon bar defaults.
--- @param  control  The control to update
--- @return The updated control
local function update_icon_bar_defaults(control)
  if get_service_id_required() == 1 then
    if control:object_name() == "service_run_number_entry" then
      control = control:object_parent()..".round_trip"
    elseif control:object_name() == "round_trip" then
      control = control:object_parent()..".service_run_number_entry"
    end
  end
  return control
end

---
--- @brief  Update the icon bar grid.
--- @param  mapargs  The map arguments from the callback
function cb_update_icon_bar(mapargs)
  local context_screen = mapargs and mapargs.context_screen or gre.env("active_screen")
  if context_screen ~= "home" then return end

  local qty_icons = sbsettings.get_home_screen_icon_bar_qty() or DEFAULT_ICON_QTY
  if qty_icons < MIN_ICONS then
    qty_icons = MIN_ICONS
  elseif qty_icons > MAX_ICONS then
    qty_icons = MAX_ICONS
  end

  local data = {}

  -- Set the icon separators.
  local icon_total_width = gre.get_value(LAYER..".sep.grd_x")
  local icon_spacing = math.floor(icon_total_width / qty_icons)
  data[SEPARATORS..".grd_width"] = icon_spacing * (qty_icons - 1)
  gre.set_table_attrs(SEPARATORS, { cols = (qty_icons - 1) })
  for i=1,(qty_icons - 1) do
    gre.set_table_cell_attrs(SEPARATORS, 1, i, { width = icon_spacing })
  end

  -- Get hide all icons and get default icon list.
  local icons = GROUP:get_children()
  local icon_list = {}
  for i=#icons,1,-1 do
    local control = update_icon_bar_defaults(tostring(icons[i]))
    data[control..".grd_hidden"] = true
    if i > #icons - qty_icons then
      table.insert(icon_list, control)
    end
  end

  -- Replace defaults with settings.
  local setting = get_home_screen_icon_list()
  for i,control in pairs(setting) do
    -- Remove existing icon from list.
    for i=1,#icon_list do
      if icon_list[i] == GROUP.."."..control then
        icon_list[i] = nil
      end
    end
    icon_list[tonumber(i)] = GROUP.."."..control
  end

  -- Update the icon bar.
  for i = 1,qty_icons do
    local control = icon_list[i]
    if control then
      local hidden = false
      local fn = _G["show_"..control:object_name()]
      if type(fn) == "function" then
        hidden = not fn(context_screen)
      end
      data[control..".grd_hidden"] = hidden

      local fn = _G["active_"..control:object_name()]
      if fn then
        data[control..".grd_active"] = fn() and 1 or 0
        data[control..".alpha_active"] = fn() and 255 or 85
      end
      data[control..".grd_x"] = icon_spacing * (i - 1)
      if i == qty_icons then
        data[control..".grd_width"] = icon_total_width - data[control..".grd_x"]
      else
        data[control..".grd_width"] = icon_spacing - 1
      end
    end
  end
  gre.set_data(data)
end

