local _AddonName, _EchoRaidTools = ...;


local module = _EchoRaidTools:GetModule("todo-list")

module.config = module.config or {}
local config = module.config
local currentlyConfiguredMod
local currentlyConfiguredModUnaltered
local editedFields
local editor
local openLoadConditionEditor, loadConditionEditor
local getModConfigEditor


local function getField(fieldTable)
    return module:GetField(currentlyConfiguredMod, fieldTable)
end

local function doesFieldMatchUnalteredState(fieldTable)
    return module:DeepCompare(getField(fieldTable), module:GetField(currentlyConfiguredModUnaltered, fieldTable))
end

local function updateTempModDisplay()
    currentlyConfiguredMod.identifier = currentlyConfiguredMod.identifier or module.GenerateUid()
    module:UpdateModState(currentlyConfiguredMod)
end

local function registerFieldEdit(fieldTable)
    local key = ""

    for i, field in ipairs(fieldTable) do
        key = key .. field
    end

    editedFields[key] = fieldTable
    updateTempModDisplay()
end

local function setField(fieldTable, value)
    if module:SetField(currentlyConfiguredMod, fieldTable, value) then
        registerFieldEdit(fieldTable)
    end
end


local function getFonts()
    local fontsTable = {}
    for i, fontName in ipairs(module.lsm:GetFonts()) do
        local fontFile = module.lsm:GetFontPath(fontName)
        table.insert(fontsTable, { label = fontName, value = fontName, font = fontFile })
    end
    return fontsTable
end

local function getTextures()
    local texturesTable = {}
    for i, textureName in ipairs(module.lsm:GetStatusbars()) do
        local statusbarFile = module.lsm:GetStatusbarPath(textureName)
        table.insert(texturesTable, { label = textureName, value = textureName, statusbar = statusbarFile })
    end
    return texturesTable
end


local function getNumberOfQuestIds()
    return currentlyConfiguredMod.questIds and #currentlyConfiguredMod.questIds or 1
end

local function getDifficulties()
    local instanceId = currentlyConfiguredMod.instanceId
    if not instanceId then return {} end
    local instanceInfo = module:GetInstanceInfoByInstanceId(instanceId)
    if not instanceInfo then return {} end
    if not instanceInfo.difficulties then return {} end
    local dropdownTable = {
        { label = "Any", value = nil }
    }

    for i, id in pairs(instanceInfo.difficulties) do
        local diffName = GetDifficultyInfo(id)
        table.insert(dropdownTable, { label = diffName, value = id })
    end
    return dropdownTable
end

local function getInstances()
    local instances = module:GetInstances()
    local ret = {}
    for i, instanceInfo in pairs(instances) do
        table.insert(ret, {
            label = instanceInfo.name,
            value = instanceInfo.mapId,
        })
    end
    table.sort(ret, function(a, b)
        return a.value == b.value and a.label > b.label or a.value > b.value
    end)
    return ret
end
local configFields = {
    {
        inputType = "string",
        name = "Task Name",
        field = { "name" },
        --TODO add tooltip for these
    },
    {
        inputType = "dropdown",
        name = "Task Type",
        field = { "type" },
        dropdownOptions = {
            { label = "Quest",           value = "quest" }, --should probably have a helper function to get label, it's referenced elsewhere too
            { label = "Custom",          value = "custom" },
            { label = "Achievement",     value = "achievement" },
            { label = "Currency",        value = "currency" },
            { label = "Title / Divider", value = "title" },
            { label = "Vault",           value = "vault" },
            { label = "Reputation",      value = "reputation" },
            { label = "Money",           value = "money" },
            { label = "Item",            value = "item" },
            { label = "Item Level",      value = "itemlevel" },
            { label = "Bonus ID",        value = "bonusID" },
            { label = "Lockout",         value = "lockout" },
            { label = "Level",           value = "level" },
            --{ label = "Event",   value = "event" },
        },
    },
    {
        inputType = "dropdown",
        name = "Bar Texture",
        field = { "texture" },
        dropdownOptions = getTextures,
    },
    {
        inputType = "colorpicker",
        field = { "colour" },
        name = "Bar Colour",
    },
    {
        inputType = "colorpicker",
        field = { "bgColour" },
        name = "Bar Background Colour",
    },
    {
        inputType = "slider",
        name = "Bar Height",
        field = { "height" },
        boundaries = { 1, 200, 1 }
    },
    {
        inputType = "dropdown",
        name = "Font",
        field = { "font" },
        dropdownOptions = getFonts,
    },
    {
        inputType = "slider",
        name = "Text Font Size",
        field = { "textFontSize" },
        boundaries = { 1, 30, 1 }
    },
    {
        inputType = "colorpicker",
        field = { "textColour" },
        name = "Text Colour",
    },
    {
        inputType = "slider",
        name = "Progress Text Font Size",
        field = { "progressTextFontSize" },
        boundaries = { 1, 30, 1 }
    },
    {
        inputType = "colorpicker",
        field = { "progressTextColour" },
        name = "Progress Text Colour",
    },
    {
        inputType = "string",
        name = "Icon or Texture",
        field = { "icon" },
    },
    {
        inputType = "string",
        name = "Title Text",
        field = { "text" },
        condition = function() return currentlyConfiguredMod.type == "title" end,
    },
    {
        inputType = "string",
        name = "Title Override",
        field = { "text" },
        condition = function() return currentlyConfiguredMod.type ~= "title" end,
    },
    {
        inputType = "string",
        name = "Progress Text",
        field = { "progressText" },
        condition = function() return currentlyConfiguredMod.type == "title" end,
    },
    {
        inputType = "checkbox",
        condition = function() return currentlyConfiguredMod.type == "title" end,
        name = "Fill Bar",
        field = { "filled" },
    },
    {
        inputType = "multinumber",
        condition = function() return currentlyConfiguredMod.type == "quest" end,
        name = "Quest Id(s)",
        field = { "questIds" },
    },
    {
        inputType = "dropdown",
        name = "Quest Criteria",
        field = { "questState" },
        dropdownOptions = {
            { label = "Rep Reward Consumed", value = "hasRepBonus" },
            { label = "Turned In Warbound",  value = "warbound" },
            { label = "Turned In",           value = nil },
            { label = "Completed",           value = "done" },
            { label = "Picked Up",           value = "pickup" },
        },
        condition = function() return currentlyConfiguredMod.type == "quest" end,
    },
    {
        inputType = "checkbox",
        condition = function() return currentlyConfiguredMod.type ~= "title" end,
        name = "Hide when completed",
        field = { "hideOnComplete" },
    },
    {
        inputType = "checkbox",
        condition = function() return currentlyConfiguredMod.type == "itemlevel" end,
        name = "Item slot ilvl",
        field = { "useItemslot" },
    },
    {
        inputType = "number",
        name = "Max",
        field = { "total" },
        condition = function()
            return
                currentlyConfiguredMod.type == "quest" and getNumberOfQuestIds() > 1
                or currentlyConfiguredMod.type == "currency"
                or currentlyConfiguredMod.type == "reputation"
                or currentlyConfiguredMod.type == "vault"
                or currentlyConfiguredMod.type == "money"
                or currentlyConfiguredMod.type == "item"
                or currentlyConfiguredMod.type == "itemlevel"
                or currentlyConfiguredMod.type == "lockout"
                or currentlyConfiguredMod.type == "level"
                or currentlyConfiguredMod.type == "bonusID" and currentlyConfiguredMod.bonusType == "gem"
        end,
    },
    {
        inputType = "number",
        condition = function() return currentlyConfiguredMod.type == "currency" end,
        name = "Currency ID",
        field = { "currencyId" },
    },
    {
        inputType = "number",
        condition = function() return currentlyConfiguredMod.type == "achievement" end,
        name = "Achievement ID",
        field = { "achievementId" },
    },
    {
        inputType = "number",
        condition = function() return currentlyConfiguredMod.type == "reputation" end,
        name = "Reputation ID",
        field = { "reputationId" },
    },
    {
        inputType = "number",
        condition = function() return currentlyConfiguredMod.type == "item" end,
        name = "Item ID",
        field = { "itemId" },
    },
    {
        inputType = "dropdown",
        name = "Instance",
        field = { "instanceId" },
        dropdownOptions = getInstances,
        condition = function() return currentlyConfiguredMod.type == "lockout" end,
    },
    {
        inputType = "number",
        condition = function()
            return currentlyConfiguredMod.type == "bonusID" or
                currentlyConfiguredMod.type == "itemlevel" and currentlyConfiguredMod.useItemslot
        end,
        name = "Slot ID",
        field = { "itemslot" },
    },
    {
        inputType = "dropdown",
        name = "Vault Type",
        field = { "activtyID" },
        dropdownOptions = {
            { label = "Dungeons", value = 1 },
            { label = "Raid",     value = 3 },
            { label = "World",    value = 6 },
        },
        condition = function() return currentlyConfiguredMod.type == "vault" end,
    },
    {
        inputType = "dropdown",
        name = "Bonus Type",
        field = { "bonusType" },
        dropdownOptions = {
            { label = "Enchant", value = "enchant" },
            { label = "Gem",     value = "gem" },
        },
        condition = function() return currentlyConfiguredMod.type == "bonusID" end,
    },
    {
        inputType = "dropdown",
        name = "Difficulty",
        field = { "difficultyId" },
        dropdownOptions = getDifficulties,
        condition = function() return currentlyConfiguredMod.type == "lockout" end,
    },
    {
        inputType = "checkbox",
        name = "Enable Timer",
        field = { "enableTimer" },
    },
    {
        inputType = "number",
        condition = function() return currentlyConfiguredMod.enableTimer end,
        name = "Interval",
        field = { "timer", "interval" },
    },
    {
        inputType = "number",
        condition = function() return currentlyConfiguredMod.enableTimer end,
        name = "Duration",
        field = { "timer", "duration" },
    },
    {
        inputType = "number",
        condition = function() return currentlyConfiguredMod.enableTimer end,
        name = "Offset",
        field = { "timer", "offset" },
    },
}


local function updateConfigButtons()
    local configEditor = getModConfigEditor()
    if not configEditor then return end
    if not configEditor.configButtons then
        configEditor.configButtons = {}

        for i, configOptions in ipairs(configFields) do
            local button = {}
            button.condition = configOptions.condition or function() return true end
            button.xOffset = configOptions.position and configOptions.position[1] or 0
            button.yOffset = configOptions.position and configOptions.position[2] or 0


            if configOptions.inputType == "string" then
                button.frame = CreateFrame("Frame", nil, configEditor, "EchoRaidToolsConfig_EditBox")
                button.frame.TopText:SetText(configOptions.name)
                button.frame.TopText:Show()
                button.frame:SetWidth(200)
                button.update = function()
                    button.frame.Editbox:SetText(getField(configOptions.field) or "")
                end



                button.frame.Editbox:SetScript("OnTextChanged", function()
                    --use OnEditFocusLost instead?¨
                    local text = button.frame.Editbox:GetText()
                    setField(configOptions.field, text ~= "" and text or nil)
                end)

                button.frame.Editbox:SetScript("OnEditFocusLost", updateConfigButtons)

                button.yOffset = button.yOffset + 5
            elseif configOptions.inputType == "dropdown" then
                button.frame = CreateFrame("Frame", nil, configEditor,
                    "EchoRaidToolsConfig_Dropdown")
                button.frame.TopText:SetText(configOptions.name)
                button.frame.TopText:Show()
                button.frame:SetWidth(200)



                local static = type(configOptions.dropdownOptions) == "table" and true or false
                local function updateDropDown()
                    local items = {
                    }
                    local dropdownOptions = static and configOptions.dropdownOptions or
                        configOptions.dropdownOptions()
                    for index, info in ipairs(dropdownOptions) do
                        table.insert(items,
                            {
                                icon = nil,
                                label = info.label,
                                value = info.value,
                                onclick = function()
                                    setField(configOptions.field, info.value)
                                    updateConfigButtons()
                                end,
                                statusbar = info.statusbar,
                                font = info.font,
                            })
                    end
                    button.frame:SetupSelections(items)
                end
                updateDropDown()
                button.update = function()
                    if static == false then
                        updateDropDown()
                    end
                    local stateSetting = getField(configOptions.field)
                    button.frame:UpdateSelection(stateSetting)
                end
            elseif configOptions.inputType == "multinumber" then
                button.frame = CreateFrame("Frame", nil, configEditor, "EchoRaidToolsConfig_EditBox")
                button.frame.TopText:SetText(configOptions.name)
                button.frame.TopText:Show()
                button.frame:SetWidth(200)
                button.frame.Editbox:SetMaxLetters(9999)
                button.update = function()
                    local editBoxText = ""
                    local field = getField(configOptions.field)
                    if field and type(field) == "table" then
                        for index, number in ipairs(field) do
                            editBoxText = editBoxText .. number .. ", "
                        end
                    end
                    button.frame.Editbox:SetText(editBoxText)
                end



                button.frame.Editbox:SetScript("OnTextChanged", function()
                    local text = button.frame.Editbox:GetText()

                    local fieldValue = {}

                    for num in text:gmatch("%d+") do
                        table.insert(fieldValue, tonumber(num))
                    end



                    setField(configOptions.field, #fieldValue > 0 and fieldValue or nil)
                end)

                button.yOffset = button.yOffset + 5
            elseif configOptions.inputType == "number" then
                button.frame = CreateFrame("Frame", nil, configEditor, "EchoRaidToolsConfig_EditBox")
                button.frame.TopText:SetText(configOptions.name)
                button.frame.TopText:Show()
                button.frame:SetWidth(110)
                button.frame.Editbox:SetMaxLetters(12)
                button.frame.Editbox:SetNumeric(true)
                button.update = function()
                    button.frame.Editbox:SetText(getField(configOptions.field) or "")
                end

                button.frame.Editbox:SetScript("OnTextChanged", function()
                    local text = button.frame.Editbox:GetText()
                    setField(configOptions.field, text ~= "" and tonumber(text) or nil)
                    updateConfigButtons()
                end)

                button.yOffset = button.yOffset + 5
            elseif configOptions.inputType == "colorpicker" then
                button.frame = CreateFrame("Button", nil, configEditor, "EchoRaidToolsConfig_NewColorPicker")
                --colorpicker.tooltip = "The color of the notification."

                button.frame:SetupInteraction(function(...)
                    setField(configOptions.field, { ... })
                end, 1, 1, 1, 1, nil, nil, true)
                button.update = function()
                    local stateSetting = getField(configOptions.field)
                    button.frame:SetRGBA(
                        stateSetting[1],
                        stateSetting[2],
                        stateSetting[3],
                        stateSetting[4])
                end
                button.frame.TopText:SetText(configOptions.name)
                button.frame.TopText:Show()
            elseif configOptions.inputType == "checkbox" then
                button.frame = CreateFrame("CheckButton", nil, configEditor, "EchoRaidToolsConfig_CheckButton")
                button.frame:SetSize(25, 25)
                --button.frame.CheckButton:SetSize(25, 25)
                button.frame.CheckButton:SetScript("OnClick", function(self)
                    setField(configOptions.field, self:GetChecked())
                    updateConfigButtons()
                end)

                button.update = function() button.frame.CheckButton:SetChecked(getField(configOptions.field)) end
                button.frame.TopText:SetText(configOptions.name)
                button.frame.TopText:Show()

                button.yOffset = button.yOffset + 5
            elseif configOptions.inputType == "slider" then
                button.frame = CreateFrame("FRAME", nil, configEditor, "EchoRaidTools_BetterSlider")
                --button.frame.Slider.tooltip = "Set the Border's Size"
                button.frame:Show()
                --button.frame.ValueEditbox.tooltip = "Set the Border's Size"
                button.frame:SetSize(200, 32)
                button.frame.TopText:SetText(configOptions.name)
                button.frame.TopText:Show()
                --button.frame:UpdateValues()
                local boundaries = configOptions.boundaries
                button.frame:Setup(
                    boundaries[1], boundaries[2], boundaries[3],
                    function() return getField(configOptions.field) end,
                    function(value)
                        setField(configOptions.field, value)
                    end, boundaries[1], boundaries[2])
                button.update = function() button.frame:UpdateValues() end
            elseif configOptions.inputType == "divider" then

            else
                print("type missing", configOptions.inputType)
            end

            table.insert(configEditor.configButtons, button)
        end
    end


    local columns = 4
    local xSize = 220
    local ySize = 80
    local i = 0
    for _, button in ipairs(configEditor.configButtons) do
        if button.condition() then
            button.update()
            button.frame:Show()
            local row = math.floor(i / columns)
            local column = i - (row * columns)
            local xPosition = (xSize * column)
            local yPosition = (ySize * row)


            button.frame:SetPoint("TOPLEFT", configEditor, "TOPLEFT", xPosition + button.xOffset,
                -(yPosition + button.yOffset))

            i = i + 1
        else
            button.frame:Hide()
        end
        --read fields from state, set shown/hidden depending on the condition function
    end
end




function getModConfigEditor()
    if editor then return editor end
    local mainEditor = config:GetUserEditor()
    if not mainEditor then return end



    local window = CreateFrame("FRAME", nil, mainEditor, "EchoRaidToolsMainConfigPanel_NoScroll")
    window:SetPoint("TOPLEFT", mainEditor.Header, "BOTTOMLEFT", 15, -15)
    window:SetPoint("BOTTOMRIGHT")
    local acceptButton = CreateFrame("FRAME", nil, window, "EchoRaidToolsConfig_Button")
    acceptButton.Button:SetText("Accept")
    acceptButton.Button:SetScript("OnClick", function()
        config.closeModConfigEditor(true)
    end)
    acceptButton:SetSize(150, 30)
    acceptButton:SetPoint("BOTTOMRIGHT", window, "BOTTOMRIGHT", -20, 5)
    acceptButton:Show()
    local cancelButton = CreateFrame("FRAME", nil, window, "EchoRaidToolsConfig_Button")
    cancelButton.Button:SetText("Cancel")
    cancelButton.Button:SetScript("OnClick", function()
        config.closeModConfigEditor(false)
    end)
    cancelButton:SetSize(150, 30)
    cancelButton:SetPoint("RIGHT", acceptButton, "LEFT", -10, 0)
    cancelButton:Show()


    local loadConditionButton = CreateFrame("FRAME", nil, window, "EchoRaidToolsConfig_Button")
    loadConditionButton.Button:SetText("Load Conditions")
    loadConditionButton.Button:SetScript("OnClick", function()
        openLoadConditionEditor()
    end)
    loadConditionButton:SetSize(150, 30)
    loadConditionButton:SetPoint("BOTTOMLEFT", window, "BOTTOMLEFT", 5, 5)
    loadConditionButton:Show()


    editor = window

    return window
end

function config.openModConfigEditor(mod)
    if mod then
        currentlyConfiguredMod = module:DeepCopy(mod)
    else
        currentlyConfiguredMod = {}
        module:applyDefaultFieldsToState(currentlyConfiguredMod)
        --this should never be a admin mod,  right???

        --fresh
    end
    currentlyConfiguredModUnaltered = module:DeepCopy(currentlyConfiguredMod)
    updateTempModDisplay()
    editedFields = {}

    if not mod then
        setField({ "index" }, #module:GetUserMods() + 1)
    end
    getModConfigEditor():Show()
    updateConfigButtons()
    local mainEditor = config:GetUserEditor()
    mainEditor.panel.tabControl:Hide()
end

function config.closeModConfigEditor(shouldSave)
    getModConfigEditor():Hide()

    if shouldSave then
        for _, fieldTable in pairs(editedFields) do
            local value = getField(fieldTable)
            if not doesFieldMatchUnalteredState(fieldTable) then
                module:SaveModField(currentlyConfiguredMod, fieldTable, value)
            end
        end
    end
    module:ReloadModFromStorage(currentlyConfiguredMod.identifier)
    currentlyConfiguredMod = nil

    local mainEditor = config:GetUserEditor()
    mainEditor.panel.tabControl:Show()
end

function openLoadConditionEditor()
    if not loadConditionEditor then
        loadConditionEditor = CreateFrame("Frame", nil, config:GetUserEditor(), "EchoRaidToolsMainConfigPanel_NoScroll")
        loadConditionEditor:Hide()
        loadConditionEditor:SetAllPoints()

        local closeButton = CreateFrame("FRAME", nil, loadConditionEditor, "EchoRaidToolsConfig_Button")
        closeButton.Button:SetText("Close")
        closeButton.Button:SetScript("OnClick", function()
            loadConditionEditor:Hide()
            getModConfigEditor():Show()
        end)

        closeButton:SetSize(150, 30)
        closeButton:SetPoint("BOTTOMLEFT", loadConditionEditor, "BOTTOMLEFT", 20, 5)
        closeButton:Show()
        loadConditionEditor.closeButton = closeButton


        local specConditionCheckbox = CreateFrame("CheckButton", nil, loadConditionEditor,
            "EchoRaidToolsConfig_CheckButton")

        specConditionCheckbox:SetSize(25, 25)
        specConditionCheckbox.TopText:SetText("Specialization")
        specConditionCheckbox.TopText:Show()
        specConditionCheckbox:SetPoint("TOPLEFT", loadConditionEditor, "TOPLEFT", 10, -80)
        loadConditionEditor.specConditionCheckbox = specConditionCheckbox



        local specDropdown = CreateFrame("FRAME", nil, loadConditionEditor, "EchoRaidToolsConfig_Dropdown")

        local function onSpecCheckboxUpdate()
            local checked = getField({ "loadConditions", "spec", "enabled" })
            specConditionCheckbox.CheckButton:SetChecked(checked)
            if checked then
                specDropdown:Enable()
            else
                specDropdown:Disable()
            end
        end

        specConditionCheckbox.CheckButton:SetScript("OnClick", function(self)
            local isChecked = self:GetChecked()
            setField({ "loadConditions", "spec", "enabled" }, isChecked)
            onSpecCheckboxUpdate()
        end)






        specDropdown:SetPoint("TOPLEFT", specConditionCheckbox, "BOTTOMLEFT", 0, 0)
        specDropdown:SetSize(200, 38)

        local function updateSpecDropdown()
            local specs = {
                togglesEnabled = true,
            }

            for i, spec in ipairs(module.Constants.specs) do
                table.insert(specs, {
                    --icon = spec.specIcon,
                    label = string.format("%s %s", spec.specName, spec.className),
                    value = spec.specID,
                    onclick = function(button, down, value, self)
                        local newChecked = self.CheckButton:GetChecked()
                        setField({ "loadConditions", "spec", "specs", spec.specID }, newChecked)
                    end,
                    toggle = function(checked, button, down, value)
                        setField({ "loadConditions", "spec", "specs", spec.specID }, checked)
                    end,
                    checked = getField({ "loadConditions", "spec", "specs", spec.specID })
                })
            end
            specDropdown:SetupSelections(specs, 0)
        end


        loadConditionEditor.specDropdown = specDropdown

        local levelConditionCheckbox = CreateFrame("CheckButton", nil, loadConditionEditor,
            "EchoRaidToolsConfig_CheckButton")
        levelConditionCheckbox:SetSize(25, 25)
        levelConditionCheckbox.TopText:SetText("Level")
        levelConditionCheckbox.TopText:Show()
        levelConditionCheckbox:SetPoint("TOPLEFT", loadConditionEditor, "TOPLEFT", 250, -80)
        loadConditionEditor.levelConditionCheckbox = levelConditionCheckbox





        local levelMaxConditionSlider = CreateFrame("FRAME", nil, loadConditionEditor, "EchoRaidTools_BetterSlider")
        local levelMinConditionSlider = CreateFrame("FRAME", nil, loadConditionEditor, "EchoRaidTools_BetterSlider")
        local function onLevelCheckboxUpdate()
            local checked = getField({ "loadConditions", "level", "enabled" })
            levelConditionCheckbox.CheckButton:SetChecked(checked)
            if checked then
                levelMaxConditionSlider:Enable()
                levelMinConditionSlider:Enable()
            else
                levelMaxConditionSlider:Disable()
                levelMinConditionSlider:Disable()
            end
        end

        levelConditionCheckbox.CheckButton:SetScript("OnClick", function(self)
            setField({ "loadConditions", "level", "enabled" }, self:GetChecked())
            onLevelCheckboxUpdate()
        end)


        local boundaries = { 1, GetMaxPlayerLevel() + 10, 1 }
        levelMaxConditionSlider:SetPoint("TOPLEFT", levelConditionCheckbox, "BOTTOMLEFT", 0, -10)
        levelMaxConditionSlider:SetSize(200, 32)
        levelMaxConditionSlider.TopText:SetText("Max")
        levelMaxConditionSlider.TopText:ClearAllPoints()
        levelMaxConditionSlider.TopText:SetPoint("BOTTOM", levelMaxConditionSlider, "TOP")
        levelMaxConditionSlider.TopText:Show()

        levelMaxConditionSlider:Setup(
            boundaries[1], boundaries[2], boundaries[3],
            function() return getField({ "loadConditions", "level", "max" }) or boundaries[1] end,
            function(value)
                setField({ "loadConditions", "level", "max" }, value)
            end, boundaries[1], boundaries[2])


        levelMinConditionSlider:SetPoint("TOPLEFT", levelMaxConditionSlider, "BOTTOMLEFT", 0, -10)
        levelMinConditionSlider:SetSize(200, 32)
        levelMinConditionSlider.TopText:SetText("Min")
        levelMinConditionSlider.TopText:ClearAllPoints()
        levelMinConditionSlider.TopText:SetPoint("BOTTOM", levelMinConditionSlider, "TOP")
        levelMinConditionSlider.TopText:Show()
        levelMinConditionSlider:Setup(
            boundaries[1], boundaries[2], boundaries[3],
            function() return getField({ "loadConditions", "level", "min" }) or boundaries[1] end,
            function(value)
                setField({ "loadConditions", "level", "min" }, value)
            end, boundaries[1], boundaries[2])




        loadConditionEditor:SetScript("OnShow", function()
            if not getField({ "loadConditions", "level", "max" }) then
                setField({ "loadConditions", "level", "max" }, UnitLevel("player"))
            end
            if not getField({ "loadConditions", "level", "min" }) then
                setField({ "loadConditions", "level", "min" }, 1)
            end
            if not getField({ "loadConditions", "spec", "specs" }) then
                setField({ "loadConditions", "spec", "specs" }, {})
            end
            updateSpecDropdown()
            onSpecCheckboxUpdate()
            onLevelCheckboxUpdate()

            levelMaxConditionSlider:UpdateValues()
            levelMinConditionSlider:UpdateValues()
        end)
    end



    getModConfigEditor():Hide()
    loadConditionEditor:Show()
end
