--[[
  @package    eg4-crank-ui-2
  @file       dom-object-utils.lua
  @brief      utilities related to gredom objects.
]]


local DEFAULT_LIST_LOCATION = ".menu.list"

---
--- @brief  Get the children of an object
--- @param  object  The object as string to get the children of
--- @return The children or nil if not found
function get_children(object)
  object = object and gredom.get_object(object)
  return object and object:get_children()
end

---
--- @brief  Get the children of an dom object represented by a string
--- @return The children or nil if not found
function string:get_children()
  local object = gredom.get_object(self)
  return object and object:get_children()
end


---
--- @brief  Find the child object with the specified name in the parent object
--- @param  parent  The parent object to search in
--- @param  name    The name of the child object to find
--- @return The child object as string or nil if not found
function find_child_object(parent, name)
  if not parent then return end
  for _,object in ipairs(parent:get_children()) do
    local item = tostring(object)
    if item:ends_with("."..name) then
      return item
    else
      item = find_child_object(item, name)
      if item then return item end
    end
  end
end

---
--- @brief  Find the child object with the specified name in the parent object`
--- @param  parent  The parent object to search in
--- @return The child object as string or nil if not found
function string:find_child_object(name)
  return find_child_object(self, name)
end

---
--- @brief  Filter the visible controls from a list
--- @param  list  The list of controls
--- @return The list of visible controls
function filter_visible(list)
  if not list then return {} end
  local visible = {}
  for i = #list,1,-1 do
    local obj = list[i]
    if not obj:get_hidden() then
      table.insert(visible, tostring(obj))
    end
  end
  return visible
end

---
--- @brief  Filter the visible controls from a list
--- @param  object  The object as string to get the children of
--- @return The list of visible controls
function get_visible_children(object)
  return filter_visible(get_children(object))
end

---
--- @brief  Filter the visible controls from a list for a string object
--- @return The list of visible controls
function string:get_visible_children()
  return filter_visible(get_children(self))
end


---
--- @brief  Clear all the child objects for the specified item.
--- @param  parent  The parent object to clear the children for
function delete_all_child_objects(parent)
  for _,object in pairs(gredom.get_object(parent):get_children()) do
    gre.delete_object(tostring(object))
  end
end

---
--- @brief  Get the list control for the specified screen object
--- @param  context_screen  The screen object to get the list control for
--- @return The list control or nil if not found
function get_list_control(context_screen)
  local layers = gredom.get_object(context_screen):get_children()
  local control = DEFAULT_LIST_LOCATION
  for _,layer in ipairs(layers) do
    local value = tostring(layer):sub(#context_screen + 2)..control
    if gre.get_value(value..".grd_x") then
      return value
    end
  end
end
