--[[
Author: Jarth
Filename: CBs_Fragment.lua
]] --

-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local base = CollectionBars

-------------------------------------------------------------------------------------------------
--  PRIVATE FUNCTIONS --
-------------------------------------------------------------------------------------------------

function base.UpdateFragments(fragmentType)
    for _, _type in pairs(base.Type) do
        if _type.Saved.Enabled then
            base.UpdateFragment(_type, fragmentType)
        end
    end
    base.UpdateFragment(base.Global.Combine, fragmentType)
end

function base.UpdateFragment(_type, fragmentType)
    local currentScene = SCENE_MANAGER:GetCurrentScene()
    local isHidden = currentScene == SCENE_MANAGER:GetScene("gameMenuInGame") and not base.Saved.ShowBarInMenu

    if _type.Fragment == nil then
        _type.Fragment = ZO_HUDFadeSceneFragment:New(_type.Frame)
    end

    for key, scene in pairs(base.Global.ScenePairs) do
        if not fragmentType or key == fragmentType then
            if scene ~= nil then
                if base.Saved[key] then
                    if scene == currentScene then
                        isHidden = false
                    end
                    if not scene:HasFragment(_type.Fragment) then
                        scene:AddFragment(_type.Fragment)
                    end
                else
                    scene:RemoveFragment(_type.Fragment)
                end
            end
        end
    end

    _type.Frame:SetHidden(isHidden)
end