local _, addonTable = ...
local WipedQoL = addonTable.WipedQoL
local UI = addonTable.UI
local TRANSLATE = addonTable.TRANSLATE or function(msg) return msg end

local MODIFIER_OPTIONS = {
    { value = "SHIFT", label = "Shift" },
    { value = "CTRL", label = "Ctrl" },
    { value = "ALT", label = "Alt" },
}

WipedQoL:RegisterTabBuilder("questing", function(addon, parent)
    local frame = CreateFrame("Frame", nil, parent)
    local settings = addon:GetQuestAutomationSettings()
    local modifierLabel
    local modifierDropdown
    local automationSection = UI.CreateSection(
        frame,
        TRANSLATE("Automatically handle quests"),
        TRANSLATE("Automatically accepts and turns in quests, and skips NPC dialogue. Hold the selected key to pause.")
    )
    automationSection:SetPoint("TOPLEFT", frame, "TOPLEFT", 14, -14)
    automationSection:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -14, -14)
    automationSection:SetHeight(104)

    local function RefreshModifierState()
        local enabled = settings.enabled == true

        if modifierLabel then
            if enabled then
                modifierLabel:SetTextColor(0.78, 0.78, 0.78)
            else
                modifierLabel:SetTextColor(0.38, 0.38, 0.38)
            end
        end

        if modifierDropdown then
            modifierDropdown:SetAlpha(enabled and 1 or 0.45)
            if modifierDropdown._wqolClickOverlay then
                modifierDropdown._wqolClickOverlay:EnableMouse(enabled)
            end
        end
    end

    local automationCheckbox = UI.CreateSectionToggle(automationSection, function()
        return settings.enabled == true
    end, function(value)
        settings.enabled = value and true or false
        RefreshModifierState()
    end)

    modifierLabel = UI.CreateLabel(automationSection, TRANSLATE("Temporarily disable"), 12)
    modifierLabel:SetPoint("TOPLEFT", automationSection.description, "BOTTOMLEFT", 0, -10)
    modifierLabel:SetTextColor(0.78, 0.78, 0.78)

    modifierDropdown = UI.CreateDropdown(automationSection, 80, MODIFIER_OPTIONS, function()
        return settings.modifier or "SHIFT"
    end, function(value)
        addon:SetQuestAutomationModifier(value)
        settings = addon:GetQuestAutomationSettings()
    end)
    modifierDropdown:SetPoint("LEFT", modifierLabel, "RIGHT", -12, 0)

    RefreshModifierState()

    return frame
end)
