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

-------------------------------------------------------------------------------------------------
-- Libraries --
-------------------------------------------------------------------------------------------------
local LAM2 = LibStub:GetLibrary("LibAddonMenu-2.0")

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

-------------------------------------------------------------------------------------------------
-- Menu Functions --
-------------------------------------------------------------------------------------------------
function MementoBar:CreateSettingsWindow()
    local panelData = {
        type = "panel",
        name = MementoBar.Addon.DisplayName,
        displayName = MementoBar.Addon.DisplayName,
        author = MementoBar.Addon.Author,
        version = MementoBar:GetVersion(true),
        slashCommand = MementoBar.Addon.SlashCommand,
        registerForRefresh = true,
        registerForDefaults = true
    }
    local cntrlOptionsPanel = LAM2:RegisterAddonPanel(MementoBar.Addon.Name, panelData)
    local optionsData = {
        [1] = {
            type = "header",
            name = MementoBar.Addon.DisplayName .. " Settings"
        },
        [2] = {
            type = "description",
            text = string.format("Here you can adjust the %s works.\nSlash command: %s", MementoBar.Addon.DisplayName, MementoBar.Addon.SlashCommand)
        },
        [3] = {
            type = "checkbox",
            name = "Use account settings",
            tooltip = "When ON the account settings will be used. When OFF character settings will be used.",
            default = MementoBar.Default.UseAccountSettings,
            getFunc = function()
                return MementoBar.Saved.UseAccountSettings
            end,
            setFunc = function(newValue)
                MementoBar.Saved.UseAccountSettings = newValue
                if newValue then
                    MementoBar.Saved = ZO_SavedVars:NewAccountWide("MementoBar_Account", MementoBar.Addon.Version, nil, MementoBar.Default)
                else
                    MementoBar.Saved = ZO_SavedVars:New("MementoBar_Character", MementoBar.Addon.Version, nil, MementoBar.Default)
                end
                MementoBar.Saved.UseAccountSettings = newValue
                MementoBar:RestorePanel()
            end
        },
        [4] = {
            type = "checkbox",
            name = "Bar orientation horizontal",
            tooltip = "When ON the bar will orientate horizontally.",
            default = MementoBar.Default.Horizontal,
            getFunc = function()
                return MementoBar.Saved.Horizontal
            end,
            setFunc = function(newValue)
                MementoBar.Saved.Horizontal = newValue
                MementoBar:RestorePanel()
            end
        },
        [5] = {
            type = "checkbox",
            name = "Hide bar in menu",
            tooltip = "When ON the bar will hide when a menu is opened.",
            default = MementoBar.Default.HideBarInMenu,
            getFunc = function()
                return MementoBar.Saved.HideBarInMenu
            end,
            setFunc = function(newValue)
                MementoBar.Saved.HideBarInMenu = newValue
                MementoBar:SetFragmentBehaviour(true)
            end
        },
        [6] = {
            type = "submenu",
            name = "Position and size",
            tooltip = "Settings regarding position and size.",
            controls = {
                [1] = {
                    type = "checkbox",
                    name = "Show bar position",
                    tooltip = "When ON the bar will show the X,Y position of the top left corner, when the mouse enters and drags the panel around.",
                    -- disabled = function()
                    --     local count = 0
                    --     if IsCollectibleUnlocked(267) and MementoBar.Saved.ShowAssistants[267] then
                    --         count = count + 1
                    --     end
                    --     if IsCollectibleUnlocked(300) and MementoBar.Saved.ShowAssistants[300] then
                    --         count = count + 1
                    --     end
                    --     if IsCollectibleUnlocked(301) and MementoBar.Saved.ShowAssistants[301] then
                    --         count = count + 1
                    --     end
                    --     return count == 0
                    -- end,
                    default = function()
                        return MementoBar.Global.IsMoveEnabled
                    end,
                    getFunc = function()
                        return MementoBar.Global.IsMoveEnabled
                    end,
                    setFunc = function(newValue)
                        MementoBar.Global.IsMoveEnabled = newValue
                        MementoBar:RestorePanel()
                    end
                },
                [2] = {
                    type = "slider",
                    name = "Choose button margin",
                    default = MementoBar.Default.Margin,
                    getFunc = function()
                        return MementoBar.Saved.Margin
                    end,
                    setFunc = function(value)
                        MementoBar.Saved.Margin = value
                        MementoBar:RestorePanel()
                    end,
                    min = 1,
                    max = 50
                },
                [3] = {
                    type = "slider",
                    name = "Choose button height",
                    default = MementoBar.Default.Height,
                    getFunc = function()
                        return MementoBar.Saved.Height
                    end,
                    setFunc = function(value)
                        MementoBar.Saved.Height = value
                        MementoBar:RestorePanel()
                    end,
                    min = 1,
                    max = 100
                },
                [4] = {
                    type = "slider",
                    name = "Choose button width",
                    default = MementoBar.Default.Width,
                    getFunc = function()
                        return MementoBar.Saved.Width
                    end,
                    setFunc = function(value)
                        MementoBar.Saved.Width = value
                        MementoBar:RestorePanel()
                    end,
                    min = 1,
                    max = 100
                },
                [5] = {
                    type = "slider",
                    name = "Choose snap size when moving",
                    default = MementoBar.Default.SnapSize,
                    getFunc = function()
                        return MementoBar.Saved.SnapSize
                    end,
                    setFunc = function(value)
                        MementoBar.Saved.SnapSize = value
                        MementoBar:RestorePanel()
                    end,
                    min = 1,
                    max = 10
                },
                [6] = {
                    type = "checkbox",
                    name = "Show hotkey's on bar",
                    tooltip = "When ON the bar will hide when a menu is opened.",
                    default = MementoBar.Default.ShowKeyBinding,
                    getFunc = function()
                        return MementoBar.Saved.ShowKeyBinding
                    end,
                    setFunc = function(newValue)
                        MementoBar.Saved.ShowKeyBinding = newValue
                        MementoBar:RestorePanel()
                    end,
                    width = "half"
                },
                [7] = {
                    type = "dropdown",
                    name = "Hotkey label location",
                    tooltip = "Select hotkey label location",
                    choices = MementoBar.Global.ChoisesKeyBindingLocation,
                    default = MementoBar.Default.KeyBindingLocation,
                    disabled = function()
                        return not MementoBar.Saved.ShowKeyBinding
                    end,
                    getFunc = function()
                        return MementoBar:HotKeyGetLocationText(MementoBar.Saved.KeyBindingLocation)
                    end,
                    setFunc = function(value)
                        MementoBar.Saved.KeyBindingLocation = MementoBar:HotKeyGetLocationValue(value)
                        MementoBar:RestorePanel()
                    end,
                    width = "half"
                },
                [8] = {
                    type = "colorpicker",
                    name = "Font Color",
                    default = MementoBar.Default.FontColor,
                    tooltip = "Changes of the move frame font.",
                    getFunc = function()
                        return MementoBar.Saved.FontColor.r, MementoBar.Saved.FontColor.g, MementoBar.Saved.FontColor.b, MementoBar.Saved.FontColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        MementoBar.Saved.FontColor = {r = r, g = g, b = b, a = a}
                        MementoBar:RestorePanel()
                    end,
                    width = "half"
                }
            }
        },
        [7] = {
            type = "submenu",
            name = "Colors",
            tooltip = "Allows you to change colors.",
            controls = {
                [1] = {
                    type = "description",
                    text = "Here you can adjust the colors."
                },
                [2] = {
                    type = "colorpicker",
                    name = "Button background Color",
                    default = function()
                        return MementoBar.Default.CenterColor
                    end,
                    tooltip = "Changes the background color of the buttons.",
                    getFunc = function()
                        return MementoBar.Saved.CenterColor.r, MementoBar.Saved.CenterColor.g, MementoBar.Saved.CenterColor.b, MementoBar.Saved.CenterColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        MementoBar.Saved.CenterColor = {r = r, g = g, b = b, a = a}
                        MementoBar:RestorePanel()
                    end,
                    width = "half"
                },
                [3] = {
                    type = "colorpicker",
                    name = "Button edge Color",
                    default = function()
                        return MementoBar.Default.EdgeColor
                    end,
                    tooltip = "Changes the edgecolor of the buttons.",
                    getFunc = function()
                        return MementoBar.Saved.EdgeColor.r, MementoBar.Saved.EdgeColor.g, MementoBar.Saved.EdgeColor.b, MementoBar.Saved.EdgeColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        MementoBar.Saved.EdgeColor = {r = r, g = g, b = b, a = a}
                        MementoBar:RestorePanel()
                    end,
                    width = "half"
                },
                [4] = {
                    type = "colorpicker",
                    name = "Button highlight Color",
                    default = function()
                        return MementoBar.Default.HighlightColor
                    end,
                    tooltip = "Changes the highlight color of the buttons.",
                    getFunc = function()
                        return MementoBar.Saved.HighlightColor.r, MementoBar.Saved.HighlightColor.g, MementoBar.Saved.HighlightColor.b, MementoBar.Saved.HighlightColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        MementoBar.Saved.HighlightColor = {r = r, g = g, b = b, a = a}
                        MementoBar:RestorePanel()
                    end,
                    width = "half"
                }
            }
        },
        [8] = {
            type = "submenu",
            name = MementoBar.Global.CategoryName .. " (Total: " .. MementoBar.Global.CategoryTotal .. " Unlocked: " .. MementoBar.Global.CategoryNumUnlocked .. ")",
            tooltip = "Choose what memento's you want on the bar.",
            controls = {
                [1] = {
                    type = "slider",
                    name = "Choose bar depth (number of rows/columns)", -- or string id or function returning a string
                    default = MementoBar.Default.BarDepth,
                    getFunc = function()
                        return MementoBar.Saved.BarDepth
                    end,
                    setFunc = function(value)
                        MementoBar.Saved.BarDepth = value
                        MementoBar:RestorePanel()
                    end,
                    min = 1,
                    max = MementoBar.Global.CategoryNumUnlocked
                },
                [2] = {
                    type = "description",
                    text = "Choose what memento's you want enabled."
                }
            }
        }
    }

    for index, _type in ipairs(MementoBar.OrderedMementos) do
        local line = {
            type = "checkbox",
            name = "Show " .. _type.Name,
            tooltip = _type.Tooltip,
            default = not _type.Disabled,
            disabled = _type.Disabled,
            getFunc = function()
                return MementoBar.Saved.SelectedMementos[_type.Id]
            end,
            setFunc = function(newValue)
                if newValue == true then
                    MementoBar.Saved.SelectedMementos[_type.Id] = newValue
                else
                    MementoBar.Saved.SelectedMementos[_type.Id] = nil
                end
                MementoBar:RestorePanel()
            end
        }
        table.insert(optionsData[8].controls, index + 2, line)
    end

    LAM2:RegisterOptionControls(MementoBar.Addon.Name, optionsData)
end