--[[
Author: Jarth
Filename: CBs_Menu.lua
]] --
-------------------------------------------------------------------------------------------------
-- Libraries --
-------------------------------------------------------------------------------------------------
local LAM2 = LibAddonMenu2

-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local base = CollectionBars

-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-------------------------------------------------------------------------------------------------
function base.CreateSettingsWindow()
    LAM2:RegisterAddonPanel(base.Addon.Name, {
        _type = "panel",
        name = base.Addon.DisplayName,
        displayName = base.Addon.DisplayName,
        author = base.Addon.Author,
        version = base.GetVersion(true),
        slashCommand = base.Addon.Command,
        registerForRefresh = true,
        registerForDefaults = true
    })

    local whenOnTheBar = "When ON the bar will show the bar "
    local disabledWhenTooltipIsHidden = "Disabled when tooltip is not shown"
    local disabledWhenLabelIsHidden = "Disabled when label is not shown"
    local disabledWhenCombined = "Disabled when Collection is included in combine bar"

    local optionsData = {
        {type = "header", name = base.Addon.DisplayName .. " Settings"}, {type = "description", text = string.format("Here you can setup %s.\nSlash command: %s\nCollection bars settings: %s", base.Addon.DisplayName, base.Addon.Command, base.Addon.SettingsSlash)}, {
            type = "checkbox",
            name = "Use account settings",
            tooltip = "When ON the account settings will be used. When OFF character settings will be used",
            default = base.Default.UseAccountSettings,
            warning = "Reload to see full effect",
            requiresReload = true,
            getFunc = function()
                return base.Saved.UseAccountSettings
            end,
            setFunc = function(newValue)
                base.SetAndUpdateAccountSettings(newValue)
            end,
            width = "half"
        }, {
            type = "button",
            name = "Toggle settings",
            tooltip = "When ON all enabled collection types will display a label\nOpen a settings panel for each collection type, and select what collectibles you want to enable.",
            func = function()
                base.ToggleEnableSettings()
            end,
            width = "half"
        }, {
            type = "submenu",
            name = "General settings",
            tooltip = "Setup some general settings",
            controls = {
                {type = "description", text = "Here you can setup position and size"}, {
                    type = "checkbox",
                    name = "Unlock movement of bar",
                    tooltip = "When ON the bar will show the X,Y position of the top left corner, and is draggable",
                    default = function()
                        return base.Global.IsMoveEnabled
                    end,
                    getFunc = function()
                        return base.Global.IsMoveEnabled
                    end,
                    setFunc = function(newValue)
                        base.Global.IsMoveEnabled = newValue
                        base.RestoreFrames()
                    end
                }, {
                    type = "slider",
                    name = "Choose snap size when moving",
                    default = base.Default.SnapSize,
                    getFunc = function()
                        return base.Saved.SnapSize
                    end,
                    setFunc = function(value)
                        base.Saved.SnapSize = value
                        base.RestoreFrames()
                    end,
                    min = 1,
                    max = 10
                }, {
                    type = "slider",
                    name = "Choose default bar depth (number of rows/columns)",
                    default = base.Default.BarDepth,
                    getFunc = function()
                        return base.Saved.BarDepth
                    end,
                    setFunc = function(value)
                        base.Saved.BarDepth = value
                        base.RestoreFrames()
                    end,
                    min = 1,
                    max = base.Global.HighestUnlocked
                }, {
                    type = "slider",
                    name = "Choose max bar height (number of inverse rows/columns)",
                    default = base.Default.BarWidth,
                    getFunc = function()
                        return base.Saved.BarWidth
                    end,
                    setFunc = function(value)
                        base.Saved.BarWidth = value
                        base.RestoreFrames()
                    end,
                    min = 0,
                    max = base.Global.HighestUnlocked
                }, {
                    type = "checkbox",
                    name = "Show binding's on bar",
                    tooltip = "When ON the binding's will be shown on the bar",
                    default = base.Default.ShowBinding,
                    getFunc = function()
                        return base.Saved.ShowBinding
                    end,
                    setFunc = function(newValue)
                        base.Saved.ShowBinding = newValue
                        base.RestoreFrames()
                    end,
                    width = "half"
                }, {
                    type = "slider",
                    name = "Button size",
                    default = ZO_GAMEPAD_ACTION_BUTTON_SIZE,
                    getFunc = function()
                        return base.Saved.ButtonXY
                    end,
                    setFunc = function(value)
                        base.Saved.ButtonXY = value
                        base.RestoreFrames()
                    end,
                    min = 1,
                    max = 100,
                    width = "half"
                }, {type = "divider", width = "full"}, {type = "description", text = "Here you can setup what views/scenes the bars are visible in"}, {
                    type = "checkbox",
                    name = "Show bar " .. "on main view/hud",
                    tooltip = whenOnTheBar .. "on main view/hud",
                    default = base.Default.ShowBarOnHud,
                    getFunc = function()
                        return base.Saved.ShowBarOnHud
                    end,
                    setFunc = function(newValue)
                        base.Saved.ShowBarOnHud = newValue
                        base.UpdateFragments("ShowBarOnHud")
                    end
                }, {
                    type = "checkbox",
                    name = "Show bar " .. "on the main view when an overlay is activated/hudui",
                    tooltip = whenOnTheBar .. "on the main view when an overlay is activated/hudui",
                    default = base.Default.ShowBarOnHudUI,
                    getFunc = function()
                        return base.Saved.ShowBarOnHudUI
                    end,
                    setFunc = function(newValue)
                        base.Saved.ShowBarOnHudUI = newValue
                        base.UpdateFragments("ShowBarOnHudUI")
                    end
                }, {
                    type = "checkbox",
                    name = "Show bar " .. "in menu",
                    tooltip = whenOnTheBar .. "in menu",
                    default = base.Default.ShowBarInMenu,
                    getFunc = function()
                        return base.Saved.ShowBarInMenu
                    end,
                    setFunc = function(newValue)
                        base.Saved.ShowBarInMenu = newValue
                        base.UpdateFragments("ShowBarInMenu")
                    end
                }, {
                    type = "checkbox",
                    name = "Show bar " .. "in the inventory",
                    tooltip = whenOnTheBar .. "in the inventory",
                    default = base.Default.ShowBarInInventory,
                    getFunc = function()
                        return base.Saved.ShowBarInInventory
                    end,
                    setFunc = function(newValue)
                        base.Saved.ShowBarInInventory = newValue
                        base.UpdateFragments("ShowBarInInventory")
                    end
                }, {
                    type = "checkbox",
                    name = "Show bar " .. "in interactions",
                    tooltip = whenOnTheBar .. "in interactions",
                    default = base.Default.ShowBarInInteract,
                    getFunc = function()
                        return base.Saved.ShowBarInInteract
                    end,
                    setFunc = function(newValue)
                        base.Saved.ShowBarInInteract = newValue
                        base.UpdateFragments("ShowBarInInteract")
                    end
                }, {
                    type = "checkbox",
                    name = "Show bar " .. "at a bank",
                    tooltip = whenOnTheBar .. "at a bank",
                    default = base.Default.ShowBarInBank,
                    getFunc = function()
                        return base.Saved.ShowBarInBank
                    end,
                    setFunc = function(newValue)
                        base.Saved.ShowBarInBank = newValue
                        base.UpdateFragments("ShowBarInBank")
                    end
                }, {
                    type = "checkbox",
                    name = "Show bar " .. "at a fence",
                    tooltip = whenOnTheBar .. "at a fence",
                    default = base.Default.ShowBarInFence,
                    getFunc = function()
                        return base.Saved.ShowBarInFence
                    end,
                    setFunc = function(newValue)
                        base.Saved.ShowBarInFence = newValue
                        base.UpdateFragments("ShowBarInFence")
                    end
                }, {
                    type = "checkbox",
                    name = "Show bar " .. "at a store",
                    tooltip = whenOnTheBar .. "at a store",
                    default = base.Default.ShowBarInStore,
                    getFunc = function()
                        return base.Saved.ShowBarInStore
                    end,
                    setFunc = function(newValue)
                        base.Saved.ShowBarInStore = newValue
                        base.UpdateFragments("ShowBarInStore")
                    end
                }, {type = "divider", width = "full"}, {type = "description", text = "Here you can setup the active and activation indication"}, {
                    type = "checkbox",
                    name = "Show active and activation",
                    tooltip = "When ON the active collectibles will be highlighted, and activation animation will display",
                    default = function()
                        return base.Saved.IsActiveActivationEnabled
                    end,
                    getFunc = function()
                        return base.Saved.IsActiveActivationEnabled
                    end,
                    setFunc = function(newValue)
                        base.Saved.IsActiveActivationEnabled = newValue
                        base.RestoreFrames()
                    end
                }, {type = "divider", width = "full"}, {type = "description", text = "Here you can setup the audio"}, {
                    type = "checkbox",
                    name = "Play hover audio",
                    tooltip = "When ON hover events on the bar, will play audio",
                    default = function()
                        return base.Saved.IsAudioEnabled
                    end,
                    getFunc = function()
                        return base.Saved.IsAudioEnabled
                    end,
                    setFunc = function(newValue)
                        base.Saved.IsAudioEnabled = newValue
                        base.RestoreFrames()
                    end
                }
            }
        }, {
            type = "submenu",
            name = "Combined bar",
            tooltip = "Setup the combined bar",
            controls = {
                {type = "description", text = "Here you can setup the display name and labels"}, {
                    type = "slider",
                    name = "Display name offset horizontal (X)",
                    default = base.Default.Combine.Label.OffsetX,
                    getFunc = function()
                        return base.Saved.Combine.Label.OffsetX
                    end,
                    setFunc = function(value)
                        base.Saved.Combine.Label.OffsetX = value
                        base.RestoreCombineLabels()
                    end,
                    min = -500,
                    max = 500,
                    width = "half"
                }, {
                    type = "slider",
                    name = "Display name offset vertical (Y)",
                    default = base.Default.Combine.Label.OffsetY,
                    getFunc = function()
                        return base.Saved.Combine.Label.OffsetY
                    end,
                    setFunc = function(value)
                        base.Saved.Combine.Label.OffsetY = value
                        base.RestoreCombineLabels()
                    end,
                    min = -500,
                    max = 500,
                    width = "half"
                }, {
                    type = "dropdown",
                    name = "Display name anchor position on button",
                    tooltip = "Select display name anchor position on the button",
                    choices = base.Global.ChoiceCornerLocations,
                    default = base.Default.Combine.Label.PositionTarget,
                    getFunc = function()
                        return base.GetLocationText(base.Saved.Combine.Label.PositionTarget)
                    end,
                    setFunc = function(value)
                        base.Saved.Combine.Label.PositionTarget = base.GetLocationValue(value)
                        base.Saved.Combine.AnchorXY = base.GetLocationValue(value)
                        base.RestoreCombineLabels()
                    end,
                    width = "half"
                }, {
                    type = "dropdown",
                    name = "Display name anchor position on label",
                    tooltip = "Select display name anchor position on the label",
                    choices = base.Global.ChoiceLocations,
                    default = base.Default.Combine.Label.Position,
                    getFunc = function()
                        return base.GetLocationText(base.Saved.Combine.Label.Position)
                    end,
                    setFunc = function(value)
                        base.Saved.Combine.Label.Position = base.GetLocationValue(value)
                        base.RestoreCombineLabels()
                    end,
                    width = "half"
                }, {
                    type = "dropdown",
                    name = "Bar anchor location",
                    tooltip = "Select Bar anchor location, used when bar is collapsed",
                    choices = base.Global.ChoiceCornerLocations,
                    default = base.Default.Combine.AnchorXY,
                    getFunc = function()
                        return base.GetLocationText(base.Saved.Combine.AnchorXY)
                    end,
                    setFunc = function(value)
                        base.Saved.Combine.AnchorXY = base.GetLocationValue(value)
                        base.RestoreCombineLabels()
                    end,
                    width = "half"
                }, {type = "divider", width = "full"}, {type = "description", text = "Here you can setup position and size"}, {
                    type = "slider",
                    name = "Choose default bar depth (number of rows/columns)",
                    default = base.Default.BarDepth,
                    getFunc = function()
                        return base.Saved.Combine.BarDepth
                    end,
                    setFunc = function(value)
                        base.Saved.Combine.BarDepth = value
                        base.RestoreFrames()
                    end,
                    min = 0,
                    max = base.Global.HighestUnlocked
                }, {
                    type = "slider",
                    name = "Choose max bar height (number of inverse rows/columns)",
                    default = base.Default.BarWidth,
                    getFunc = function()
                        return base.Saved.Combine.BarWidth
                    end,
                    setFunc = function(value)
                        base.Saved.Combine.BarWidth = value
                        base.RestoreFrames()
                    end,
                    min = 0,
                    max = base.Global.HighestUnlocked
                }
            }
        }, {
            type = "submenu",
            name = "Collection types",
            tooltip = "Select what collection types you want enabled",
            controls = {{type = "description", text = "The selected collection types will be displayed when ON\nReload to see effect in menu"}}
        }
    }

    local function AppendCollectionType(controlIndex, _type)
        table.insert(optionsData[controlIndex].controls, {
            type = "checkbox",
            name = string.format("Show %s", _type.Name),
            tooltip = "When ON the menu item " .. _type.Name .. " will be available for selection",
            default = base.Default[_type.Name].Enabled,
            getFunc = function()
                return _type.Saved.Enabled
            end,
            setFunc = function(newValue)
                _type.Saved.Enabled = newValue
                if newValue then
                    base.InitializeType(_type)
                else
                    base.RemoveLabel(_type)
                    base.RemoveFrame(_type)
                end
                if _type.Saved.IsCombined then
                    base.RestoreCombineLabels()
                end
            end
        })
    end

    local function AppendCollectionMenuItem(controlIndex, _type)
        local typeName = _type.Name
        local controls = {
            {
                type = "checkbox",
                name = "Include " .. typeName .. " in combine bar",
                tooltip = "When ON " .. typeName .. " will be attached to a combined bar",
                default = base.Default[typeName].Combine,
                getFunc = function()
                    return _type.Saved.IsCombined
                end,
                setFunc = function(newValue)
                    _type.Saved.IsCombined = newValue
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end
            }, {type = "divider", width = "full"}, {type = "description", text = "Here you can setup tooltips"}, {
                type = "checkbox",
                name = "Show tooltip",
                tooltip = "When ON tooltips will be shown, when hovering buttons on the bar",
                default = base.Default[typeName].Tooltip.Show,
                getFunc = function()
                    return _type.Saved.Tooltip.Show
                end,
                setFunc = function(newValue)
                    _type.Saved.Tooltip.Show = newValue
                    base.SetupButtons(_type)
                end,
                width = "half"
            }, {
                type = "dropdown",
                name = "Show tooltip anchor position",
                tooltip = "Select tooltip anchor position\n" .. disabledWhenTooltipIsHidden,
                choices = base.Global.ChoiceSideLocations,
                default = base.Default[typeName].Tooltip.Position,
                disabled = function()
                    return not _type.Saved.Tooltip.Show
                end,
                getFunc = function()
                    return base.GetLocationText(_type.Saved.Tooltip.Position)
                end,
                setFunc = function(value)
                    _type.Saved.Tooltip.Position = base.GetLocationValue(value)
                    base.SetupButtons(_type)
                end,
                width = "half"
            }, {type = "divider", width = "full"}, {type = "description", text = "Here you can setup the name and labels"}, {
                type = "checkbox",
                name = "Enable hide/toggle visibility of the label",
                tooltip = "When enabled a +/- at the end of the label indicates if the label is hidden or shown\n" .. disabledWhenCombined,
                disabled = function()
                    return _type.Saved.IsCombined
                end,
                default = base.Default[typeName].HideAllEnabled,
                getFunc = function()
                    return _type.Saved.IsCombined or _type.Saved.HideAllEnabled
                end,
                setFunc = function(newValue)
                    if (not newValue) then
                        _type.Saved.HideAll = newValue
                        base.RestoreFrame(_type)
                    end
                    _type.Saved.HideAllEnabled = newValue
                    _type.Saved.LabelShow = true
                    base.SetupLabel(_type)
                    base.RestoreLabel(_type)
                end,
                width = "half"
            }, {
                type = "checkbox",
                name = "Show label",
                tooltip = disabledWhenCombined .. " or hide/toggle visibility is enabled",
                disabled = function()
                    return _type.Saved.HideAllEnabled or _type.Saved.IsCombined
                end,
                default = base.Default[typeName].LabelShow,
                getFunc = function()
                    return _type.Saved.LabelShow or _type.Saved.IsCombined
                end,
                setFunc = function(newValue)
                    _type.Saved.LabelShow = newValue
                    base.SetupLabel(_type)
                    base.RestoreLabel(_type)
                end,
                width = "half"
            }, {
                type = "editbox",
                name = "Display name",
                tooltip = disabledWhenLabelIsHidden,
                disabled = function()
                    return not _type.Saved.LabelShow
                end,
                default = base.Default[typeName].Display,
                getFunc = function()
                    return tostring(_type.Saved.Display)
                end,
                setFunc = function(value)
                    _type.Saved.Display = value
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end,
                width = "half"
            }, {
                type = "dropdown",
                name = "Display name font",
                tooltip = disabledWhenLabelIsHidden,
                disabled = function()
                    return not _type.Saved.LabelShow
                end,
                choices = base.Global.AvailableFonts,
                default = base.Default[typeName].Label.Font,
                getFunc = function()
                    return _type.Saved.Label.Font
                end,
                setFunc = function(value)
                    _type.Saved.Label.Font = value
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end,
                width = "half"
            }, {
                type = "slider",
                name = "Display name height",
                tooltip = disabledWhenLabelIsHidden,
                disabled = function()
                    return not _type.Saved.LabelShow
                end,
                default = base.Default[typeName].Label.Height,
                getFunc = function()
                    return _type.Saved.Label.Height
                end,
                setFunc = function(value)
                    _type.Saved.Label.Height = value
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end,
                min = 0,
                max = 500,
                width = "half"
            }, {
                type = "slider",
                name = "Display name width",
                tooltip = disabledWhenLabelIsHidden,
                disabled = function()
                    return not _type.Saved.LabelShow
                end,
                default = base.Default[typeName].Label.Width,
                getFunc = function()
                    return _type.Saved.Label.Width
                end,
                setFunc = function(value)
                    _type.Saved.Label.Width = value
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end,
                min = 0,
                max = 500,
                width = "half"
            }, {
                type = "slider",
                name = "Display name offset horizontal (X)",
                tooltip = disabledWhenLabelIsHidden,
                disabled = function()
                    return not _type.Saved.LabelShow
                end,
                default = base.Default[typeName].Label.OffsetX,
                getFunc = function()
                    return _type.Saved.Label.OffsetX
                end,
                setFunc = function(value)
                    _type.Saved.Label.OffsetX = value
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end,
                min = -500,
                max = 500,
                width = "half"
            }, {
                type = "slider",
                name = "Display name offset vertical (Y)",
                tooltip = disabledWhenLabelIsHidden,
                disabled = function()
                    return not _type.Saved.LabelShow
                end,
                default = base.Default[typeName].Label.OffsetY,
                getFunc = function()
                    return _type.Saved.Label.OffsetY
                end,
                setFunc = function(value)
                    _type.Saved.Label.OffsetY = value
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end,
                min = -500,
                max = 500,
                width = "half"
            }, {
                type = "dropdown",
                name = "Display name anchor position on button",
                tooltip = "Select display name anchor position on the button\n" .. disabledWhenLabelIsHidden .. "\n or Collection is included in combine bar",
                disabled = function()
                    return not _type.Saved.LabelShow or _type.Saved.IsCombined
                end,
                choices = base.Global.ChoiceLocations,
                default = base.Default.BindingLocation,
                getFunc = function()
                    if _type.Saved.IsCombined then
                        return base.GetLocationText(base.Saved.Combine.Label.PositionTarget)
                    else
                        return base.GetLocationText(_type.Saved.Label.PositionTarget)
                    end
                end,
                setFunc = function(value)
                    _type.Saved.Label.PositionTarget = base.GetLocationValue(value)
                    _type.Saved.AnchorXY = base.GetLocationValue(value)
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end,
                width = "half"
            }, {
                type = "dropdown",
                name = "Display name anchor position on label",
                tooltip = "Select display name anchor position on the label\n" .. disabledWhenLabelIsHidden .. "\n or Collection is included in combine bar",
                disabled = function()
                    return not _type.Saved.LabelShow or _type.Saved.IsCombined
                end,
                choices = base.Global.ChoiceLocations,
                default = base.Default.BindingLocation,
                getFunc = function()
                    if _type.Saved.IsCombined then
                        return base.GetLocationText(base.Saved.Combine.Label.Position)
                    else
                        return base.GetLocationText(_type.Saved.Label.Position)
                    end
                end,
                setFunc = function(value)
                    _type.Saved.Label.Position = base.GetLocationValue(value)
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end,
                width = "half"
            }, {
                type = "dropdown",
                name = "Bar anchor location",
                tooltip = "Select Bar anchor location, used when bar is collapsed\n" .. disabledWhenCombined,
                choices = base.Global.ChoiceCornerLocations,
                default = base.Default[typeName].AnchorXY,
                disabled = function()
                    return _type.Saved.IsCombined
                end,
                getFunc = function()
                    if _type.Saved.IsCombined then
                        return base.GetLocationText(base.Saved.Combine.AnchorXY)
                    else
                        return base.GetLocationText(_type.Saved.AnchorXY)
                    end
                end,
                setFunc = function(value)
                    _type.Saved.AnchorXY = base.GetLocationValue(value)
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end,
                width = "half"
            }, {type = "divider", width = "full"}, {type = "description", text = "Here you can setup the position and size"}, {
                type = "slider",
                name = "Choose bar depth (number of rows/columns)",
                default = base.Default[typeName].BarDepth,
                tooltip = disabledWhenCombined,
                disabled = function()
                    return _type.Saved.IsCombined
                end,
                getFunc = function()
                    return _type.Saved.BarDepth
                end,
                setFunc = function(value)
                    _type.Saved.BarDepth = value
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end,
                min = 0,
                max = _type.Unlocked
            }, {
                type = "slider",
                name = "Choose max bar height (number of inverse rows/columns)",
                default = base.Default[typeName].BarWidth,
                tooltip = disabledWhenCombined,
                disabled = function()
                    return _type.Saved.IsCombined
                end,
                getFunc = function()
                    return _type.Saved.BarWidth
                end,
                setFunc = function(value)
                    _type.Saved.BarWidth = value
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end,
                min = 0,
                max = _type.Unlocked
            }, {
                type = "checkbox",
                name = "Bar orientation horizontal",
                default = base.Default[typeName].Horizontal,
                tooltip = disabledWhenCombined,
                disabled = function()
                    return _type.Saved.IsCombined
                end,
                getFunc = function()
                    return _type.Saved.Horizontal
                end,
                setFunc = function(newValue)
                    _type.Saved.Horizontal = newValue
                    base.RestoreFrame(_type)
                    base.RestoreCombineLabels()
                end
            }
        }
        table.insert(optionsData, controlIndex, {
            type = "submenu",
            name = string.format("Setup %s", typeName),
            tooltip = 'Choose what collection\'s you want on the bar\nDisabled when collection is disabled in "Collection Types"',
            disabled = function()
                return not _type.Saved.Enabled
            end,
            controls = controls
        })
    end

    local indexShowCollection = 7
    local indexCollectionMenuItem = 8
    for _, _type in ipairs(base.TypeOrdered) do
        AppendCollectionType(indexShowCollection, _type)
        AppendCollectionMenuItem(indexCollectionMenuItem, _type)
        indexCollectionMenuItem = indexCollectionMenuItem + 1
    end

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