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

-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local MementoBar = MementoBar

-------------------------------------------------------------------------------------------------
-- PRIVATE FUNCTIONS --
-------------------------------------------------------------------------------------------------
function MementoBar:SetFragmentBehaviour()
    local base = MementoBar
    local frame = MementoBar_Frame
    local saved = base.Saved
    local fragment = base.Fragment
    local currentScene = SCENE_MANAGER:GetCurrentScene()
    local frameIsHidden = true

    if fragment == nil then
        base.Fragment = ZO_HUDFadeSceneFragment:New(frame)
        fragment = base.Fragment
    end

    if saved.ShowBarInMenu then
        SCENE_MANAGER:GetScene("gameMenuInGame"):AddFragment(fragment)

        -- Case where the user changes visibility in the settings
        if currentScene ~= nil and currentScene:GetName() == "gameMenuInGame" then
            frameIsHidden = false
        end
    else
        SCENE_MANAGER:GetScene("gameMenuInGame"):RemoveFragment(fragment)
    end

    if saved.ShowBarOnHud then
        HUD_SCENE:AddFragment(fragment)

        -- Case when landing on a scene, before a transition
        if currentScene == nil then
            frameIsHidden = false
        end
    else
        HUD_SCENE:RemoveFragment(fragment)
    end

    if saved.ShowBarOnHudUI then
        HUD_UI_SCENE:AddFragment(fragment)
    else
        HUD_UI_SCENE:RemoveFragment(fragment)
    end

    frame:SetHidden(frameIsHidden)
end