--[[
Author: Jarth
Filename: CBs_Fragment.lua
]] --
-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local base = CollectionBars
local texts = base.Texts

-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-------------------------------------------------------------------------------------------------
function base.UpdateFragments(fragmentType)
    for _, category in pairs(base.Categories) do if category.Saved.Enabled then base.UpdateFragment(category, fragmentType) end end
    base.UpdateFragment(base.Global.Combine, fragmentType)
end

function base.UpdateFragment(category, fragmentType)
    local currentScene = SCENE_MANAGER:GetCurrentScene()
    local isHidden = (currentScene == nil or currentScene:GetName() == "empty") and not base.Saved.ShowBarOnHud

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

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

function base.RemoveFragments(category) if category.Fragment ~= nil then for _, scene in pairs(base.Global.ScenePairs) do if scene ~= nil then scene:RemoveFragment(category.Fragment) end end end end