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

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

-- DROPDOWN SETTINGS --
local dropOverlay           = {L.DropOverlay1, L.DropOverlay2, L.DropOverlay3, L.DropOverlay4, L.DropOverlay5, L.DropOverlay6}
local dropOverlayRef        = {[L.DropOverlay1] = 1, [L.DropOverlay2] = 2, [L.DropOverlay3] = 3, [L.DropOverlay4] = 4, [L.DropOverlay5] = 5, [L.DropOverlay6] = 6}
local dropColourBy          = {L.DropColourBy1, L.DropColourBy2, L.DropColourBy3}
local dropColourByRef       = {[L.DropColourBy1] = 1, [L.DropColourBy2] = 2, [L.DropColourBy3] = 3}
local dropExpBarStyle       = {L.DropExpBarStyle1, L.DropExpBarStyle2, L.DropExpBarStyle3}
local dropExpBarStyleRef    = {[L.DropExpBarStyle1] = 1, [L.DropExpBarStyle2] = 2, [L.DropExpBarStyle3] = 3}

local playerSettingsList    = {}
local playerSettingCopyFrom

local tabButtons        = false
local tabPanels         = {}
local lastAddedControl  = {}
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(AzurahDB.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(Azurah.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(AzurahDB.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 optionsData = {
    -- ----------------------------
    -- GENERAL --------------------
    -- ----------------------------
    [1] = {
        {
            type = "description",
            text = L.GeneralAnchorDesc,
        },
        {
            type = "button",
            name = L.GeneralAnchorUnlock,
            func = function() Azurah.SlashCommand('unlock') end,
        },
        {
            type = "header",
            name = "", -- blank header just to add a nice dividing line
        },
        {
            type = "slider",
            name = L.GeneralPinScale,
            tooltip = L.GeneralPinScaleTip,
            min = 60,
            max = 110,
            getFunc = function() return Azurah.db.compassPinScale * 100 end,
            setFunc = function(arg)
                    Azurah.db.compassPinScale = arg / 100
                    Azurah:ConfigureCompass()
                end,
        },
        {
            type = "checkbox",
            name = L.GeneralPinLabel,
            tooltip = L.GeneralPinLabelTip,
            getFunc = function() return Azurah.db.compassHidePinLabel end,
            setFunc = function(arg)
                    Azurah.db.compassHidePinLabel = arg
                    Azurah:ConfigureCompass()
                end,
        },
    },
    -- ----------------------------
    -- ATTRIBUTES -----------------
    -- ----------------------------
    [2] = {
        {
            type = "slider",
            name = L.AttributesFadeMin,
            tooltip = L.AttributesFadeMinTip,
            min = 0,
            max = 100,
            step = 5,
            getFunc = function() return Azurah.db.attributes.fadeMinAlpha * 100 end,
            setFunc = function(arg)
                    Azurah.db.attributes.fadeMinAlpha = arg / 100
                    Azurah:ConfigureAttributeFade()
                end,
        },
        {
            type = "slider",
            name = L.AttributesFadeMax,
            tooltip = L.AttributesFadeMaxTip,
            min = 0,
            max = 100,
            step = 5,
            getFunc = function() return Azurah.db.attributes.fadeMaxAlpha * 100 end,
            setFunc = function(arg)
                    Azurah.db.attributes.fadeMaxAlpha = arg / 100
                    Azurah:ConfigureAttributeFade()
                end,
        },
        {
            type = "checkbox",
            name = L.AttributesCombatBars,
            tooltip = L.AttributesCombatBarsTip,
            getFunc = function() return Azurah.db.attributes.combatBars end,
            setFunc = function(arg)
                Azurah.db.attributes.combatBars = arg
                Azurah:ConfigureAttributeFade()
            end,
        },
        {
            type = "checkbox",
            name = L.AttributesLockSize,
            tooltip = L.AttributesLockSizeTip,
            getFunc = function() return Azurah.db.attributes.lockSize end,
            setFunc = function(arg)
                    Azurah.db.attributes.lockSize = arg
                    Azurah:ConfigureAttributeSizeLock()
                end,
            warning = L.AttributesLockSizeWarn,
        },
        {
            type = "header",
            name = L.AttributesOverlayHealth,
        },
        {
            type = "dropdown",
            name = L.SettingOverlayFormat,
            tooltip = L.AttributesOverlayFormatTip,
            choices = dropOverlay,
            getFunc = function() return dropOverlay[Azurah.db.attributes.healthOverlay] end,
            setFunc = function(arg)
                    Azurah.db.attributes.healthOverlay = dropOverlayRef[arg]
                    Azurah:ConfigureAttributeOverlays()
                end,
        },
        {
            type = "checkbox",
            name = L.SettingOverlayFancy,
            tooltip = L.SettingOverlayFancyTip,
            getFunc = function() return Azurah.db.attributes.healthOverlayFancy end,
            setFunc = function(arg)
                    Azurah.db.attributes.healthOverlayFancy = arg
                    Azurah:ConfigureAttributeOverlays()
                end,
        },
        {
            type = "fontblock",
            name = L.SettingOverlayStyle,
            getFace = function() return Azurah.db.attributes.healthFontFace end,
            getSize = function() return Azurah.db.attributes.healthFontSize end,
            getOutline = function() return Azurah.db.attributes.healthFontOutline end,
            getColor = function() return Azurah.db.attributes.healthFontColour.r, Azurah.db.attributes.healthFontColour.g, Azurah.db.attributes.healthFontColour.b, Azurah.db.attributes.healthFontColour.a end,
            setFace = function(font)
                    Azurah.db.attributes.healthFontFace = font
                    Azurah:ConfigureAttributeOverlays()
                end,
            setSize = function(size)
                    Azurah.db.attributes.healthFontSize = size
                    Azurah:ConfigureAttributeOverlays()
                end,
            setOutline = function(outline)
                    Azurah.db.attributes.healthFontOutline = outline
                    Azurah:ConfigureAttributeOverlays()
                end,
            setColor = function(r, g, b, a)
                    Azurah.db.attributes.healthFontColour.r = r
                    Azurah.db.attributes.healthFontColour.g = g
                    Azurah.db.attributes.healthFontColour.b = b
                    Azurah.db.attributes.healthFontColour.a = a
                    Azurah:ConfigureAttributeOverlays()
                end,
        },
        {
            type = "header",
            name = L.AttributesOverlayMagicka,
        },
        {
            type = "dropdown",
            name = L.SettingOverlayFormat,
            tooltip = L.AttributesOverlayFormatTip,
            choices = dropOverlay,
            getFunc = function() return dropOverlay[Azurah.db.attributes.magickaOverlay] end,
            setFunc = function(arg)
                    Azurah.db.attributes.magickaOverlay = dropOverlayRef[arg]
                    Azurah:ConfigureAttributeOverlays()
                end,
        },
        {
            type = "checkbox",
            name = L.SettingOverlayFancy,
            tooltip = L.SettingOverlayFancyTip,
            getFunc = function() return Azurah.db.attributes.magickaOverlayFancy end,
            setFunc = function(arg)
                    Azurah.db.attributes.magickaOverlayFancy = arg
                    Azurah:ConfigureAttributeOverlays()
                end,
        },
        {
            type = "fontblock",
            name = L.SettingOverlayStyle,
            getFace = function() return Azurah.db.attributes.magickaFontFace end,
            getSize = function() return Azurah.db.attributes.magickaFontSize end,
            getOutline = function() return Azurah.db.attributes.magickaFontOutline end,
            getColor = function() return Azurah.db.attributes.magickaFontColour.r, Azurah.db.attributes.magickaFontColour.g, Azurah.db.attributes.magickaFontColour.b, Azurah.db.attributes.magickaFontColour.a end,
            setFace = function(font)
                    Azurah.db.attributes.magickaFontFace = font
                    Azurah:ConfigureAttributeOverlays()
                end,
            setSize = function(size)
                    Azurah.db.attributes.magickaFontSize = size
                    Azurah:ConfigureAttributeOverlays()
                end,
            setOutline = function(outline)
                    Azurah.db.attributes.magickaFontOutline = outline
                    Azurah:ConfigureAttributeOverlays()
                end,
            setColor = function(r, g, b, a)
                    Azurah.db.attributes.magickaFontColour.r = r
                    Azurah.db.attributes.magickaFontColour.g = g
                    Azurah.db.attributes.magickaFontColour.b = b
                    Azurah.db.attributes.magickaFontColour.a = a
                    Azurah:ConfigureAttributeOverlays()
                end,
        },
        {
            type = "header",
            name = L.AttributesOverlayStamina,
        },
        {
            type = "dropdown",
            name = L.SettingOverlayFormat,
            tooltip = L.AttributesOverlayFormatTip,
            choices = dropOverlay,
            getFunc = function() return dropOverlay[Azurah.db.attributes.staminaOverlay] end,
            setFunc = function(arg)
                    Azurah.db.attributes.staminaOverlay = dropOverlayRef[arg]
                    Azurah:ConfigureAttributeOverlays()
                end,
        },
        {
            type = "checkbox",
            name = L.SettingOverlayFancy,
            tooltip = L.SettingOverlayFancyTip,
            getFunc = function() return Azurah.db.attributes.staminaOverlayFancy end,
            setFunc = function(arg)
                    Azurah.db.attributes.staminaOverlayFancy = arg
                    Azurah:ConfigureAttributeOverlays()
                end,
        },
        {
            type = "fontblock",
            name = L.SettingOverlayStyle,
            getFace = function() return Azurah.db.attributes.staminaFontFace end,
            getSize = function() return Azurah.db.attributes.staminaFontSize end,
            getOutline = function() return Azurah.db.attributes.staminaFontOutline end,
            getColor = function() return Azurah.db.attributes.staminaFontColour.r, Azurah.db.attributes.staminaFontColour.g, Azurah.db.attributes.staminaFontColour.b, Azurah.db.attributes.staminaFontColour.a end,
            setFace = function(font)
                    Azurah.db.attributes.staminaFontFace = font
                    Azurah:ConfigureAttributeOverlays()
                end,
            setSize = function(size)
                    Azurah.db.attributes.staminaFontSize = size
                    Azurah:ConfigureAttributeOverlays()
                end,
            setOutline = function(outline)
                    Azurah.db.attributes.staminaFontOutline = outline
                    Azurah:ConfigureAttributeOverlays()
                end,
            setColor = function(r, g, b, a)
                    Azurah.db.attributes.staminaFontColour.r = r
                    Azurah.db.attributes.staminaFontColour.g = g
                    Azurah.db.attributes.staminaFontColour.b = b
                    Azurah.db.attributes.staminaFontColour.a = a
                    Azurah:ConfigureAttributeOverlays()
                end,
        },
    },
    -- ----------------------------
    -- TARGET ---------------------
    -- ----------------------------
    [3] = {
        {
            type = "checkbox",
            name = L.TargetLockSize,
            tooltip = L.TargetLockSizeTip,
            getFunc = function() return Azurah.db.target.lockSize end,
            setFunc = function(arg)
                    Azurah.db.target.lockSize = arg
                    Azurah:ConfigureTargetSizeLock()
                end
        },
        {
            type = "dropdown",
            name = L.TargetColourByBar,
            tooltip = L.TargetColourByBarTip,
            choices = dropColourBy,
            getFunc = function() return dropColourBy[Azurah.db.target.colourByBar] end,
            setFunc = function(arg)
                    Azurah.db.target.colourByBar = dropColourByRef[arg]
                    Azurah:ConfigureTargetColouring()
                end,
        },
        {
            type = "dropdown",
            name = L.TargetColourByName,
            tooltip = L.TargetColourByNameTip,
            choices = dropColourBy,
            getFunc = function() return dropColourBy[Azurah.db.target.colourByName] end,
            setFunc = function(arg)
                    Azurah.db.target.colourByName = dropColourByRef[arg]
                    Azurah:ConfigureTargetColouring()
                end,
        },
        {
            type = "checkbox",
            name = L.TargetColourByLevel,
            tooltip = L.TargetColourByLevelTip,
            getFunc = function() return Azurah.db.target.colourByLevel end,
            setFunc = function(arg)
                    Azurah.db.target.colourByLevel = arg
                    Azurah:ConfigureTargetColouring()
                end,
        },
        {
            type = "checkbox",
            name = L.TargetIconClassShow,
            tooltip = L.TargetIconClassShowTip,
            getFunc = function() return Azurah.db.target.classShow end,
            setFunc = function(arg)
                    Azurah.db.target.classShow = arg
                    Azurah:ConfigureTargetIcons()
                end,
        },
        {
            type = "checkbox",
            name = L.TargetIconClassByName,
            tooltip = L.TargetIconClassByNameTip,
            getFunc = function() return Azurah.db.target.classByName end,
            setFunc = function(arg)
                    Azurah.db.target.classByName = arg
                    Azurah:ConfigureTargetIcons()
                end,
        },
        {
            type = "checkbox",
            name = L.TargetIconAllianceShow,
            tooltip = L.TargetIconAllianceShowTip,
            getFunc = function() return Azurah.db.target.allianceShow end,
            setFunc = function(arg)
                    Azurah.db.target.allianceShow = arg
                    Azurah:ConfigureTargetIcons()
                end,
        },
        {
            type = "checkbox",
            name = L.TargetIconAllianceByName,
            tooltip = L.TargetIconAllianceByNameTip,
            getFunc = function() return Azurah.db.target.allianceByName end,
            setFunc = function(arg)
                    Azurah.db.target.allianceByName = arg
                    Azurah:ConfigureTargetIcons()
                end,
        },
        {
            type = "dropdown",
            name = L.SettingOverlayFormat,
            tooltip = L.TargetOverlayFormatTip,
            choices = dropOverlay,
            getFunc = function() return dropOverlay[Azurah.db.target.overlay] end,
            setFunc = function(arg)
                    Azurah.db.target.overlay = dropOverlayRef[arg]
                    Azurah:ConfigureTargetOverlay()
                end,
        },
        {
            type = "checkbox",
            name = L.SettingOverlayFancy,
            tooltip = L.SettingOverlayFancyTip,
            getFunc = function() return Azurah.db.target.overlayFancy end,
            setFunc = function(arg)
                    Azurah.db.target.overlayFancy = arg
                    Azurah:ConfigureTargetOverlay()
                end,
        },
        {
            type = "fontblock",
            name = L.SettingOverlayStyle,
            getFace = function() return Azurah.db.target.fontFace end,
            getSize = function() return Azurah.db.target.fontSize end,
            getOutline = function() return Azurah.db.target.fontOutline end,
            getColor = function() return Azurah.db.target.fontColour.r, Azurah.db.target.fontColour.g, Azurah.db.target.fontColour.b, Azurah.db.target.fontColour.a end,
            setFace = function(font)
                    Azurah.db.target.fontFace = font
                    Azurah:ConfigureTargetOverlay()
                end,
            setSize = function(size)
                    Azurah.db.target.fontSize = size
                    Azurah:ConfigureTargetOverlay()
                end,
            setOutline = function(outline)
                    Azurah.db.target.fontOutline = outline
                    Azurah:ConfigureTargetOverlay()
                end,
            setColor = function(r, g, b, a)
                    Azurah.db.target.fontColour.r = r
                    Azurah.db.target.fontColour.g = g
                    Azurah.db.target.fontColour.b = b
                    Azurah.db.target.fontColour.a = a
                    Azurah:ConfigureTargetOverlay()
                end,
        },
        {
            type = "header",
            name = L.BossbarHeader,
        },
        {
            type = "dropdown",
            name = L.SettingOverlayFormat,
            tooltip = L.BossbarOverlayFormatTip,
            choices = dropOverlay,
            getFunc = function() return dropOverlay[Azurah.db.bossbar.overlay] end,
            setFunc = function(arg)
                    Azurah.db.bossbar.overlay = dropOverlayRef[arg]
                    Azurah:ConfigureBossbarOverlay()
                end,
        },
        {
            type = "checkbox",
            name = L.SettingOverlayFancy,
            tooltip = L.SettingOverlayFancyTip,
            getFunc = function() return Azurah.db.bossbar.overlayFancy end,
            setFunc = function(arg)
                    Azurah.db.bossbar.overlayFancy = arg
                    Azurah:ConfigureBossbarOverlay()
                end,
        },
        {
            type = "fontblock",
            name = L.SettingOverlayStyle,
            getFace = function() return Azurah.db.bossbar.fontFace end,
            getSize = function() return Azurah.db.bossbar.fontSize end,
            getOutline = function() return Azurah.db.bossbar.fontOutline end,
            getColor = function() return Azurah.db.bossbar.fontColour.r, Azurah.db.bossbar.fontColour.g, Azurah.db.bossbar.fontColour.b, Azurah.db.bossbar.fontColour.a end,
            setFace = function(font)
                    Azurah.db.bossbar.fontFace = font
                    Azurah:ConfigureBossbarOverlay()
                end,
            setSize = function(size)
                    Azurah.db.bossbar.fontSize = size
                    Azurah:ConfigureBossbarOverlay()
                end,
            setOutline = function(outline)
                    Azurah.db.bossbar.fontOutline = outline
                    Azurah:ConfigureBossbarOverlay()
                end,
            setColor = function(r, g, b, a)
                    Azurah.db.bossbar.fontColour.r = r
                    Azurah.db.bossbar.fontColour.g = g
                    Azurah.db.bossbar.fontColour.b = b
                    Azurah.db.bossbar.fontColour.a = a
                    Azurah:ConfigureBossbarOverlay()
                end,
        },
    },
    -- ----------------------------
    -- ACTION BAR -----------------
    -- ----------------------------
    [4] = {
        {
            type = "checkbox",
            name = L.ActionBarHideBindBG,
            tooltip = L.ActionBarHideBindBGTip,
            getFunc = function() return Azurah.db.actionBar.hideBindBG end,
            setFunc = function(arg)
                    Azurah.db.actionBar.hideBindBG = arg
                    Azurah:ConfigureActionBarElements()
                end,
        },
        {
            type = "checkbox",
            name = L.ActionBarHideBindText,
            tooltip = L.ActionBarHideBindTextTip,
            getFunc = function() return Azurah.db.actionBar.hideBindText end,
            setFunc = function(arg)
                    Azurah.db.actionBar.hideBindText = arg
                    Azurah:ConfigureActionBarElements()
                end,
        },
        {
            type = "checkbox",
            name = L.ActionBarHideWeaponSwap,
            tooltip = L.ActionBarHideWeaponSwapTip,
            getFunc = function() return Azurah.db.actionBar.hideWeaponSwap end,
            setFunc = function(arg)
                    Azurah.db.actionBar.hideWeaponSwap = arg
                    Azurah:ConfigureActionBarElements()
                end,
        },
        {
            type = "header",
            name = L.ActionBarOverlayUltValue,
        },
        {
            type = "checkbox",
            name = L.ActionBarOverlayShow,
            tooltip = L.ActionBarOverlayUltValueShowTip,
            getFunc = function() return Azurah.db.actionBar.ultValueShow end,
            setFunc = function(arg)
                    Azurah.db.actionBar.ultValueShow = arg
                    Azurah:ConfigureUltimateOverlays()
                end,
        },
        {
            type = "checkbox",
            name = L.ActionBarOverlayUltValueShowCost,
            tooltip = L.ActionBarOverlayUltValueShowCostTip,
            getFunc = function() return Azurah.db.actionBar.ultValueShowCost end,
            setFunc = function(arg)
                    Azurah.db.actionBar.ultValueShowCost = arg
                    Azurah:ConfigureUltimateOverlays()
                end,
        },
        {
            type = "fontblock",
            name = L.SettingOverlayStyle,
            getFace = function() return Azurah.db.actionBar.ultValueFontFace end,
            getSize = function() return Azurah.db.actionBar.ultValueFontSize end,
            getOutline = function() return Azurah.db.actionBar.ultValueFontOutline end,
            getColor = function() return Azurah.db.actionBar.ultValueFontColour.r, Azurah.db.actionBar.ultValueFontColour.g, Azurah.db.actionBar.ultValueFontColour.b, Azurah.db.actionBar.ultValueFontColour.a end,
            setFace = function(font)
                    Azurah.db.actionBar.ultValueFontFace = font
                    Azurah:ConfigureUltimateOverlays()
                end,
            setSize = function(size)
                    Azurah.db.actionBar.ultValueFontSize = size
                    Azurah:ConfigureUltimateOverlays()
                end,
            setOutline = function(outline)
                    Azurah.db.actionBar.ultValueFontOutline = outline
                    Azurah:ConfigureUltimateOverlays()
                end,
            setColor = function(r, g, b, a)
                    Azurah.db.actionBar.ultValueFontColour.r = r
                    Azurah.db.actionBar.ultValueFontColour.g = g
                    Azurah.db.actionBar.ultValueFontColour.b = b
                    Azurah.db.actionBar.ultValueFontColour.a = a
                    Azurah:ConfigureUltimateOverlays()
                end,
        },
        {
            type = "header",
            name = L.ActionBarOverlayUltPercent,
        },
        {
            type = "checkbox",
            name = L.ActionBarOverlayShow,
            tooltip = L.ActionBarOverlayUltPercentShowTip,
            getFunc = function() return Azurah.db.actionBar.ultPercentShow end,
            setFunc = function(arg)
                    Azurah.db.actionBar.ultPercentShow = arg
                    Azurah:ConfigureUltimateOverlays()
                end,
        },
        {
            type = "checkbox",
            name = L.ActionBarOverlayUltPercentRelative,
            tooltip = L.ActionBarOverlayUltPercentRelativeTip,
            getFunc = function() return Azurah.db.actionBar.ultPercentRelative end,
            setFunc = function(arg)
                    Azurah.db.actionBar.ultPercentRelative = arg
                    Azurah:ConfigureUltimateOverlays()
                end,
        },
        {
            type = "fontblock",
            name = L.SettingOverlayStyle,
            getFace = function() return Azurah.db.actionBar.ultPercentFontFace end,
            getSize = function() return Azurah.db.actionBar.ultPercentFontSize end,
            getOutline = function() return Azurah.db.actionBar.ultPercentFontOutline end,
            getColor = function() return Azurah.db.actionBar.ultPercentFontColour.r, Azurah.db.actionBar.ultPercentFontColour.g, Azurah.db.actionBar.ultPercentFontColour.b, Azurah.db.actionBar.ultPercentFontColour.a end,
            setFace = function(font)
                    Azurah.db.actionBar.ultPercentFontFace = font
                    Azurah:ConfigureUltimateOverlays()
                end,
            setSize = function(size)
                    Azurah.db.actionBar.ultPercentFontSize = size
                    Azurah:ConfigureUltimateOverlays()
                end,
            setOutline = function(outline)
                    Azurah.db.actionBar.ultPercentFontOutline = outline
                    Azurah:ConfigureUltimateOverlays()
                end,
            setColor = function(r, g, b, a)
                    Azurah.db.actionBar.ultPercentFontColour.r = r
                    Azurah.db.actionBar.ultPercentFontColour.g = g
                    Azurah.db.actionBar.ultPercentFontColour.b = b
                    Azurah.db.actionBar.ultPercentFontColour.a = a
                    Azurah:ConfigureUltimateOverlays()
                end,
        },
    },
    -- ----------------------------
    -- EXPERIENCE BAR -------------
    -- ----------------------------
    [5] = {
        {
            type = "dropdown",
            name = L.ExperienceDisplayStyle,
            tooltip = L.ExperienceDisplayStyleTip,
            choices = dropExpBarStyle,
            getFunc = function() return dropExpBarStyle[Azurah.db.experienceBar.displayStyle] end,
            setFunc = function(arg)
                    Azurah.db.experienceBar.displayStyle = dropExpBarStyleRef[arg]
                    Azurah:ConfigureExperienceBarDisplay()
                end,
        },
        {
            type = "dropdown",
            name = L.SettingOverlayFormat,
            tooltip = L.ExperienceOverlayFormatTip,
            choices = dropOverlay,
            getFunc = function() return dropOverlay[Azurah.db.experienceBar.overlay] end,
            setFunc = function(arg)
                    Azurah.db.experienceBar.overlay = dropOverlayRef[arg]
                    Azurah:ConfigureExperienceBarOverlay()
                end,
        },
        {
            type = "checkbox",
            name = L.SettingOverlayFancy,
            tooltip = L.SettingOverlayFancyTip,
            getFunc = function() return Azurah.db.experienceBar.overlayFancy end,
            setFunc = function(arg)
                    Azurah.db.experienceBar.overlayFancy = arg
                    Azurah:ConfigureExperienceBarOverlay()
                end,
        },
        {
            type = "fontblock",
            name = L.SettingOverlayStyle,
            getFace = function() return Azurah.db.experienceBar.fontFace end,
            getSize = function() return Azurah.db.experienceBar.fontSize end,
            getOutline = function() return Azurah.db.experienceBar.fontOutline end,
            getColor = function() return Azurah.db.experienceBar.fontColour.r, Azurah.db.experienceBar.fontColour.g, Azurah.db.experienceBar.fontColour.b, Azurah.db.experienceBar.fontColour.a end,
            setFace = function(font)
                    Azurah.db.experienceBar.fontFace = font
                    Azurah:ConfigureExperienceBarOverlay()
                end,
            setSize = function(size)
                    Azurah.db.experienceBar.fontSize = size
                    Azurah:ConfigureExperienceBarOverlay()
                end,
            setOutline = function(outline)
                    Azurah.db.experienceBar.fontOutline = outline
                    Azurah:ConfigureExperienceBarOverlay()
                end,
            setColor = function(r, g, b, a)
                    Azurah.db.experienceBar.fontColour.r = r
                    Azurah.db.experienceBar.fontColour.g = g
                    Azurah.db.experienceBar.fontColour.b = b
                    Azurah.db.experienceBar.fontColour.a = a
                    Azurah:ConfigureExperienceBarOverlay()
                end,
        },
    },
    -- ----------------------------
    -- PROFILES -------------------
    -- ----------------------------
    [6] = {
        {
            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,
        },
    },
}

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 = optionsData[id]

    if (id == 6) then
        GeneratePlayerList()
    end

    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

    BuildTabWindow(id)
end

local function BuildTabButton(controlPanel, id)
    local tab = WM:CreateControlFromVirtual(nil, controlPanel, 'ZO_DefaultButton')
    tab:SetWidth(((controlPanel:GetWidth() - 60) / 3) - 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 Azurah:InitializeSettings()
    local panelData = {
        type = 'panel',
        name = self.name,
        displayName = L.Azurah,
        author = 'Kith, Garkin, Phinix, Sounomi',
        version = self.version,
        registerForRefresh = true,
        registerForDefaults = false,
    }
    controlPanel = LAM:RegisterAddonPanel(self.name .. '_SettingsMenu', panelData)
    local buttonsWidth = controlPanel:GetWidth() - 60

    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 tabBtn = BuildTabButton(controlPanel, 1)
    tabBtn:SetAnchor(TOPLEFT, buttons, TOPLEFT, 0, 0)
    tabBtn = BuildTabButton(controlPanel, 2)
    tabBtn:SetAnchor(TOPLEFT, buttons, TOPLEFT, buttonsWidth / 3, 0)
    tabBtn = BuildTabButton(controlPanel, 3)
    tabBtn:SetAnchor(TOPLEFT, buttons, TOPLEFT, buttonsWidth / 3 * 2, 0)
    tabBtn = BuildTabButton(controlPanel, 4)
    tabBtn:SetAnchor(BOTTOMLEFT, buttons, BOTTOMLEFT, 0, 8)
    tabBtn = BuildTabButton(controlPanel, 5)
    tabBtn:SetAnchor(BOTTOMLEFT, buttons, BOTTOMLEFT, buttonsWidth / 3, 8)
    tabBtn = BuildTabButton(controlPanel, 6)
    tabBtn:SetAnchor(BOTTOMLEFT, buttons, BOTTOMLEFT, buttonsWidth / 3 * 2, 8)

    tabButtons = buttons

    ZO_PreHookHandler(controlPanel, "OnEffectivelyShown", function(control)
        if (not tabPanels[1]) then
            BuildTabPanel(1)
        end
    end)
end