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

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

-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-------------------------------------------------------------------------------------------------
function base.InitializeSettingsSlash()
    SLASH_COMMANDS[base.Global.SettingsSlash] = function()
        base.ToggleEnableSettings()
    end
end

function base.ToggleEnableSettings()
    base.Global.EnableSettings = not base.Global.EnableSettings
    base.RestoreFrames()
    base.RestoreCombineLabels()
end

function base.SetupToggleSettings(_type)
    local toggleSettings = GetControl(base.Global.HideAllId .. _type.Name .. "ToggleSettings")
    if toggleSettings then
        local hideAllSettingsFunc = function()
            base.ShowSettings(_type)
        end
        toggleSettings:SetHandler("OnClicked", hideAllSettingsFunc)
    end
end

function base.UpdateToggleSettings(_type)
    local offsetX = 0
    if base.Global.EnableSettings then
        offsetX = -19
    end

    if _type.FrameLabel and _type.FrameToggleSettings then
        _type.FrameToggleSettings:ClearAnchors()
        _type.FrameToggleSettings:SetAnchor(TOPLEFT, _type.FrameLabel, TOPRIGHT, offsetX, 0)
        _type.FrameToggleSettings:SetAnchor(BOTTOMRIGHT, _type.FrameLabel, BOTTOMRIGHT, 0, 0)
    end
    if _type.FrameLabel and _type.FrameLabelButton then
        _type.FrameLabelButton:ClearAnchors()
        _type.FrameLabelButton:SetAnchor(TOPLEFT, _type.FrameLabel, TOPLEFT, 0, 0)
        _type.FrameLabelButton:SetAnchor(BOTTOMRIGHT, _type.FrameLabel, BOTTOMRIGHT, offsetX, 0)
    end
end

function base.SetupSettingsFrameHandlers(control, text, onClicked)
    control.tooltipText = text
    control:SetHandler("OnClicked", onClicked)
    control:SetHandler(
        "OnMouseEnter",
        function(_control)
            ZO_Tooltips_ShowTextTooltip(_control, BOTTOM, _control.tooltipText)
        end
    )
    control:SetHandler(
        "OnMouseExit",
        function()
            ZO_Tooltips_HideTextTooltip()
        end
    )
end

function base.SetupSetttingsFrame(_type)
    local selector = base.Addon.Abbreviation .. "_Settings"

    base.Global.SettingsFrame = base.WM:CreateControlFromVirtual(selector, GuiRoot, selector)
    base.Global.SettingsLabel = GetControl(selector .. "Label")
    base.Global.SettingsList = CBs_List:New(base, _type, base.Global.SettingsFrame)

    local titleControl = GetControl(selector .. "Title")
    titleControl:SetText(base.Addon.DisplayName .. " Settings")

    local closeFrame = GetControl(base.Global.SettingsFrame, "Close")
    if closeFrame then
        base.SetupSettingsFrameHandlers(
            closeFrame,
            "Close",
            function(buttonControl)
                local control = buttonControl:GetParent()
                control:SetHidden(true)
            end
        )
    end
    local moveFrame = GetControl(base.Global.SettingsFrame, "Move")
    if moveFrame then
        base.SetupSettingsFrameHandlers(
            moveFrame,
            "Toggle move frame",
            function()
                base.Global.IsMoveEnabled = not base.Global.IsMoveEnabled
                base.RestoreFrames()
            end
        )
    end
    local refreshFrame = GetControl(base.Global.SettingsFrame, "Refresh")
    if refreshFrame then
        base.SetupSettingsFrameHandlers(
            refreshFrame,
            "Reload collection list",
            function()
                base.CreateCollection(_type)
                base.RestoreFrame(_type)
                base.RestoreCombineLabels()
                base.Global.SettingsList:RefreshData()
            end
        )
    end
end

function base.ShowSettings(_type)
    local show = false
    if base.Global.SettingsFrame == nil then
        base.SetupSetttingsFrame(_type)
        show = true
    end

    show = show or base.Global.SettingsFrame:IsHidden() or _type ~= base.Global.SettingsList._type
    base.Global.SettingsFrame:SetHidden(not show)
    if show then
        base.Global.SettingsLabel:SetText(show and _type.Name or "")
        base.Global.SettingsList._type = show and _type or nil
        base.Global.SettingsList:RefreshData()
    end
end