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

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

-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local SummonAssistant = SummonAssistant

-------------------------------------------------------------------------------------------------
-- Menu Functions --
-------------------------------------------------------------------------------------------------
function SummonAssistant:CreateSettingsWindow()
    local panelData = {
        type = "panel",
        name = SummonAssistant.Addon.DisplayName,
        displayName = SummonAssistant.Addon.DisplayName,
        author = SummonAssistant.Addon.Author,
        version = SummonAssistant:GetVersion(true),
        slashCommand = SummonAssistant.Addon.SlashCommand,
        registerForRefresh = true,
        registerForDefaults = true
    }
    local cntrlOptionsPanel = LAM2:RegisterAddonPanel(SummonAssistant.Addon.Name, panelData)
    local optionsData = {
        [1] = {
            type = "header",
            name = SummonAssistant.Addon.DisplayName .. " Settings"
        },
        [2] = {
            type = "description",
            text = string.format("Here you can adjust the %s works.\nSlash command: %s", SummonAssistant.Addon.DisplayName, SummonAssistant.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 = SummonAssistant.Default.UseAccountSettings,
            getFunc = function()
                return SummonAssistant.Saved.UseAccountSettings
            end,
            setFunc = function(newValue)
                SummonAssistant.Saved.UseAccountSettings = newValue
                if newValue then
                    SummonAssistant.Saved = ZO_SavedVars:NewAccountWide("SummonAssistant_Account", SummonAssistant.Addon.Version, nil, SummonAssistant.Default)
                else
                    SummonAssistant.Saved = ZO_SavedVars:New("SummonAssistant_Character", SummonAssistant.Addon.Version, nil, SummonAssistant.Default)
                end
                SummonAssistant.Saved.UseAccountSettings = newValue
                SummonAssistant:RestorePanel()
            end
        },
        [4] = {
            type = "checkbox",
            name = "Bar orientation horizontal",
            tooltip = "When ON the bar will orientate horizontally.",
            default = SummonAssistant.Default.Horizontal,
            getFunc = function()
                return SummonAssistant.Saved.Horizontal
            end,
            setFunc = function(newValue)
                SummonAssistant.Saved.Horizontal = newValue
                SummonAssistant:RestorePanel()
            end
        },
        [5] = {
            type = "checkbox",
            name = "Hide bar in menu",
            tooltip = "When ON the bar will hide when a menu is opened.",
            default = SummonAssistant.Default.HideBarInMenu,
            getFunc = function()
                return SummonAssistant.Saved.HideBarInMenu
            end,
            setFunc = function(newValue)
                SummonAssistant.Saved.HideBarInMenu = newValue
                SummonAssistant: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 SummonAssistant.Saved.ShowAssistants[267] then
                            count = count + 1
                        end
                        if IsCollectibleUnlocked(300) and SummonAssistant.Saved.ShowAssistants[300] then
                            count = count + 1
                        end
                        if IsCollectibleUnlocked(301) and SummonAssistant.Saved.ShowAssistants[301] then
                            count = count + 1
                        end
                        return count == 0
                    end,
                    default = function()
                        return SummonAssistant.Global.IsMoveEnabled
                    end,
                    getFunc = function()
                        return SummonAssistant.Global.IsMoveEnabled
                    end,
                    setFunc = function(newValue)
                        SummonAssistant.Global.IsMoveEnabled = newValue
                        SummonAssistant:RestorePanel()
                    end
                },
                [2] = {
                    type = "slider",
                    name = "Choose button margin",
                    tooltip = "It is at this time, 'not possible' to move/drag the frame, when the margin is set to 0",
                    default = SummonAssistant.Default.Margin,
                    getFunc = function()
                        return SummonAssistant.Saved.Margin
                    end,
                    setFunc = function(value)
                        SummonAssistant.Saved.Margin = value
                        SummonAssistant:RestorePanel()
                    end,
                    min = 1,
                    max = 50
                },
                [3] = {
                    type = "slider",
                    name = "Choose button height",
                    default = SummonAssistant.Default.Height,
                    getFunc = function()
                        return SummonAssistant.Saved.Height
                    end,
                    setFunc = function(value)
                        SummonAssistant.Saved.Height = value
                        SummonAssistant:RestorePanel()
                    end,
                    min = 1,
                    max = 100
                },
                [4] = {
                    type = "slider",
                    name = "Choose button width",
                    default = SummonAssistant.Default.Width,
                    getFunc = function()
                        return SummonAssistant.Saved.Width
                    end,
                    setFunc = function(value)
                        SummonAssistant.Saved.Width = value
                        SummonAssistant:RestorePanel()
                    end,
                    min = 1,
                    max = 100
                },
                [5] = {
                    type = "slider",
                    name = "Choose snap size when moving",
                    default = SummonAssistant.Default.SnapSize,
                    getFunc = function()
                        return SummonAssistant.Saved.SnapSize
                    end,
                    setFunc = function(value)
                        SummonAssistant.Saved.SnapSize = value
                        SummonAssistant:RestorePanel()
                    end,
                    min = 1,
                    max = 10
                },
                [6] = {
                    type = "checkbox",
                    name = "Show hotkey's on bar",
                    tooltip = "When ON the hotkey's will be shown on the bar.",
                    default = SummonAssistant.Default.ShowKeyBinding,
                    getFunc = function()
                        return SummonAssistant.Saved.ShowKeyBinding
                    end,
                    setFunc = function(newValue)
                        SummonAssistant.Saved.ShowKeyBinding = newValue
                        SummonAssistant:RestorePanel()
                    end,
                    width = "half"
                },
                [7] = {
                    type = "dropdown",
                    name = "Hotkey label location",
                    tooltip = "Select hotkey label location",
                    choices = SummonAssistant.Global.ChoisesKeyBindingLocation,
                    default = SummonAssistant.Default.KeyBindingLocation,
                    disabled = function()
                        return not SummonAssistant.Saved.ShowKeyBinding
                    end,
                    getFunc = function()
                        return SummonAssistant:HotKeyGetLocationText(SummonAssistant.Saved.KeyBindingLocation)
                    end,
                    setFunc = function(value)
                        SummonAssistant.Saved.KeyBindingLocation = SummonAssistant:HotKeyGetLocationValue(value)
                        SummonAssistant:RestorePanel()
                    end,
                    width = "half"
                },
                [8] = {
                    type = "colorpicker",
                    name = "Font Color",
                    default = SummonAssistant.Default.FontColor,
                    tooltip = "Changes of the bar font color.",
                    getFunc = function()
                        return SummonAssistant.Saved.FontColor.r, SummonAssistant.Saved.FontColor.g, SummonAssistant.Saved.FontColor.b, SummonAssistant.Saved.FontColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        SummonAssistant.Saved.FontColor = {r = r, g = g, b = b, a = a}
                        SummonAssistant: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 SummonAssistant.Default.CenterColor
                    end,
                    tooltip = "Changes the background color of the buttons.",
                    getFunc = function()
                        return SummonAssistant.Saved.CenterColor.r, SummonAssistant.Saved.CenterColor.g, SummonAssistant.Saved.CenterColor.b, SummonAssistant.Saved.CenterColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        SummonAssistant.Saved.CenterColor = {r = r, g = g, b = b, a = a}
                        SummonAssistant:RestorePanel()
                    end,
                    width = "half"
                },
                [3] = {
                    type = "colorpicker",
                    name = "Button edge Color",
                    default = function()
                        return SummonAssistant.Default.EdgeColor
                    end,
                    tooltip = "Changes the edgecolor of the buttons.",
                    getFunc = function()
                        return SummonAssistant.Saved.EdgeColor.r, SummonAssistant.Saved.EdgeColor.g, SummonAssistant.Saved.EdgeColor.b, SummonAssistant.Saved.EdgeColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        SummonAssistant.Saved.EdgeColor = {r = r, g = g, b = b, a = a}
                        SummonAssistant:RestorePanel()
                    end,
                    width = "half"
                },
                [4] = {
                    type = "colorpicker",
                    name = "Button highlight Color",
                    default = function()
                        return SummonAssistant.Default.HighlightColor
                    end,
                    tooltip = "Changes the highlight color of the buttons.",
                    getFunc = function()
                        return SummonAssistant.Saved.HighlightColor.r, SummonAssistant.Saved.HighlightColor.g, SummonAssistant.Saved.HighlightColor.b, SummonAssistant.Saved.HighlightColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        SummonAssistant.Saved.HighlightColor = {r = r, g = g, b = b, a = a}
                        SummonAssistant:RestorePanel()
                    end,
                    width = "half"
                }
            }
        },
        [8] = {
            type = "submenu",
            name = "Visibility",
            tooltip = "Allows you to choose what assistant buttons you wants to see.",
            controls = {
                [1] = {
                    type = "description",
                    text = "Select assistant buttons you want enabled."
                }
            }
        }
    }

    for index, _type in ipairs(SummonAssistant.OrderedTypes) do
        local line = {
            type = "checkbox",
            name = "Show " .. _type.name,
            tooltip = _type.Tooltip,
            default = not _type.Disabled,
            disabled = _type.Disabled,
            getFunc = function()
                return SummonAssistant.Saved.ShowAssistants[_type.Id]
            end,
            setFunc = function(newValue)
                SummonAssistant.Saved.ShowAssistants[_type.Id] = newValue
                SummonAssistant:RestorePanel()
            end
        }
        table.insert(optionsData[8].controls, index + 1, line)
    end
    LAM2:RegisterOptionControls(SummonAssistant.Addon.Name, optionsData)
end