local _, namespace = ...

local WTUI = namespace.WildThingsUI
local Theme = namespace.Theme

local BORDER_COLOR = Theme.BORDER_STRONG -- unchecked border; UpdateBorderColor swaps to Theme.ACCENT_A70 when checked
local BACKGROUND_COLOR = Theme.SURFACE_2

local SPACING = 4
local DEFAULT_SIZE = Theme.SIZE_CHECKBOX

function namespace:CreateCheckButton(parent, title, OnValueChanged, labelLeft)
    local checkButton = CreateFrame("Button", nil, parent)
    local enabled = true

    checkButton.OnEnter = function() end
    checkButton.OnLeave = function() end

    checkButton:SetScript("OnEnter", function(_self) _self.OnEnter() end)
    checkButton:SetScript("OnLeave", function(_self) _self.OnLeave() end)

    local isChecked = false

    checkButton:SetSize(DEFAULT_SIZE, DEFAULT_SIZE)
    WTUI:AddHoverHighlight(checkButton)

    checkButton.OnValueChanged = OnValueChanged

    -- Background
    checkButton.tex = checkButton:CreateTexture(nil, "BACKGROUND")
    checkButton.tex:SetAllPoints()
    checkButton.tex:SetColorTexture(BACKGROUND_COLOR.r, BACKGROUND_COLOR.g, BACKGROUND_COLOR.b, BACKGROUND_COLOR.a)
    checkButton.tex:SetSnapToPixelGrid(false)
    checkButton.tex:SetTexelSnappingBias(0)

    -- Rounded overlay-clip primitive (ROUND-04/05, spike-002): mask on the base fill only.
    -- The border is NOT suppressed here (Pitfall 8) -- its checked-state color swap
    -- (Theme.ACCENT_A70) is the primary "this is checked" signal and must stay visible.
    WTUI:AddRoundedMask(checkButton, Theme.RADIUS_SM)
    checkButton.tex:AddMaskTexture(checkButton.roundedMask)

    function checkButton:SetBackgroundColor(r, g, b, a)
        checkButton.tex:SetColorTexture(r, g, b, a)
    end

    -- Border (alpha-aware SetBorderColor from the 09-01 Functions.lua enabler); driven by
    -- checked state below via UpdateBorderColor.
    WTUI:AddRoundedBorder(checkButton, Theme.RADIUS_SM, 1)

    local function UpdateBorderColor()
        if isChecked then
            checkButton:SetBorderColor(Theme.ACCENT_A70.r, Theme.ACCENT_A70.g, Theme.ACCENT_A70.b, Theme.ACCENT_A70.a)
        else
            checkButton:SetBorderColor(BORDER_COLOR.r, BORDER_COLOR.g, BORDER_COLOR.b, BORDER_COLOR.a)
        end
    end

    UpdateBorderColor()

    -- Title
    checkButton.title = checkButton:CreateFontString()

    checkButton.title:SetFontObject(WildThingsFont13)
    checkButton.title:SetText(title)
    checkButton.title:SetTextColor(Theme.TEXT.r, Theme.TEXT.g, Theme.TEXT.b, Theme.TEXT.a)

    if labelLeft then
        checkButton.title:SetPoint("RIGHT", checkButton, "LEFT", -SPACING, -1)
    else
        checkButton.title:SetPoint("LEFT", checkButton, "RIGHT", SPACING, -1)
    end

    -- Check — atlas rendered at native aspect, centered, fit inside the DEFAULT_SIZE box
    -- (SetAllPoints previously forced a non-uniform stretch; see checkbox-checkmark-
    -- misanchored.md). Mirrors the atlas-aware sizing precedent in Dropdown.lua (~line 88).
    checkButton.checkmark = checkButton:CreateTexture(nil, "OVERLAY")
    checkButton.checkmark:SetAtlas("common-icon-checkmark-yellow")

    local atlasInfo = C_Texture.GetAtlasInfo("common-icon-checkmark-yellow")
    if atlasInfo and atlasInfo.width and atlasInfo.width > 0 and atlasInfo.height and atlasInfo.height > 0 then
        local scale = math.min(DEFAULT_SIZE / atlasInfo.width, DEFAULT_SIZE / atlasInfo.height)
        checkButton.checkmark:SetSize(atlasInfo.width * scale, atlasInfo.height * scale)
    else
        checkButton.checkmark:SetAtlas("common-icon-checkmark-yellow", true)
    end

    checkButton.checkmark:SetPoint("CENTER")
    checkButton.checkmark:SetVertexColor(Theme.ACCENT.r, Theme.ACCENT.g, Theme.ACCENT.b)
    checkButton.checkmark:SetSnapToPixelGrid(false)
    checkButton.checkmark:SetTexelSnappingBias(0)
    checkButton.checkmark:Hide()

    -- Check-glyph scale-in: shared motion helper (WIDGET-12) — single AnimationGroup built
    -- once at construction (never inside SetChecked/OnClick).
    checkButton.checkAnim = WTUI:AddToggleAnimation(checkButton, checkButton.checkmark, "Scale", {from = 0.1, to = 1, duration = Theme.DUR_IN})

    function checkButton:SetChecked(checked)
        isChecked = checked

        checkButton.checkmark:SetShown(checked)
        UpdateBorderColor()

        if checked then
            checkButton.checkAnim:Play()
        else
            checkButton.checkAnim:Stop()
        end
    end

    function checkButton:IsChecked()
        return isChecked
    end

    -- Returns the total width of the check button + the text + the space in between
    function checkButton:GetTotalWidth()
        return checkButton:GetWidth() + SPACING + checkButton.title:GetUnboundedStringWidth()
    end

    checkButton:SetScript(
        "OnClick",
        function()
            if enabled then
                checkButton:SetChecked(not isChecked)
            end
        end
    )

    hooksecurefunc(
        checkButton,
        "SetChecked",
        function(_, checked, dontRun)
            if dontRun then return end -- To avoid recursion

            checkButton.OnValueChanged(checked)
        end
    )

    -- Enable/disable
    function checkButton:Enable()
        enabled = true

        checkButton.checkmark:SetDesaturated(false)
        checkButton.title:SetTextColor(Theme.TEXT.r, Theme.TEXT.g, Theme.TEXT.b, Theme.TEXT.a)

        WTUI:AddHoverHighlight(checkButton)
    end

    function checkButton:Disable()
        enabled = false

        checkButton.checkmark:SetDesaturated(true)
        checkButton.title:SetTextColor(Theme.TEXT_DISABLED.r, Theme.TEXT_DISABLED.g, Theme.TEXT_DISABLED.b, Theme.TEXT_DISABLED.a)

        WTUI:AddHoverHighlight(checkButton, nil, nil, 0.5, 0.5, 0.5)
    end

    return checkButton
end
