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

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

-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local baseModule = SummonAssistant
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("SummonAssistant_Account", baseModule.Addon.Version, nil, baseModule.Default)
                else
                    baseModule.Saved = ZO_SavedVars:New("SummonAssistant_Character", baseModule.Addon.Version, nil, baseModule.Default)
                end
                baseModule.Saved.UseAccountSettings = newValue
                baseModule:RestorePanel(baseModule.Frame)
            end
        },
        [4] = {
            type = "submenu",
            name = "Assistants",
            tooltip = "Allows you to choose what assistant buttons you wants to see.",
            controls = {
                [1] = {
                    type = "description",
                    text = "Choose what assistant buttons you want to see."
                },
                [2] = {
                    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 = 3 -- TODO: Add calculation for max assistants
                }
            }
        },
        [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 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.",
                    disabled = function()
                        local count = 0
                        if IsCollectibleUnlocked(267) and baseModule.Saved.ShowAssistants[267] then
                            count = count + 1
                        end
                        if IsCollectibleUnlocked(300) and baseModule.Saved.ShowAssistants[300] then
                            count = count + 1
                        end
                        if IsCollectibleUnlocked(301) and baseModule.Saved.ShowAssistants[301] then
                            count = count + 1
                        end
                        return count == 0
                    end,
                    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",
                    tooltip = "It is at this time, 'not possible' to move/drag the frame, when the margin is set to 0",
                    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 edgecolor 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",
                    default = baseModule.Default.FontColor,
                    tooltip = "Changes of the frame font color.",
                    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.Global.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
                }
            }
        }
    }

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

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