--[[
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 setup %s.\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 = "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."
                }
            }
        },
        [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 MementoBar.Global.IsMoveEnabled
                    end,
                    getFunc = function()
                        return MementoBar.Global.IsMoveEnabled
                    end,
                    setFunc = function(newValue)
                        MementoBar.Global.IsMoveEnabled = newValue
                        MementoBar:RestorePanel()
                    end
                },
                [3] = {
                    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
                },
                [4] = {
                    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
                },
                [5] = {
                    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
                },
                [6] = {
                    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
                },
                [7] = {
                    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
                },
                [8] = {
                    type = "checkbox",
                    name = "Show hotkey's on bar",
                    tooltip = "When ON the hotkey's will be shown on the bar.",
                    default = MementoBar.Default.ShowKeyBinding,
                    getFunc = function()
                        return MementoBar.Saved.ShowKeyBinding
                    end,
                    setFunc = function(newValue)
                        MementoBar.Saved.ShowKeyBinding = newValue
                        MementoBar:RestorePanel()
                    end,
                    width = "half"
                },
                [9] = {
                    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"
                }
            }
        },
        [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 MementoBar.Default.CenterColor
                    end,
                    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",
                    tooltip = "Changes the edge color of the buttons.",
                    default = function()
                        return MementoBar.Default.EdgeColor
                    end,
                    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",
                    tooltip = "Changes the highlight color of the buttons.",
                    default = function()
                        return MementoBar.Default.HighlightColor
                    end,
                    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"
                },
                [5] = {
                    type = "colorpicker",
                    name = "Frame font Color",
                    tooltip = "Changes of the frame font color.",
                    default = MementoBar.Default.FontColor,
                    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 = "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 MementoBar.Saved.IsAudioEnabled
                    end,
                    getFunc = function()
                        return MementoBar.Saved.IsAudioEnabled
                    end,
                    setFunc = function(newValue)
                        MementoBar.Saved.IsAudioEnabled = newValue
                    end
                }
            }
        },
        [8] = {
            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 MementoBar.Saved.IsTimerEnabled
                    end,
                    getFunc = function()
                        return MementoBar.Saved.IsTimerEnabled
                    end,
                    setFunc = function(newValue)
                        MementoBar.Saved.IsTimerEnabled = newValue
                        MementoBar:RestoreButtons()
                    end
                },
                [3] = {
                    type = "colorpicker",
                    name = "Cooldown font color",
                    tooltip = "Changes of the cooldown font color.",
                    disabled = function()
                        return not MementoBar.Saved.IsTimerEnabled
                    end,
                    default = function()
                        return MementoBar.Default.TimerFontColor
                    end,
                    getFunc = function()
                        MementoBar:UpdateCooldownExampleText()
                        return MementoBar.Saved.TimerFontColor.r, MementoBar.Saved.TimerFontColor.g, MementoBar.Saved.TimerFontColor.b, MementoBar.Saved.TimerFontColor.a
                    end,
                    setFunc = function(r, g, b, a)
                        MementoBar.Saved.TimerFontColor = {r = r, g = g, b = b, a = a}
                        MementoBar:RestoreButtons()
                        MementoBar:UpdateCooldownExampleText()
                    end,
                    width = "half"
                },
                [4] = {
                    type = "dropdown",
                    name = "Cooldown font",
                    tooltip = "Select the " .. "cooldown font.",
                    choices = MementoBar.Global.TimerFonts,
                    disabled = function()
                        return not MementoBar.Saved.IsTimerEnabled
                    end,
                    default = MementoBar.Default.TimerFont,
                    getFunc = function()
                        MementoBar:UpdateCooldownExampleText()
                        return MementoBar.Saved.TimerFont
                    end,
                    setFunc = function(value)
                        MementoBar.Saved.TimerFont = value
                        MementoBar:RestoreButtons()
                        MementoBar:UpdateCooldownExampleText()
                    end,
                    width = "half"
                },
                [5] = {
                    type = "description",
                    title = "Example text: 20.9s\n(Update a value to see an example)",
                    reference = MementoBar.Addon.DisplayName .. "ExampleTextControl"
                }
            }
        },
        [9] = {
            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 = MementoBar.Default.ShowBarOnHud,
                    getFunc = function()
                        return MementoBar.Saved.ShowBarOnHud
                    end,
                    setFunc = function(newValue)
                        MementoBar.Saved.ShowBarOnHud = newValue
                        MementoBar:SetFragmentBehaviour()
                    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 = MementoBar.Default.ShowBarOnHudUI,
                    getFunc = function()
                        return MementoBar.Saved.ShowBarOnHudUI
                    end,
                    setFunc = function(newValue)
                        MementoBar.Saved.ShowBarOnHudUI = newValue
                        MementoBar:SetFragmentBehaviour()
                    end
                },
                [3] = {
                    type = "checkbox",
                    name = "Show bar " .. "in menu",
                    tooltip = "When ON the bar will show the bar " .. "in menu",
                    default = MementoBar.Default.ShowBarInMenu,
                    getFunc = function()
                        return MementoBar.Saved.ShowBarInMenu
                    end,
                    setFunc = function(newValue)
                        MementoBar.Saved.ShowBarInMenu = newValue
                        MementoBar:SetFragmentBehaviour()
                    end
                },
                [4] = {
                    type = "checkbox",
                    name = "Show bar " .. "in the inventory",
                    tooltip = "When ON the bar will show the bar " .. "in the inventory",
                    default = MementoBar.Default.ShowBarInInventory,
                    getFunc = function()
                        return MementoBar.Saved.ShowBarInInventory
                    end,
                    setFunc = function(newValue)
                        MementoBar.Saved.ShowBarInInventory = newValue
                        MementoBar:SetFragmentBehaviour()
                    end
                },
                [5] = {
                    type = "checkbox",
                    name = "Show bar " .. "in a store",
                    tooltip = "When ON the bar will show the bar " .. "in a store",
                    default = MementoBar.Default.ShowBarInStore,
                    getFunc = function()
                        return MementoBar.Saved.ShowBarInStore
                    end,
                    setFunc = function(newValue)
                        MementoBar.Saved.ShowBarInStore = newValue
                        MementoBar:SetFragmentBehaviour()
                    end
                }
            }
        }
    }

    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[4].controls, index + 2, line)
    end

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

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

    if control ~= nil and control.desc ~= nil then
        control.desc:SetColor(fontColor.r, fontColor.g, fontColor.b, fontColor.a)
        control.desc:SetFont(MementoBar.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(MementoBar.Saved.TimerFont)
    end
end