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

local ADDON_COLOR_MODE_OPTIONS = {
    { value = "class", label = "Class color" },
    { value = "custom", label = "Custom color" },
}

local function OpenColorPicker(getColor, setColor)
    if not ColorPickerFrame then
        return
    end

    local originalR, originalG, originalB = getColor()
    local function ApplyColor()
        local r, g, b = ColorPickerFrame:GetColorRGB()
        setColor(r, g, b)
    end
    local function RestoreColor()
        setColor(originalR, originalG, originalB)
    end

    ColorPickerFrame:Hide()
    ColorPickerFrame:SetFrameStrata("FULLSCREEN_DIALOG")
    ColorPickerFrame:SetClampedToScreen(true)
    if ColorPickerFrame.SetupColorPickerAndShow then
        ColorPickerFrame:SetupColorPickerAndShow({
            r = originalR,
            g = originalG,
            b = originalB,
            hasOpacity = false,
            swatchFunc = ApplyColor,
            cancelFunc = RestoreColor,
        })
    else
        ColorPickerFrame.hasOpacity = false
        ColorPickerFrame.func = ApplyColor
        ColorPickerFrame.cancelFunc = RestoreColor
        ColorPickerFrame:SetColorRGB(originalR, originalG, originalB)
        ColorPickerFrame:Show()
    end
end

WipedQoL:RegisterTabBuilder("addon", function(addon, parent)
    local frame = CreateFrame("Frame", nil, parent)
    local minimapSection = UI.CreateSection(
        frame,
        TRANSLATE("Show minimap button"),
        TRANSLATE("Shows or hides the WipedQoL button on the minimap.")
    )
    minimapSection:SetPoint("TOPLEFT", frame, "TOPLEFT", 14, -14)
    minimapSection:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -14, -14)
    minimapSection:SetHeight(58)

    local minimapToggle = UI.CreateSectionToggle(minimapSection, function()
        return not addon:IsMinimapHidden()
    end, function(value)
        addon:SetMinimapHidden(not value)
    end)

    local colorSection = UI.CreateSection(
        frame,
        TRANSLATE("Addon color"),
        TRANSLATE("Sets the accent color used throughout WipedQoL.")
    )
    colorSection:SetPoint("TOPLEFT", minimapSection, "BOTTOMLEFT", 0, -12)
    colorSection:SetPoint("TOPRIGHT", minimapSection, "BOTTOMRIGHT", 0, -12)
    colorSection:SetHeight(76)

    local colorModeLabel = UI.CreateLabel(colorSection, string.format("%s:", TRANSLATE("Color source")), 11)
    colorModeLabel:SetPoint("TOPLEFT", colorSection.description, "BOTTOMLEFT", 0, -10)
    colorModeLabel:SetTextColor(0.78, 0.78, 0.78)

    local colorModeDropdown = UI.CreateDropdown(colorSection, 126, ADDON_COLOR_MODE_OPTIONS, function()
        return addon:GetAddonColorMode()
    end, function(value)
        addon:SetAddonColorMode(value)
    end)
    colorModeDropdown:SetPoint("LEFT", colorModeLabel, "RIGHT", -10, 0)

    local colorLabel = UI.CreateLabel(colorSection, string.format("%s:", TRANSLATE("Color")), 11)
    colorLabel:SetPoint("LEFT", colorModeDropdown, "RIGHT", 12, 0)
    colorLabel:SetTextColor(0.78, 0.78, 0.78)

    local colorButton = CreateFrame("Button", nil, colorSection, "BackdropTemplate")
    colorButton:SetSize(34, 22)
    colorButton:SetPoint("LEFT", colorLabel, "RIGHT", 8, 0)
    UI.SkinFrame(colorButton, { light = true })
    colorButton.swatch = colorButton:CreateTexture(nil, "ARTWORK")
    colorButton.swatch:SetPoint("TOPLEFT", colorButton, "TOPLEFT", 3, -3)
    colorButton.swatch:SetPoint("BOTTOMRIGHT", colorButton, "BOTTOMRIGHT", -3, 3)
    colorButton.swatch:SetTexture("Interface\\Buttons\\WHITE8X8")

    function addon:RefreshAddonColorUI()
        local r, g, b = addon:GetAddonColor()
        local customR, customG, customB = addon:GetAddonCustomColor()
        local mode = addon:GetAddonColorMode()
        local customEnabled = mode == "custom"
        local modeText = customEnabled and "Custom color" or "Class color"

        UIDropDownMenu_SetText(colorModeDropdown, TRANSLATE(modeText))
        colorLabel:SetTextColor(customEnabled and 0.78 or 0.38, customEnabled and 0.78 or 0.38, customEnabled and 0.78 or 0.38)
        colorButton.swatch:SetVertexColor(customR, customG, customB, 1)
        colorButton:SetAlpha(customEnabled and 1 or 0.45)
        colorButton:EnableMouse(customEnabled)
        if minimapToggle.RefreshColor then
            minimapToggle:RefreshColor()
        end

        local mainWindow = addon.mainWindow
        if mainWindow and mainWindow.titleQoL then
            mainWindow.titleQoL:SetTextColor(r, g, b, 1)
        end
        if mainWindow and mainWindow.tabButtons then
            for key, button in pairs(mainWindow.tabButtons) do
                if button and button.Text then
                    if key == addon.activeTab then
                        button.Text:SetTextColor(r, g, b, 1)
                    end
                end
            end
        end
        if mainWindow and mainWindow.tabsBar and mainWindow.tabsBar.wqolActiveIndicatorFrame then
            local texture = mainWindow.tabsBar.wqolActiveIndicatorFrame:GetRegions()
            if texture then
                texture:SetColorTexture(r, g, b, 0.95)
            end
        end
    end

    colorButton:SetScript("OnClick", function()
        OpenColorPicker(
            function()
                return addon:GetAddonCustomColor()
            end,
            function(r, g, b)
                addon:SetAddonColor(r, g, b)
            end
        )
    end)

    addon:RefreshAddonColorUI()

    local changelogSection = UI.CreateSection(
        frame,
        TRANSLATE("Changelog"),
        TRANSLATE("Shows the latest changes to WipedQoL.")
    )
    changelogSection:SetPoint("TOPLEFT", colorSection, "BOTTOMLEFT", 0, -12)
    changelogSection:SetPoint("TOPRIGHT", colorSection, "BOTTOMRIGHT", 0, -12)
    changelogSection:SetPoint("BOTTOM", frame, "BOTTOM", 0, 14)

    local scrollFrame = CreateFrame("ScrollFrame", nil, changelogSection, "UIPanelScrollFrameTemplate")
    scrollFrame:SetPoint("TOPLEFT", changelogSection.description, "BOTTOMLEFT", 0, -10)
    scrollFrame:SetPoint("BOTTOMRIGHT", changelogSection, "BOTTOMRIGHT", -22, 10)

    local content = CreateFrame("Frame", nil, scrollFrame)
    content:SetSize(1, 1)
    scrollFrame:SetScrollChild(content)

    local changelogText = content:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    changelogText:SetPoint("TOPLEFT", content, "TOPLEFT", 0, 0)
    changelogText:SetPoint("TOPRIGHT", content, "TOPRIGHT", 0, 0)
    changelogText:SetJustifyH("LEFT")
    changelogText:SetJustifyV("TOP")
    changelogText:SetTextColor(0.86, 0.86, 0.86, 1)
    changelogText:SetSpacing(4)
    changelogText:SetNonSpaceWrap(true)
    changelogText:SetWordWrap(true)
    changelogText:SetText(addon.GetChangelogText and addon:GetChangelogText() or "")
    UI.ApplyFont(changelogText, 12, "")

    local function RefreshChangelogSize()
        local width = scrollFrame:GetWidth()
        if width and width > 0 then
            content:SetWidth(width - 8)
            changelogText:SetWidth(width - 8)
        end
        content:SetHeight(math.max(scrollFrame:GetHeight() or 1, (changelogText:GetStringHeight() or 0) + 10))
    end

    scrollFrame:SetScript("OnSizeChanged", RefreshChangelogSize)
    RefreshChangelogSize()

    if scrollFrame.ScrollBar then
        UI.SkinScrollBar(scrollFrame.ScrollBar)
    end

    return frame
end)
