local _, PUP = ...

local DEFAULT_HEIGHT = 26

function PUP:CreateButton(parent, title, OnClick)
    local button = CreateFrame("Button", nil, parent)
    local fixedWidth = false

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

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

    button.OnClick = OnClick
    
    button:SetText(" ")
    button:SetText(title)
    button:SetNormalFontObject(PUFont13)
    button:SetHighlightFontObject(PUFont13)
    button:SetDisabledFontObject(PUFont13)
    button:SetScript("OnClick", button.OnClick)

    local function UpdateWidth()
        button:SetSize(button:GetFontString():GetUnboundedStringWidth() + 20, DEFAULT_HEIGHT)
    end

    function button:SetFixedWidth(isFixed)
        fixedWidth = isFixed
    end

    hooksecurefunc(
        button,
        "SetText",
        function()
            if not fixedWidth then
                UpdateWidth()
            end
        end
    )

    hooksecurefunc(
        button,
        "SetNormalFontObject",
        function()
            if not fixedWidth then
                UpdateWidth()
            end
        end
    )

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

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

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

    -- Highlight
    PUP:AddHoverHighlight(button)

    UpdateWidth()
    
    return button
end
