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

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

-------------------------------------------------------------------------------------------------
-- 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 == nil or currentScene:GetName() == "empty") and not base.Saved.ShowBarOnHud

    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) and scene ~= nil then
            if base.Saved[key] then
                if not scene:HasFragment(_type.Fragment) then
                    scene:AddFragment(_type.Fragment)
                end
            else
                scene:RemoveFragment(_type.Fragment)
            end
        end
        if scene == currentScene then
            isHidden = isHidden or not base.Saved[key]
        end
    end
    _type.Frame:SetHidden(isHidden)
end

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