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

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

-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local baseModule = MementoBar
local sharedBaseModule = JarthSharedBase

-------------------------------------------------------------------------------------------------
-- Menu Functions --
-------------------------------------------------------------------------------------------------
function baseModule:CreateSettingsWindow()
    local panelData = {
        type = "panel",
        name = baseModule.Addon.DisplayName,
        displayName = baseModule.Addon.DisplayName,
        author = baseModule.Addon.Author,
        version = sharedBaseModule:GetVersion(baseModule, true),
        slashCommand = baseModule.Addon.SlashCommand,
        registerForRefresh = true,
        registerForDefaults = true
    }
    local cntrlOptionsPanel = LAM2:RegisterAddonPanel(baseModule.Addon.Name, panelData)
    local optionsData = {
        [1] = {
            type = "header",
            name = baseModule.Addon.DisplayName .. " Settings"
        },
        [2] = {
            type = "description",
            text = string.format("Here you can setup %s.\nSlash command: %s", baseModule.Addon.DisplayName, baseModule.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 = baseModule.Default.UseAccountSettings,
            getFunc = function()
                return baseModule.Saved.UseAccountSettings
            end,
            setFunc = function(newValue)
                baseModule.Saved.UseAccountSettings = newValue
                if newValue then
                    baseModule.Saved = ZO_SavedVars:NewAccountWide("MementoBar_Account", baseModule.Addon.Version, nil, baseModule.Default)
                else
                    baseModule.Saved = ZO_SavedVars:New("MementoBar_Character", baseModule.Addon.Version, nil, baseModule.Default)
                end
                baseModule.Saved.UseAccountSettings = newValue
                baseModule:RestorePanel(baseModule.Frame)
            end
        },
        [4] = {
            type = "submenu",
            name = baseModule.Global.CategoryName .. " (Total: " .. baseModule.Global.CategoryTotal .. " Unlocked: " .. baseModule.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 = baseModule.Default.BarDepth,
                    getFunc = function()
                        return baseModule.Saved.BarDepth
                    end,
                    setFunc = function(value)
                        baseModule.Saved.BarDepth = value
                        baseModule:RestorePanel(baseModule.Frame)
                    end,
                    min = 1,
                    max = baseModule.Global.CategoryNumUnlocked
                },
                [2] = {
                    type = "description",
                    text = "Choose what memento's you want enabled."
                }
            }
        },
        [5] = {
            type = "submenu",
            name = "Position and size",
            tooltip = "Setup position and size.",
            controls = {
                [1] = {
                    type = "description",
                    text = "Here you can setup position and size \n(Unlock to move the bar)."
                },
                [2] = {
                    type = "checkbox",
                    name = "Unlock movement of bar",
                    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.",
                    default = function()
                        return baseModule.Global.IsMoveEnabled
                    end,
                    getFunc = function()
                        return baseModule.Global.IsMoveEnabled
                    end,
                    setFunc = function(newValue)
                        baseModule.Global.IsMoveEnabled = newValue
                        baseModule:RestorePanel(baseModule.Frame)
                    end
                },
                [3] = {
                    type = "checkbox",
                    name = "Bar orientation horizontal",
                    tooltip = "When ON the bar will orientate horizontally.",
                    default = baseModule.Default.Horizontal,
                    getFunc = function()
                        return baseModule.Saved.Horizontal
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.Horizontal = newValue
                        baseModule:RestorePanel(baseModule.Frame)
                    end
                },
                [4] = {
                    type = "slider",
                    name = "Choose button margin",
                    default = baseModule.Default.Margin,
                    getFunc = function()
                        return baseModule.Saved.Margin
                    end,
                    setFunc = function(value)
                        baseModule.Saved.Margin = value
                        baseModule:RestorePanel(baseModule.Frame)
                    end,
                    min = 1,
                    max = 50
                },
                [5] = {
                    type = "slider",
                    name = "Choose button height",
                    default = baseModule.Default.Height,
                    getFunc = function()
                        return baseModule.Saved.Height
                    end,
                    setFunc = function(value)
                        baseModule.Saved.Height = value
                        baseModule:RestorePanel(baseModule.Frame)
                    end,
                    min = 1,
                    max = 100
                },
                [6] = {
                    type = "slider",
                    name = "Choose button width",
                    default = baseModule.Default.Width,
                    getFunc = function()
                        return baseModule.Saved.Width
                    end,
                    setFunc = function(value)
                        baseModule.Saved.Width = value
                        baseModule:RestorePanel(baseModule.Frame)
                    end,
                    min = 1,
                    max = 100
                },
                [7] = {
                    type = "slider",
                    name = "Choose snap size when moving",
                    default = baseModule.Default.SnapSize,
                    getFunc = function()
                        return baseModule.Saved.SnapSize
                    end,
                    setFunc = function(value)
                        baseModule.Saved.SnapSize = value
                        baseModule:RestorePanel(baseModule.Frame)
                    end,
                    min = 1,
                    max = 10
                },
                [8] = {
                    type = "checkbox",
                    name = "Show hotkey's on bar",
                    tooltip = "When ON the hotkey's will be shown on the bar.",
                    default = baseModule.Default.ShowKeyBinding,
                    getFunc = function()
                        return baseModule.Saved.ShowKeyBinding
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.ShowKeyBinding = newValue
                        baseModule:RestorePanel(baseModule.Frame)
                    end,
                    width = "half"
                },
                [9] = {
                    type = "dropdown",
                    name = "Hotkey label location",
                    tooltip = "Select hotkey label location",
                    choices = baseModule.Global.ChoisesKeyBindingLocation,
                    default = baseModule.Default.KeyBindingLocation,
                    disabled = function()
                        return not baseModule.Saved.ShowKeyBinding
                    end,
                    getFunc = function()
                        return sharedBaseModule:HotKeyGetLocationText(baseModule.Saved.KeyBindingLocation)
                    end,
                    setFunc = function(value)
                        baseModule.Saved.KeyBindingLocation = sharedBaseModule:HotKeyGetLocationValue(value)
                        baseModule:RestorePanel(baseModule.Frame)
                    end,
                    width = "half"
                }
            }
        },
        [6] = {
            type = "submenu",
            name = "Colors",
            tooltip = "Setup button and frame font colors.",
            controls = {
                [1] = {
                    type = "description",
                    text = "Here you can setup the button and frame font colors."
                },
                [2] = {
                    type = "colorpicker",
                    name = "Button background Color",
                    tooltip = "Changes the background color of the buttons.",
                    default = function()
                        return baseModule.Default.CenterColor
                    end,
                    getFunc = function()
                        return baseModule.Saved.CenterColor.r, baseModule.Saved.CenterColor.g, baseModule.Saved.CenterColor.b, baseModule.Saved.CenterColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        baseModule.Saved.CenterColor = {r = r, g = g, b = b, a = a}
                        baseModule:RestorePanel(baseModule.Frame)
                    end,
                    width = "half"
                },
                [3] = {
                    type = "colorpicker",
                    name = "Button edge Color",
                    tooltip = "Changes the edge color of the buttons.",
                    default = function()
                        return baseModule.Default.EdgeColor
                    end,
                    getFunc = function()
                        return baseModule.Saved.EdgeColor.r, baseModule.Saved.EdgeColor.g, baseModule.Saved.EdgeColor.b, baseModule.Saved.EdgeColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        baseModule.Saved.EdgeColor = {r = r, g = g, b = b, a = a}
                        baseModule:RestorePanel(baseModule.Frame)
                    end,
                    width = "half"
                },
                [4] = {
                    type = "colorpicker",
                    name = "Button highlight Color",
                    tooltip = "Changes the highlight color of the buttons.",
                    default = function()
                        return baseModule.Default.HighlightColor
                    end,
                    getFunc = function()
                        return baseModule.Saved.HighlightColor.r, baseModule.Saved.HighlightColor.g, baseModule.Saved.HighlightColor.b, baseModule.Saved.HighlightColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        baseModule.Saved.HighlightColor = {r = r, g = g, b = b, a = a}
                        baseModule:RestorePanel(baseModule.Frame)
                    end,
                    width = "half"
                },
                [5] = {
                    type = "colorpicker",
                    name = "Frame font Color",
                    tooltip = "Changes of the frame font color.",
                    default = baseModule.Default.FontColor,
                    getFunc = function()
                        return baseModule.Saved.FontColor.r, baseModule.Saved.FontColor.g, baseModule.Saved.FontColor.b, baseModule.Saved.FontColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        baseModule.Saved.FontColor = {r = r, g = g, b = b, a = a}
                        baseModule:RestorePanel(baseModule.Frame)
                    end,
                    width = "half"
                }
            }
        },
        [7] = {
            type = "submenu",
            name = "Audio",
            tooltip = "Setup audio.",
            controls = {
                [1] = {
                    type = "description",
                    text = "Here you can setup the audio."
                },
                [2] = {
                    type = "checkbox",
                    name = "Play hover audio",
                    tooltip = "When ON hover events on the bar, will play audio.",
                    default = function()
                        return baseModule.Saved.IsAudioEnabled
                    end,
                    getFunc = function()
                        return baseModule.Saved.IsAudioEnabled
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.IsAudioEnabled = newValue
                    end
                }
            }
        },
        [8] = {
            type = "submenu",
            name = "Visibility",
            tooltip = "Setup visibility",
            controls = {
                [1] = {
                    type = "checkbox",
                    name = "Show bar " .. "on main view/hud",
                    tooltip = "When ON the bar will show the bar " .. "on main view/hud",
                    default = baseModule.Default.ShowBarOnHud,
                    getFunc = function()
                        return baseModule.Saved.ShowBarOnHud
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.ShowBarOnHud = newValue
                        baseModule.Fragment = sharedBaseModule:SetFragmentBehaviour(baseModule.Frame, baseModule.Saved, baseModule.Fragment)
                    end
                },
                [2] = {
                    type = "checkbox",
                    name = "Show bar " .. "on the main view when an overlay is activated/hudui",
                    tooltip = "When ON the bar will show the bar " .. "on the main view when an overlay is activated/hudui",
                    default = baseModule.Default.ShowBarOnHudUI,
                    getFunc = function()
                        return baseModule.Saved.ShowBarOnHudUI
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.ShowBarOnHudUI = newValue
                        baseModule.Fragment = sharedBaseModule:SetFragmentBehaviour(baseModule.Frame, baseModule.Saved, baseModule.Fragment)
                    end
                },
                [3] = {
                    type = "checkbox",
                    name = "Show bar " .. "in menu",
                    tooltip = "When ON the bar will show the bar " .. "in menu",
                    default = baseModule.Default.ShowBarInMenu,
                    getFunc = function()
                        return baseModule.Saved.ShowBarInMenu
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.ShowBarInMenu = newValue
                        baseModule.Fragment = sharedBaseModule:SetFragmentBehaviour(baseModule.Frame, baseModule.Saved, baseModule.Fragment)
                    end
                },
                [4] = {
                    type = "checkbox",
                    name = "Show bar " .. "in the inventory",
                    tooltip = "When ON the bar will show the bar " .. "in the inventory",
                    default = baseModule.Default.ShowBarInInventory,
                    getFunc = function()
                        return baseModule.Saved.ShowBarInInventory
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.ShowBarInInventory = newValue
                        baseModule.Fragment = sharedBaseModule:SetFragmentBehaviour(baseModule.Frame, baseModule.Saved, baseModule.Fragment)
                    end
                },
                [5] = {
                    type = "checkbox",
                    name = "Show bar " .. "in interactions",
                    tooltip = "When ON the bar will show the bar " .. "in interactions",
                    default = baseModule.Default.ShowBarInInteract,
                    getFunc = function()
                        return baseModule.Saved.ShowBarInInteract
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.ShowBarInInteract = newValue
                        baseModule.Fragment = sharedBaseModule:SetFragmentBehaviour(baseModule.Frame, baseModule.Saved, baseModule.Fragment)
                    end
                },
                [6] = {
                    type = "checkbox",
                    name = "Show bar " .. "at a bank",
                    tooltip = "When ON the bar will show the bar " .. "at a bank",
                    default = baseModule.Default.ShowBarInBank,
                    getFunc = function()
                        return baseModule.Saved.ShowBarInBank
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.ShowBarInBank = newValue
                        baseModule.Fragment = sharedBaseModule:SetFragmentBehaviour(baseModule.Frame, baseModule.Saved, baseModule.Fragment)
                    end
                },
                [7] = {
                    type = "checkbox",
                    name = "Show bar " .. "at a fence",
                    tooltip = "When ON the bar will show the bar " .. "at a fence",
                    default = baseModule.Default.ShowBarInFence,
                    getFunc = function()
                        return baseModule.Saved.ShowBarInFence
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.ShowBarInFence = newValue
                        baseModule.Fragment = sharedBaseModule:SetFragmentBehaviour(baseModule.Frame, baseModule.Saved, baseModule.Fragment)
                    end
                },
                [8] = {
                    type = "checkbox",
                    name = "Show bar " .. "at a store",
                    tooltip = "When ON the bar will show the bar " .. "at a store",
                    default = baseModule.Default.ShowBarInStore,
                    getFunc = function()
                        return baseModule.Saved.ShowBarInStore
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.ShowBarInStore = newValue
                        baseModule.Fragment = sharedBaseModule:SetFragmentBehaviour(baseModule.Frame, baseModule.Saved, baseModule.Fragment)
                    end
                }
            }
        },
        [9] = {
            type = "submenu",
            name = "Cooldown",
            tooltip = "Setup cooldown",
            controls = {
                [1] = {
                    type = "description",
                    text = "Here you can setup the cooldown"
                },
                [2] = {
                    type = "checkbox",
                    name = "Display cooldown",
                    tooltip = "When ON cooldown will be displayed.",
                    default = function()
                        return baseModule.Saved.IsTimerEnabled
                    end,
                    getFunc = function()
                        return baseModule.Saved.IsTimerEnabled
                    end,
                    setFunc = function(newValue)
                        baseModule.Saved.IsTimerEnabled = newValue
                        baseModule:RestoreButtons(baseModule.OrderedMementos)
                    end
                },
                [3] = {
                    type = "colorpicker",
                    name = "Cooldown font color",
                    tooltip = "Changes of the cooldown font color.",
                    disabled = function()
                        return not baseModule.Saved.IsTimerEnabled
                    end,
                    default = function()
                        return baseModule.Default.TimerFontColor
                    end,
                    getFunc = function()
                        baseModule:UpdateCooldownExampleText()
                        return baseModule.Saved.TimerFontColor.r, baseModule.Saved.TimerFontColor.g, baseModule.Saved.TimerFontColor.b, baseModule.Saved.TimerFontColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        baseModule.Saved.TimerFontColor = {r = r, g = g, b = b, a = a}
                        baseModule:RestoreButtons(baseModule.OrderedMementos)
                        baseModule:UpdateCooldownExampleText()
                    end,
                    width = "half"
                },
                [4] = {
                    type = "dropdown",
                    name = "Cooldown font",
                    tooltip = "Select the " .. "cooldown font.",
                    choices = baseModule.Global.TimerFonts,
                    disabled = function()
                        return not baseModule.Saved.IsTimerEnabled
                    end,
                    default = baseModule.Default.TimerFont,
                    getFunc = function()
                        baseModule:UpdateCooldownExampleText()
                        return baseModule.Saved.TimerFont
                    end,
                    setFunc = function(value)
                        baseModule.Saved.TimerFont = value
                        baseModule:RestoreButtons(baseModule.OrderedMementos)
                        baseModule:UpdateCooldownExampleText()
                    end,
                    width = "half"
                },
                [5] = {
                    type = "description",
                    title = "Example text: 20.9s\n(Update a value to see an example)",
                    reference = baseModule.Addon.DisplayName .. "ExampleTextControl"
                }
            }
        }
    }

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

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

function baseModule:UpdateCooldownExampleText()
    local control = GetControl(baseModule.Addon.DisplayName .. "ExampleTextControl")
    local fontColor = baseModule.Saved.TimerFontColor

    if control ~= nil and control.desc ~= nil then
        control.desc:SetColor(fontColor.r, fontColor.g, fontColor.b, fontColor.a)
        control.desc:SetFont(baseModule.Saved.TimerFont)
    end
    if control ~= nil and control.title ~= nil then
        control.title:SetColor(fontColor.r, fontColor.g, fontColor.b, fontColor.a)
        control.title:SetFont(baseModule.Saved.TimerFont)
    end
end