local _, namespace = ...

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

local DEFAULT_HEIGHT = Theme.SIZE_SWATCH

-- A dedicated RGB brightening delta for the swatch hover state -- deliberately its own
-- constant, not borrowed from Theme.ACCENT_A20.a (which is an overlay *opacity*, a different
-- physical quantity; the two happened to share a numeric value but are otherwise unrelated).
-- Clamped per-channel below since additive brightening can overflow 1.0 for bright colors.
local HOVER_BRIGHTEN = 0.2

local function Clamp01(value)
    if value > 1 then return 1 end
    if value < 0 then return 0 end
    return value
end

function namespace:CreateColorPicker(parent, title, OnValueChanged, hasOpacity, labelLeft)
    local colorPicker = CreateFrame("Button", nil, parent)

    local options
    local colorOnOpen = {} -- The chosen color on opening the color picker, so we can revert to it on cancel

    colorPicker:SetSize(DEFAULT_HEIGHT, DEFAULT_HEIGHT)

    function colorPicker:SetColor(r, g, b, a)
        options.r = r
        options.g = g
        options.b = b
        options.opacity = a

        colorPicker.tex:SetColorTexture(r, g, b)

        OnValueChanged(r, g, b, a)
    end

    local function OnColorChanged()
        local r, g, b = ColorPickerFrame:GetColorRGB()
        local a = ColorPickerFrame:GetColorAlpha()

        colorPicker:SetColor(r, g, b, a)
    end

    local function OnCancel()
        colorPicker:SetColor(colorOnOpen.r, colorOnOpen.g, colorOnOpen.b, colorOnOpen.opacity)
    end

    options = {
        swatchFunc = OnColorChanged,
        opacityFunc = OnColorChanged,
        cancelFunc = OnCancel,
        hasOpacity = hasOpacity,
        opacity = 1,
        r = 1,
        g = 1,
        b = 1
    }

    colorPicker:SetScript(
        "OnClick",
        function()
            colorOnOpen = {
                r = options.r,
                g = options.g,
                b = options.b,
                opacity = options.opacity
            }

            ColorPickerFrame:SetupColorPickerAndShow(options)
        end
    )

    colorPicker.OnEnter = function()
        colorPicker.tex:SetColorTexture(
            Clamp01(options.r + HOVER_BRIGHTEN),
            Clamp01(options.g + HOVER_BRIGHTEN),
            Clamp01(options.b + HOVER_BRIGHTEN)
        )
    end

    colorPicker.OnLeave = function()
        colorPicker.tex:SetColorTexture(options.r, options.g, options.b)
    end

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

    -- Texture
    colorPicker.tex = colorPicker:CreateTexture(nil, "OVERLAY")
    colorPicker.tex:SetAllPoints(colorPicker)
    colorPicker.tex:SetColorTexture(options.r, options.g, options.b)
    colorPicker.tex:SetSnapToPixelGrid(false)
    colorPicker.tex:SetTexelSnappingBias(0)

    -- Label
    colorPicker.title = colorPicker:CreateFontString()

    colorPicker.title:SetFontObject(WildThingsFont13)
    colorPicker.title:SetText(title)
    colorPicker.title:SetTextColor(Theme.TEXT_DIM.r, Theme.TEXT_DIM.g, Theme.TEXT_DIM.b, Theme.TEXT_DIM.a)

    if labelLeft then
        colorPicker.title:SetPoint("RIGHT", colorPicker, "LEFT", -4, 0)
    else
        colorPicker.title:SetPoint("LEFT", colorPicker, "RIGHT", 4, 0)
    end

    -- Rounded swatch: the fill needs the mask too, or its square corners would poke out
    -- past the rounded border below.
    WTUI:AddRoundedMask(colorPicker, Theme.RADIUS_SM)
    colorPicker.tex:AddMaskTexture(colorPicker.roundedMask)

    -- Border
    WTUI:AddRoundedBorder(colorPicker, Theme.RADIUS_SM, 1)
    colorPicker:SetBorderColor(Theme.WT_WHITE.r, Theme.WT_WHITE.g, Theme.WT_WHITE.b, Theme.WT_WHITE.a)

    return colorPicker
end
