local _, PUP = ...

local SPACING = 4
local DEFAULT_SIZE = 20

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

    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)
    PUP:AddHoverHighlight(checkButton)

    checkButton.OnValueChanged = OnValueChanged

    -- Background
    checkButton.tex = checkButton:CreateTexture(nil, "BACKGROUND")
    checkButton.tex:SetAllPoints()
    checkButton.tex:SetColorTexture(0, 0, 0, 0.5)
    checkButton.tex:SetSnapToPixelGrid(false)
    checkButton.tex:SetTexelSnappingBias(0)

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

    -- Border
    local borderColor = PUP.gs.visual.borderColor

    PUP:AddBorder(checkButton)
    checkButton:SetBorderColor(borderColor.r, borderColor.g, borderColor.b)

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

    checkButton.title:SetFontObject(PUFont13)
    checkButton.title:SetText(string.format("|cFFFFCC00%s|r", title))

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

    -- Check
    checkButton.checkmark = checkButton:CreateTexture(nil, "OVERLAY")
    checkButton.checkmark:SetAllPoints()
    checkButton.checkmark:SetAtlas("common-icon-checkmark-yellow")
    checkButton.checkmark:SetSnapToPixelGrid(false)
    checkButton.checkmark:SetTexelSnappingBias(0)
    checkButton.checkmark:Hide()

    function checkButton:SetChecked(checked)
        isChecked = checked

        checkButton.checkmark:SetShown(checked)
    end

    function checkButton:IsChecked()
        return isChecked
    end

    function checkButton:GetTotalWidth()
        return checkButton:GetWidth() + SPACING + checkButton.title:GetUnboundedStringWidth()
    end

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

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

            checkButton.OnValueChanged(checked)
        end
    )

    return checkButton
end