local Srendarr = _G['Srendarr'] -- grab addon table from global
local LAM = LibStub('LibAddonMenu-2.0')
local LMP = LibStub('LibMediaProvider-1.0')
local L = Srendarr:GetLocale()

-- UPVALUES --
local WM            = GetWindowManager()
local CM            = CALLBACK_MANAGER
local tinsert       = table.insert
local tsort         = table.sort

-- DROPDOWN SETINGS --
local dropGrowth        = {L.DropGrowth1, L.DropGrowth2, L.DropGrowth3, L.DropGrowth4, L.DropGrowth5, L.DropGrowth6}
local dropGrowthRef     = {[L.DropGrowth1] = 'UP', [L.DropGrowth2] = 'DOWN', [L.DropGrowth3] = 'LEFT', [L.DropGrowth4] = 'LEFTCENTER', [L.DropGrowth5] = 'RIGHT', [L.DropGrowth6] = 'RIGHTCENTER',
                           ['UP'] = L.DropGrowth1, ['DOWN'] = L.DropGrowth2, ['LEFT'] = L.DropGrowth3, ['LEFTCENTER'] = L.DropGrowth4, ['RIGHT'] = L.DropGrowth5, ['RIGHTCENTER'] = L.DropGrowth6}
local dropSort          = {L.DropSort1, L.DropSort2}
local dropSortRef       = {[L.DropSort1] = 'TIME', [L.DropSort2] = 'NAME', ['TIME'] = L.DropSort1, ['NAME'] = L.DropSort2}
local dropTimerHorz     = {L.DropTimer1, L.DropTimer2, L.DropTimer3, L.DropTimer4}
local dropTimerVert     = {L.DropTimer1, L.DropTimer4}
local dropTimerRef      = {[L.DropTimer1] = 'OVER', [L.DropTimer2] = 'BELOW', [L.DropTimer3] = 'ABOVE', [L.DropTimer4] = 'HIDE',
                           ['OVER'] = L.DropTimer1, ['BELOW'] = L.DropTimer2, ['ABOVE'] = L.DropTimer3, ['HIDE'] = L.DropTimer4}

local playerSettingsList    = {}
local playerSettingCopyFrom

local tabButtons        = false
local tabPanels         = {}
local lastAddedControl  = {}
local examplesShown     = false
local controlPanel

-- PROFILE FUNCTIONS --
local function CopyTable(src, dest)
    if type(dest) ~= 'table' then dest = {} end
    if type(src) == 'table' then
        for k, v in pairs(src) do
            if type(v) == 'table' then
                CopyTable(v, dest[k])
            end

            dest[k] = v
        end
    end
end

local function CopySettingsFrom(src)
    local srcData, destData
    local dest = GetUnitName('player')

    for account, accountData in pairs(SrendarrDB.Default) do
        for character, data in pairs(accountData) do
            if (character == src and data.version == 2) then -- source character data
                srcData = data
            end

            if (character == dest) then -- dest character data (current character)
                destData = data
            end
        end
    end

    if (not srcData or not destData) then
        d(Srendarr.name .. ': Unable to find character settings to copy!')
        return
    end

    CopyTable(srcData, destData) -- copy settings

    ReloadUI() -- reloadui to re-initialize
end

local function GeneratePlayerList()
    local current = GetUnitName('player')

    for account, accountData in pairs(SrendarrDB.Default) do
        for player, data in pairs (accountData) do
            if (data.version == 2 and player ~= current) then -- only copy 2.X settings, ignore 1.X (and current character)
                tinsert(playerSettingsList, player)
            end
        end
    end

    tsort(playerSettingsList)
end

local function CreateWidgets(id, panelData)
    local panel = tabPanels[id]
    local isLastHalf = false
    local anchorOffset = 0

    for _, widgetData in pairs(panelData) do
        local widgetType = widgetData.type
        local widget = LAMCreateControl[widgetType](panel, widgetData)
        local isHalf = widgetData.width == "half"

        if isLastHalf and isHalf then
            widget:SetAnchor(TOPLEFT, lastAddedControl[id], TOPRIGHT, 10, 0)
            anchorOffset = zo_max(0, widget:GetHeight() - lastAddedControl[id]:GetHeight())
            isLastHalf = false
        else
            widget:SetAnchor(TOPLEFT, lastAddedControl[id], BOTTOMLEFT, 0, 15 + anchorOffset)
            anchorOffset = 0
            isLastHalf = isHalf
            lastAddedControl[id] = widget
        end
    end
end

local function BuildTabWindow(id)
    local panelData = {
        -- ----------------------------
        -- Base Window ----------------
        -- ----------------------------
        {
            type = "slider",
            name = L.WindowAlpha,
            tooltip = L.WindowAlphaTip,
            min = 5,
            max = 100,
            step = 5,
            getFunc = function() return Srendarr.db.frames[id].alpha * 100 end,
            setFunc = function(alpha)
                    Srendarr.db.frames[id].alpha = alpha / 100
                    Srendarr.auraFrames[id]:SetAlpha(alpha / 100)
                end,
        },
        {
            type = "slider",
            name = L.WindowScale,
            tooltip = L.WindowScaleTip,
            min = 50,
            max = 200,
            step = 5,
            getFunc = function() return Srendarr.db.frames[id].scale * 100 end,
            setFunc = function(scale)
                    Srendarr.db.frames[id].scale = scale / 100
                    Srendarr.auraAnchors[id]:SetScale(scale / 100)
                end,
        },
        {
            type = "dropdown",
            name = L.WindowGrowth,
            tooltip = L.WindowGrowthTip,
            choices = dropGrowth,
            getFunc = function() return dropGrowthRef[Srendarr.db.frames[id].auraGrowth] end,
            setFunc = function(arg)
                    Srendarr.db.frames[id].auraGrowth = dropGrowthRef[arg]
                    Srendarr.auraAnchors[id]:ConfigureDragOverlay()
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                    Srendarr.auraFrames[id]:ConfigureUpdateDisplay()
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "slider",
            name = L.WindowPadding,
            tooltip = L.WindowPaddingTip,
            min = 0,
            max = 24,
            getFunc = function() return Srendarr.db.frames[id].auraPadding end,
            setFunc = function(padding)
                    Srendarr.db.frames[id].auraPadding = padding
                    Srendarr.auraFrames[id]:ConfigureUpdateDisplay()
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "dropdown",
            name = L.WindowSort,
            tooltip = L.WindowSortTip,
            choices = dropSort,
            getFunc = function() return dropSortRef[Srendarr.db.frames[id].auraSort] end,
            setFunc = function(arg)
                    Srendarr.db.frames[id].auraSort = dropSortRef[arg]
                    Srendarr.auraFrames[id]:ConfigureUpdateDisplay()
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.WindowIconOn,
            tooltip = L.WindowIconOnTip,
            getFunc = function() return Srendarr.db.frames[id].auraIconOnRight end,
            setFunc = function(value)
                    Srendarr.db.frames[id].auraIconOnRight = value
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.WindowShowNameBar,
            tooltip = L.WindowShowNameBarTip,
            getFunc = function() return Srendarr.db.frames[id].showNameBar end,
            setFunc = function(value)
                    Srendarr.db.frames[id].showNameBar = value
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.WindowTooltips,
            tooltip = L.WindowTooltipsTip,
            getFunc = function() return Srendarr.db.frames[id].tooltips end,
            setFunc = function(value)
                    Srendarr.db.frames[id].tooltips = value
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
        },
        -- ----------------------------
        -- Name Label -----------------
        -- ----------------------------
        {
            type = "header",
            name = L.WindowNameHeader,
        },
        {
            type = "fontblock",
            name = L.WindowNameFont,
            getFace = function() return Srendarr.db.frames[id].nameFont end,
            getSize = function() return Srendarr.db.frames[id].nameSize end,
            getOutline = function() return Srendarr.db.frames[id].nameOutline end,
            getColor = function() return Srendarr.db.frames[id].nameColour.r, Srendarr.db.frames[id].nameColour.g, Srendarr.db.frames[id].nameColour.b end,
            setFace = function(font)
                    Srendarr.db.frames[id].nameFont = font
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
            setSize = function(size)
                    Srendarr.db.frames[id].nameSize = size
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
            setOutline = function(outline)
                    Srendarr.db.frames[id].nameOutline = outline
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
            setColor = function(r, g, b)
                    Srendarr.db.frames[id].nameColour.r = r
                    Srendarr.db.frames[id].nameColour.g = g
                    Srendarr.db.frames[id].nameColour.b = b
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                    Srendarr:ShowExampleAuras()
                end,
        },
        -- ----------------------------
        -- Timer Label ----------------
        -- ----------------------------
        {
            type = "header",
            name = L.WindowTimerHeader,
        },
        {
            type = "checkbox",
            name = L.WindowTimerShowTP,
            tooltip = L.WindowTimerShowTPTip,
            getFunc = function() return Srendarr.db.frames[id].timerShowTP end,
            setFunc = function(value)
                    Srendarr.db.frames[id].timerShowTP = value
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "dropdown",
            name = L.WindowTimerHorz,
            tooltip = L.WindowTimerHorzTip,
            choices = dropTimerHorz,
            getFunc = function() return dropTimerRef[Srendarr.db.frames[id].timerShowHorz] end,
            setFunc = function(arg)
                    Srendarr.db.frames[id].timerShowHorz = dropTimerRef[arg]
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
        },
        {
            type = "dropdown",
            name = L.WindowTimerVert,
            tooltip = L.WindowTimerVertTip,
            choices = dropTimerVert,
            getFunc = function() return dropTimerRef[Srendarr.db.frames[id].timerShowVert] end,
            setFunc = function(arg)
                    Srendarr.db.frames[id].timerShowVert = dropTimerRef[arg]
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
        },
        {
            type = "fontblock",
            name = L.WindowTimerFont,
            getFace = function() return Srendarr.db.frames[id].timerFont end,
            getSize = function() return Srendarr.db.frames[id].timerSize end,
            getOutline = function() return Srendarr.db.frames[id].timerOutline end,
            getColor = function() return Srendarr.db.frames[id].timerColour.r, Srendarr.db.frames[id].timerColour.g, Srendarr.db.frames[id].timerColour.b end,
            setFace = function(font)
                    Srendarr.db.frames[id].timerFont = font
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
            setSize = function(size)
                    Srendarr.db.frames[id].timerSize = size
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
            setOutline = function(outline)
                    Srendarr.db.frames[id].timerOutline = outline
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
            setColor = function(r, g, b)
                    Srendarr.db.frames[id].timerColour.r = r
                    Srendarr.db.frames[id].timerColour.g = g
                    Srendarr.db.frames[id].timerColour.b = b
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                    Srendarr:ShowExampleAuras()
                end,
        },
        -- ----------------------------
        -- Status Bar -----------------
        -- ----------------------------
        {
            type = "header",
            name = L.WindowBarHeader
        },
        {
            type = "checkbox",
            name = L.WindowBarGloss,
            tooltip = L.WindowBarGlossTip,
            getFunc = function() return Srendarr.db.frames[id].barGloss end,
            setFunc = function(value)
                    Srendarr.db.frames[id].barGloss = value
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
        },
        {
            type = "slider",
            name = L.WindowBarWidth,
            tooltip = L.WindowBarWidthTip,
            min = 40,
            max = 240,
            step = 5,
            getFunc = function() return Srendarr.db.frames[id].barWidth end,
            setFunc = function(width)
                    Srendarr.db.frames[id].barWidth = width
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                end,
        },
        {
            type = "doublecolor",
            name = L.WindowBarTimed,
            tooltip = L.WindowBarTimedTip,
            getFuncA = function() return Srendarr.db.frames[id].barColours.timed.r2, Srendarr.db.frames[id].barColours.timed.g2, Srendarr.db.frames[id].barColours.timed.b2 end,
            getFuncB = function() return Srendarr.db.frames[id].barColours.timed.r1, Srendarr.db.frames[id].barColours.timed.g1, Srendarr.db.frames[id].barColours.timed.b1 end,
            setFuncA = function(r, g, b)
                    Srendarr.db.frames[id].barColours.timed.r2 = r
                    Srendarr.db.frames[id].barColours.timed.g2 = g
                    Srendarr.db.frames[id].barColours.timed.b2 = b
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                    Srendarr:ShowExampleAuras()
                end,
            setFuncB = function(r, g, b)
                    Srendarr.db.frames[id].barColours.timed.r1 = r
                    Srendarr.db.frames[id].barColours.timed.g1 = g
                    Srendarr.db.frames[id].barColours.timed.b1 = b
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "doublecolor",
            name = L.WindowBarToggle,
            tooltip = L.WindowBarToggleTip,
            getFuncA = function() return Srendarr.db.frames[id].barColours.toggle.r2, Srendarr.db.frames[id].barColours.toggle.g2, Srendarr.db.frames[id].barColours.toggle.b2 end,
            getFuncB = function() return Srendarr.db.frames[id].barColours.toggle.r1, Srendarr.db.frames[id].barColours.toggle.g1, Srendarr.db.frames[id].barColours.toggle.b1 end,
            setFuncA = function(r, g, b)
                    Srendarr.db.frames[id].barColours.toggle.r2 = r
                    Srendarr.db.frames[id].barColours.toggle.g2 = g
                    Srendarr.db.frames[id].barColours.toggle.b2 = b
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                    Srendarr:ShowExampleAuras()
                end,
            setFuncB = function(r, g, b)
                    Srendarr.db.frames[id].barColours.toggle.r1 = r
                    Srendarr.db.frames[id].barColours.toggle.g1 = g
                    Srendarr.db.frames[id].barColours.toggle.b1 = b
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "doublecolor",
            name = L.WindowBarPassive,
            tooltip = L.WindowBarPassiveTip,
            getFuncA = function() return Srendarr.db.frames[id].barColours.passive.r2, Srendarr.db.frames[id].barColours.passive.g2, Srendarr.db.frames[id].barColours.passive.b2 end,
            getFuncB = function() return Srendarr.db.frames[id].barColours.passive.r1, Srendarr.db.frames[id].barColours.passive.g1, Srendarr.db.frames[id].barColours.passive.b1 end,
            setFuncA = function(r, g, b)
                    Srendarr.db.frames[id].barColours.passive.r2 = r
                    Srendarr.db.frames[id].barColours.passive.g2 = g
                    Srendarr.db.frames[id].barColours.passive.b2 = b
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                    Srendarr:ShowExampleAuras()
                end,
            setFuncB = function(r, g, b)
                    Srendarr.db.frames[id].barColours.passive.r1 = r
                    Srendarr.db.frames[id].barColours.passive.g1 = g
                    Srendarr.db.frames[id].barColours.passive.b1 = b
                    Srendarr.auraFrames[id]:ConfigureAllAuras()
                    Srendarr:ShowExampleAuras()
                end,
        },
    }

    CreateWidgets(id + 1, panelData)
end

local function BuildTabGeneral(id)
    local panelData = {
        {
            type ="description",
            text = L.GeneralAnchorDesc,
        },
        {
            type = "button",
            name = L.GeneralAnchorReset,
            func = function()
                    Srendarr.auraAnchors[1]:ClearAnchors()
                    Srendarr.auraAnchors[1]:SetAnchor(BOTTOMRIGHT, GuiRoot, BOTTOMRIGHT, 0, 0)
                    Srendarr.db.frames[1].anchor.point  = BOTTOMRIGHT
                    Srendarr.db.frames[1].anchor.x      = 0
                    Srendarr.db.frames[1].anchor.y      = 0
                    Srendarr.auraAnchors[2]:ClearAnchors()
                    Srendarr.auraAnchors[2]:SetAnchor(BOTTOMRIGHT, GuiRoot, BOTTOMRIGHT, -210, 0)
                    Srendarr.db.frames[2].anchor.point  = BOTTOMRIGHT
                    Srendarr.db.frames[2].anchor.x      = -210
                    Srendarr.db.frames[2].anchor.y      = 0
                    Srendarr.auraAnchors[3]:ClearAnchors()
                    Srendarr.auraAnchors[3]:SetAnchor(TOPRIGHT, GuiRoot, TOPRIGHT, 0, 0)
                    Srendarr.db.frames[3].anchor.point  = TOPRIGHT
                    Srendarr.db.frames[3].anchor.x      = 0
                    Srendarr.db.frames[3].anchor.y      = 0
                    Srendarr.auraAnchors[4]:ClearAnchors()
                    Srendarr.auraAnchors[4]:SetAnchor(TOP, GuiRoot, TOP, 161, 88)
                    Srendarr.db.frames[4].anchor.point  = TOP
                    Srendarr.db.frames[4].anchor.x      = 161
                    Srendarr.db.frames[4].anchor.y      = 88
                    Srendarr.auraAnchors[5]:ClearAnchors()
                    Srendarr.auraAnchors[5]:SetAnchor(TOP, GuiRoot, TOP, 200, 88)
                    Srendarr.db.frames[5].anchor.point  = TOP
                    Srendarr.db.frames[5].anchor.x      = 200
                    Srendarr.db.frames[5].anchor.y      = 88
                end,
            width = "half",
        },
        {
            type = "button",
            name = L.GeneralAnchorUnlock,
            func = function(lockBtn)
                    if (Srendarr.ui_Locked) then
                        Srendarr.SlashCommand('unlock')
                        lockBtn:SetText(L.GeneralAnchorLock)
                    else
                        Srendarr.SlashCommand('lock')
                        lockBtn:SetText(L.GeneralAnchorUnlock)
                    end
                end,
            width = "half",
        },
        -- ----------------------------
        -- Aura Controls --------------
        -- ----------------------------
        {
            type = "header",
            name = "", -- blank header just to add a nice dividing line
        },
        {
            type = "checkbox",
            name = L.GeneralOnlyCombat,
            tooltip = L.GeneralOnlyCombatTip,
            getFunc = function() return Srendarr.db.onlyInCombat end,
            setFunc = function(value)
                    Srendarr.db.onlyInCombat = value
                    Srendarr.OnCombatState(nil, IsUnitInCombat("player"))
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralCombine,
            tooltip = L.GeneralCombineTip,
            getFunc = function() return Srendarr.db.combineBuff end,
            setFunc = function(value)
                    Srendarr.db.combineBuff = value
                    Srendarr:ConfigureEvents()

                    if (not Srendarr.ui_Locked) then -- ui currently unlocked
                        Srendarr.auraAnchors[1]:ConfigureDragOverlay()

                        if (Srendarr.db.combineBuff) then -- auras combined, hide 2 overlay
                            Srendarr.auraAnchors[2]:DisableDrag()
                        else
                            Srendarr.auraAnchors[2]:EnableDrag()
                        end
                    end

                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "slider",
            name = L.GeneralThreshold,
            tooltip = L.GeneralThresholdTip,
            min = 30,
            max = 120,
            getFunc = function() return Srendarr.db.shortBuffThreshold end,
            setFunc = function(value)
                    Srendarr.db.shortBuffThreshold = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralTarget,
            tooltip = L.GeneralTargetTip,
            getFunc = function() return Srendarr.db.showTargetAuras end,
            setFunc = function(value)
                    Srendarr.db.showTargetAuras = value
                    Srendarr:ConfigureEvents()

                    if (not Srendarr.ui_Locked) then -- ui currently unlocked
                        Srendarr.auraAnchors[4]:ConfigureDragOverlay()

                        if (Srendarr.db.showTargetAuras) then -- showing
                            Srendarr.auraAnchors[4]:EnableDrag()
                        else
                            Srendarr.auraAnchors[4]:DisableDrag()
                        end
                    end

                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralTargetDebuff,
            tooltip = L.GeneralTargetTip,
            getFunc = function() return Srendarr.db.showTargetAurasDebuff end,
            setFunc = function(value)
                Srendarr.db.showTargetAurasDebuff = value
                Srendarr:ConfigureEvents()

                if (not Srendarr.ui_Locked) then -- ui currently unlocked
                    Srendarr.auraAnchors[5]:ConfigureDragOverlay()

                    if (Srendarr.db.showTargetAurasDebuff) then -- showing
                        Srendarr.auraAnchors[5]:EnableDrag()
                    else
                        Srendarr.auraAnchors[5]:DisableDrag()
                    end
                end

                Srendarr:ShowExampleAuras()
            end,
            warning = L.GeneralDebuffWarn,
        },
        {
            type = "checkbox",
            name = L.GeneralDebuff,
            tooltip = L.GeneralDebuffTip,
            getFunc = function() return Srendarr.db.showDebuff end,
            setFunc = function(value)
                    Srendarr.db.showDebuff = value
                    Srendarr.OnActionSlotsFullUpdate()
                    Srendarr:ShowExampleAuras()
                end,
            warning = L.GeneralDebuffWarn,
        },
        {
            type = "checkbox",
            name = L.GeneralMinorBuffs,
            tooltip = L.GeneralMinorBuffsTip,
            getFunc = function() return Srendarr.db.hideMinorBuffs end,
            setFunc = function(value)
                Srendarr.db.hideMinorBuffs = value
                Srendarr:ShowExampleAuras()
            end,
        },
        {
            type = "checkbox",
            name = L.GeneralMajorBuffs,
            tooltip = L.GeneralMajorBuffsTip,
            getFunc = function() return Srendarr.db.hideMajorBuffs end,
            setFunc = function(value)
                Srendarr.db.hideMajorBuffs = value
                Srendarr:ShowExampleAuras()
            end,
        },
        -- ----------------------------
        -- Aura Filters: Player -------
        -- ----------------------------
        {
            type = "header",
            name = L.GeneralHeaderFiltersPlayer,
        },
        {
            type = "checkbox",
            name = L.GeneralSoulSummons,
            tooltip = L.GeneralSoulSummonsTip,
            getFunc = function() return Srendarr.db.showSoulSummons end,
            setFunc = function(value)
                    Srendarr.db.showSoulSummons = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralToggle,
            tooltip = L.GeneralToggleTip,
            getFunc = function() return Srendarr.db.showToggle end,
            setFunc = function(value)
                    Srendarr.db.showToggle = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralPassive,
            tooltip = L.GeneralPassiveTip,
            getFunc = function() return Srendarr.db.showPassive end,
            setFunc = function(value)
                    Srendarr.db.showPassive = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralCyrodiil,
            tooltip = L.GeneralCyrodiilTip,
            getFunc = function() return Srendarr.db.showCyrodiil end,
            setFunc = function(value)
                    Srendarr.db.showCyrodiil = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralDisguise,
            tooltip = L.GeneralDisguiseTip,
            getFunc = function() return Srendarr.db.showDisguise end,
            setFunc = function(value)
                    Srendarr.db.showDisguise = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralAllEffects,
            tooltip = L.GeneralAllEffectsTip,
            getFunc = function() return Srendarr.db.showAllEffects end,
            setFunc = function(value)
                    Srendarr.db.showAllEffects = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralMundus,
            tooltip = L.GeneralMundusTip,
            getFunc = function() return Srendarr.db.showMundus end,
            setFunc = function(value)
                    Srendarr.db.showMundus = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralVampLycan,
            tooltip = L.GeneralVampLycanTip,
            getFunc = function() return Srendarr.db.showVampLycan end,
            setFunc = function(value)
                    Srendarr.db.showVampLycan = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        -- ----------------------------
        -- Aura Filters: Target -------
        -- ----------------------------
        {
            type = "header",
            name = L.GeneralHeaderFiltersTarget,
        },
        {
            type = "checkbox",
            name = L.GeneralSoulSummons,
            tooltip = L.GeneralSoulSummonsTip,
            getFunc = function() return Srendarr.db.showSoulSummonsTarget end,
            setFunc = function(value)
                    Srendarr.db.showSoulSummonsTarget = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralToggle,
            tooltip = L.GeneralToggleTip,
            getFunc = function() return Srendarr.db.showToggleTarget end,
            setFunc = function(value)
                    Srendarr.db.showToggleTarget = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralPassive,
            tooltip = L.GeneralPassiveTip,
            getFunc = function() return Srendarr.db.showPassiveTarget end,
            setFunc = function(value)
                    Srendarr.db.showPassiveTarget = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralCyrodiil,
            tooltip = L.GeneralCyrodiilTip,
            getFunc = function() return Srendarr.db.showCyrodiilTarget end,
            setFunc = function(value)
                    Srendarr.db.showCyrodiilTarget = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralDisguise,
            tooltip = L.GeneralDisguiseTip,
            getFunc = function() return Srendarr.db.showDisguiseTarget end,
            setFunc = function(value)
                    Srendarr.db.showDisguiseTarget = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralAllEffects,
            tooltip = L.GeneralAllEffectsTip,
            getFunc = function() return Srendarr.db.showAllEffectsTarget end,
            setFunc = function(value)
                    Srendarr.db.showAllEffectsTarget = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralMundus,
            tooltip = L.GeneralMundusTip,
            getFunc = function() return Srendarr.db.showMundusTarget end,
            setFunc = function(value)
                    Srendarr.db.showMundusTarget = value
                    Srendarr:ShowExampleAuras()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralVampLycan,
            tooltip = L.GeneralVampLycanTip,
            getFunc = function() return Srendarr.db.showVampLycanTarget end,
            setFunc = function(value)
                    Srendarr.db.showVampLycanTarget = value
                    Srendarr:ShowExampleAuras()
                end,
        },
    }

    CreateWidgets(id, panelData)
end

local function BuildTabProfile(id)
    GeneratePlayerList()

    local panelData = {
        {
            type = "dropdown",
            name = L.ProfileCharacterList,
            tooltip = L.ProfileCharacterListTip,
            choices = playerSettingsList,
            getFunc = function()
                    if (#playerSettingsList >= 1) then
                        playerSettingCopyFrom = playerSettingsList[1]
                        return playerSettingsList[1]
                    end
                end,
            setFunc = function(arg) playerSettingCopyFrom = arg end,
        },
        {
            type = "button",
            name = L.ProfileCopyFrom,
            tooltip = L.ProfileCopyFromTip,
            func = function() CopySettingsFrom(playerSettingCopyFrom) end,
            warning = L.ProfileCopyFromWarn,
        },
    }

    CreateWidgets(id, panelData)
end

local function BuildTabPanel(id)
    local panel = WM:CreateControl(nil, controlPanel.scroll, CT_CONTROL)
    panel.panel = controlPanel
    panel:SetWidth(controlPanel:GetWidth() - 60)
    panel:SetAnchor(TOPLEFT)
    panel:SetResizeToFitDescendents(true)

    tabPanels[id] = panel

    local ctrl = LAMCreateControl.header(panel, { type = 'header', name = L['TabHeader' .. id] })
    ctrl:SetAnchor(TOPLEFT)

    lastAddedControl[id] = ctrl

    -- build appropriuate panel
    if (id == 1) then -- general panel
        BuildTabGeneral(1)
    elseif (id == 7) then -- profiles panel
        BuildTabProfile(7)
    else -- one of the style panels
        BuildTabWindow(id - 1)
    end
end

local function BuildTabButton(controlPanel, id)
    local tab = WM:CreateControlFromVirtual(nil, controlPanel, 'ZO_DefaultButton')
    tab:SetWidth(((controlPanel:GetWidth() - 60) / 4) - 2)
    tab:SetText(L['TabButton' .. id])
    tab.tabID = id
    tab:SetHandler('OnClicked', function(btn)
        if (not tabPanels[btn.tabID]) then
            BuildTabPanel(btn.tabID)
        end
        for id, panel in pairs(tabPanels) do
            panel:SetHidden(not (id == btn.tabID))
        end
    end)

    return tab
end

function Srendarr.ShowExampleAuras()
    local time = GetGameTimeMilliseconds() / 1000

    Srendarr:StripAuras() -- only going to show examples

    for _, fragment in pairs(Srendarr.auraSceneFragments) do
        SCENE_MANAGER:AddFragment(fragment)
    end

    -- add example (short)
    Srendarr.auraFrames[1]:AddAura(1, 1, L.ExampleAura1, [[/esoui/art/icons/ability_destructionstaff_002.dds]], time, time + 8, false, false)
    Srendarr.auraFrames[1]:AddAura(1, 1, L.ExampleAura2, [[/esoui/art/icons/ability_destructionstaff_008.dds]], time, time + 24, false, false)
    Srendarr.auraFrames[1]:AddAura(1, 1, L.ExampleAura3, [[/esoui/art/icons/ability_armor_001.dds]], time, time + 48, false, false)
    -- add example (debuff)
    if (Srendarr.db.showDebuff) then
        Srendarr.auraFrames[3]:AddAura(1, 2, L.ExampleAura4, [[/esoui/art/icons/ability_armor_003.dds]], time, time + 8, false, false)
        Srendarr.auraFrames[3]:AddAura(1, 2, L.ExampleAura5, [[/esoui/art/icons/ability_destructionstaff_008.dds]], time, time + 24, false, false)
        Srendarr.auraFrames[3]:AddAura(1, 2, L.ExampleAura6, [[/esoui/art/icons/ability_armor_002.dds]], time, time + 48, false, false)
    end
    -- add example (long)
    if (Srendarr.db.combineBuff) then
        Srendarr.auraFrames[1]:AddAura(2, 1, L.ExampleAura7, [[/esoui/art/icons/ability_armor_001.dds]], time, time + 300, false, false)
        Srendarr.auraFrames[1]:AddAura(3, 1, L.ExampleAura8, [[/esoui/art/icons/ability_destructionstaff_011.dds]], 1, 1, false, false)
        Srendarr.auraFrames[1]:AddAura(4, 1, L.ExampleAura9, [[/esoui/art/icons/ability_destructionstaff_005.dds]], 1, 1, false, 0)
    else
        Srendarr.auraFrames[2]:AddAura(2, 1, L.ExampleAura7, [[/esoui/art/icons/ability_armor_001.dds]], time, time + 300, false, false)
        Srendarr.auraFrames[2]:AddAura(3, 1, L.ExampleAura8, [[/esoui/art/icons/ability_destructionstaff_011.dds]], 1, 1, false, false)
        Srendarr.auraFrames[2]:AddAura(4, 1, L.ExampleAura9, [[/esoui/art/icons/ability_destructionstaff_005.dds]], 1, 1, false, 0)
    end
    -- add example (target long)
    if (Srendarr.db.showTargetAuras) then
        Srendarr.auraFrames[4]:AddAura(2, 3, L.ExampleAura7, [[/esoui/art/icons/ability_armor_001.dds]], time, time + 300, false, false)
        Srendarr.auraFrames[4]:AddAura(3, 3, L.ExampleAura8, [[/esoui/art/icons/ability_destructionstaff_011.dds]], 1, 1, false, false)
        Srendarr.auraFrames[4]:AddAura(4, 3, L.ExampleAura9, [[/esoui/art/icons/ability_destructionstaff_005.dds]], 1, 1, false, 0)
    end
    -- add example (target debuff)
    if (Srendarr.db.showTargetAurasDebuff) then
        Srendarr.auraFrames[5]:AddAura(1, 1, L.ExampleAura4, [[/esoui/art/icons/ability_armor_003.dds]], time, time + 8, false, false)
        Srendarr.auraFrames[5]:AddAura(1, 1, L.ExampleAura5, [[/esoui/art/icons/ability_destructionstaff_008.dds]], time, time + 24, false, false)
        Srendarr.auraFrames[5]:AddAura(1, 1, L.ExampleAura6, [[/esoui/art/icons/ability_armor_002.dds]], time, time + 48, false, false)
    end
end

function Srendarr:InitializeSettings()
    local panelData = {
        type = 'panel',
        name = self.name,
        displayName = L.Srendarr,
        author = 'Kith, Garkin & silentgecko',
        version = self.version,
        registerForRefresh = true,
        registerForDefaults = false,
    }
    controlPanel = LAM:RegisterAddonPanel(self.name .. '_SettingsMenu', panelData)
    local buttonsWidth = controlPanel:GetWidth() - 40

    local buttons = WM:CreateControl(nil, controlPanel, CT_CONTROL)
    buttons:SetAnchor(TOPLEFT, controlPanel.label, BOTTOMLEFT, 0, 20)
    buttons:SetDimensions(buttonsWidth, 54)
    controlPanel.container:ClearAnchors()
    controlPanel.container:SetAnchor(TOPLEFT, buttons, BOTTOMLEFT, 0, 20)
    controlPanel.container:SetAnchor(BOTTOMRIGHT, controlPanel, BOTTOMRIGHT, -3, -3)

    local posLeft = (buttonsWidth/4)
    local tabBtn = BuildTabButton(controlPanel, 1)
    tabBtn:SetAnchor(TOPLEFT, buttons, TOPLEFT, 0, 0)
    tabBtn = BuildTabButton(controlPanel, 2)
    tabBtn:SetAnchor(TOPLEFT, buttons, TOPLEFT, posLeft, 0)
    tabBtn = BuildTabButton(controlPanel, 3)
    tabBtn:SetAnchor(TOPLEFT, buttons, TOPLEFT, posLeft * 3, 0)
    tabBtn = BuildTabButton(controlPanel, 4)
    tabBtn:SetAnchor(TOPLEFT, buttons, TOPLEFT, posLeft * 2, 0)
    tabBtn = BuildTabButton(controlPanel, 5)

    tabBtn:SetAnchor(BOTTOMLEFT, buttons, BOTTOMLEFT, 0, 6)
    tabBtn = BuildTabButton(controlPanel, 6)
    tabBtn:SetAnchor(BOTTOMLEFT, buttons, BOTTOMLEFT, posLeft, 6)
    tabBtn = BuildTabButton(controlPanel, 7)
    tabBtn:SetAnchor(BOTTOMLEFT, buttons, BOTTOMLEFT, posLeft * 3, 6)

    tabButtons = buttons

    ZO_PreHookHandler(tabButtons, "OnEffectivelyShown", function(control)
        if (not tabPanels[1]) then
            BuildTabPanel(1)
        end
        zo_callLater(self.ShowExampleAuras, 200) -- kinda hacky but it works
    end)

    ZO_PreHookHandler(tabButtons, 'OnEffectivelyHidden', function()
        self:UpdateAuras() -- clear all and repop with real ones if any
    end)
end