---
--- @package   eg4-crank-ui
--- @file      dom-object-utilities.lua
--- @brief     functionality related to objects and object lists
---
--- @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  Gets the list of child objects from the specific object name.
--- @param  object - object for which to get children.
--- @return nil or table containing list of objects.
---
function GetChildObjectList(object)
  object = object and gredom.get_object(object)
  return object and object:get_children()
end


---
--- @brief  Gets the height of a group by scanning the child controls.
--- @param  group - group to scan
--- @return Returns height of group
---
function GetGroupHeight(group)
  local list = GetChildObjectList(group)
  if list == nil then return 0 end
  local height = 0
  for i = 1, #list do
      local control = tostring(list[i])
      if list[i]:get_type() == gredom.GROUP then
        local value = gre.get_group_attrs(control, "y").y + GetGroupHeight(control)
        if value > height then
          height = value
        end
      else
        local value = gre.get_control_attrs(control, "height", "y")
        local value = value.height + value.y
        if value > height then
          height = value
        end
      end 
  end
  return height
end

---
--- @brief  Repositions the groups/controls children of object depending on visibility and count the number of visible items..
--- @param  layer - layer to scan
--- @return count of visible groups.
---
function RepositionObjectChildren(object)
  local list = GetChildObjectList(object)
  if list == nil then return end
  local count = 0
  local y = nil
  for i = #list,1,-1 do
    local object = tostring(list[i])
    if string.sub(object, -7) ~= "_toggle" and 
       string.sub(object, -7) ~= "_slider" and 
       string.sub(object, -5) ~= "_spin" and
       string.sub(object, -7) ~= "_button" then
      local data = gre.get_control_attrs(object, "hidden", "y", "height")
      y = y or data.y
      if data.hidden == 0 then
        count = count + 1
        gre.set_control_attrs(object, {y = y})
        if data.height == 0 then
          if i ~= 1 then
            y = y + GetGroupHeight(object)
          end
        else
          y = y + data.height
        end
      end
    end
  end
  for i = #list,1,-1 do
    local object = tostring(list[i])
    if string.sub(object, -7) == "_toggle" then
      local data = gre.get_control_attrs(string.sub(object, 1, -8), "hidden", "y")
      if data.hidden == 0 then
        gre.set_control_attrs(object, { y = data.y + 10 })
      end
    elseif string.sub(object, -7) == "_slider" then
      local data = gre.get_control_attrs(string.sub(object, 1, -8), "hidden", "y")
      if data.hidden == 0 then
        gre.set_control_attrs(object, { y = data.y + 4 })
      end
    elseif string.sub(object, -5) == "_spin" then
      local data = gre.get_control_attrs(string.sub(object, 1, -6), "hidden", "y")
      if data.hidden == 0 then
        gre.set_control_attrs(object, { y = data.y + 15 })
      end
    elseif string.sub(object, -7) == "_button" then
      local data = gre.get_control_attrs(string.sub(object, 1, -8), "hidden", "y")
      if data.hidden == 0 then
        gre.set_control_attrs(object, { y = data.y + 10 })
      end
    end
  end
  return count
end

---
--- @brief  Clear all the child objects for the specified item.
---
function DeleteAllChildObjects(item)
  local list = GetChildObjectList(item) or {}
  for _,object in pairs(list) do
    gre.delete_control(tostring(object))
  end
end

-- EOF --