---
--- @package   eg4-crank-ui
--- @file      status-bar.lua
--- @brief     Functionality related to the home screen status bar
---
--- @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.                                              
---

local STATUS_BAR = "home_status_layer.status"

---
--- @brief  Update the status bar items.
--          Occurs when a control hidden status changes
--- @param gre#context mapargs
function CBUpdateStatusBar(mapargs)
  local group = STATUS_BAR
  local list = GetChildObjectList(group)
  local x = 0
  local data = {}
  for i = 1, #list do
      local control = tostring(list[i])
      if gre.get_value(control..".grd_hidden") == 0 then
        data[control..".grd_x"] = x
        x = x + gre.get_value(control..".grd_width") + 8
      end
  end
  gre.set_data(data)
end

---
--- @brief  Initialises the status bar when the model starts up. 
---
--- @param gre#context mapargs
function CBInitialiseStatusBar(mapargs)
  local group = STATUS_BAR
  local list = GetChildObjectList(group)
  local x = 0
  local data = {}
  for i = #list,1,-1 do
    data[tostring(list[i])..".grd_hidden"] = 1
  end
  gre.set_data(data)
end

---
--- @brief  Updates a status bar item putting it to the end of the list of items. 
---
function UpdateStatusItem(control, hidden)
  if SystemRebooting() then return end
  gre.set_control_attrs(STATUS_BAR.."."..control, { hidden = hidden, grd_zindex = 0 }) 
end

---
--- @brief  Callback for updating status bar item status. 
---
--- @param gre#context mapargs
function CBUpdateStatusItem(mapargs)
  UpdateStatusItem(mapargs.control, mapargs.hidden) 
end

-- EOF --
