--[[
Author: Jarth
Filename: CBs_Settings.lua
]] --
-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local base = CollectionBars

-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-------------------------------------------------------------------------------------------------
function base.InitializeSettingsSlash() SLASH_COMMANDS[base.Addon.SettingsSlash] = function() base.ShowSettings() 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_Settings_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) buttonControl:GetParent():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.Global.SettingsList:RefreshData()
            base.RestoreFrames()
        end)
    end
    local refreshFrame = GetControl(base.Global.SettingsFrame, "Refresh")
    if refreshFrame then
        base.SetupSettingsFrameHandlers(refreshFrame, "Reload list of 'Collectibles'\nHint: Usefull after gaining a new collectible)", function()
            if base.Global.SettingsList._type ~= nil then
                base.CreateCollection(base.Global.SettingsList._type)
                base.RestoreFrame(base.Global.SettingsList._type)
            end
            base.RestoreCombineLabels()
            base.Global.SettingsList:RefreshData()
        end)
    end

    for controlName, displayName in pairs(base.Global.SettingsFilters) do base.SetupSettingsFilter(base.Global.SettingsFrame, controlName, displayName) end
end

function base.SetupSettingsFilter(control, controlName, displayName)
    local filterControl = GetControl(control, controlName)
    if filterControl then
        base.SetControlText(filterControl, displayName)
        filterControl:SetHandler("OnClicked", function()
            base.Global.SettingsList.masterListType = controlName
            base.Global.SettingsList:RefreshFilters()
        end)
    end
end

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

    show = show or base.Global.SettingsFrame:IsHidden() or _type ~= base.Global.SettingsList._type
    base.Global.SettingsFrame:SetHidden(not show)
    base.UpdateSettingsType(show, _type)
    if show then
        base.Global.SettingsList._type = show and _type or nil
        base.Global.SettingsList:RefreshData()
    end
end

function base.UpdateSettingsType(show, _type)
    if base.Global.SettingsList ~= nil then base.Global.SettingsList._type = _type end

    if base.Global.SettingsLabel ~= nil then base.Global.SettingsLabel:SetText(show and _type and _type.Name or "Global") end
end