local addon, ns = ...

-- ============================================================
--  rOrbs – ColorPicker compatibility layer
--  Works with Legacy (Classic) and Dragonflight/TWW (10.x+) API
-- ============================================================

local ColorPickerHelper = {}
ns.ColorPickerHelper = ColorPickerHelper

function ColorPickerHelper:Open(params)
    local r    = params.r or 1
    local g    = params.g or 1
    local b    = params.b or 1

    local cpf = ColorPickerFrame
    if not cpf then return end

    -- TWW / Dragonflight 10.0+ API.
    -- Blizzard has shipped both names across patch versions; prefer
    -- SetupColorPickerAndOpen (original DF name) but fall back to
    -- SetupColorPickerAndShow (TWW rename, also used by nCastBar).
    -- SetupColorPickerAndOpen/Show fully reinitialises the frame on its own –
    -- we must NOT call Hide() first because Blizzard's OnHide fires the
    -- old cancelFunc, which would overwrite the DB with stale colours.
    local setupFn = cpf.SetupColorPickerAndOpen or cpf.SetupColorPickerAndShow
    if setupFn then
        local info = {
            swatchFunc = function()
                local nr, ng, nb = ColorPickerFrame:GetColorRGB()
                if params.onChange then params.onChange(nr, ng, nb) end
            end,
            cancelFunc = function(prev)
                -- onChange updates DB/orbs live; onCancel must not read DB to "revert"
                -- (it would see the picked colour). Restore via previous RGB first.
                local pr, pg, pb = r, g, b
                if type(prev) == "table" and prev.r ~= nil and prev.g ~= nil and prev.b ~= nil then
                    pr, pg, pb = prev.r, prev.g, prev.b
                end
                if params.onChange then
                    params.onChange(pr, pg, pb)
                end
                if params.onCancel then
                    params.onCancel(prev)
                end
            end,
            hasOpacity = false,
            r = r, g = g, b = b,
        }
        setupFn(cpf, info)
        return
    end

    -- ---- Legacy API (Classic / pre-Dragonflight) ----
    -- Blizzard still calls self.swatchFunc(), so we must set it on the frame.
    local origR, origG, origB = r, g, b

    local function liveUpdate()
        local nr, ng, nb = cpf:GetColorRGB()
        if params.onChange then params.onChange(nr, ng, nb) end
    end

    cpf.func        = liveUpdate
    cpf.swatchFunc  = liveUpdate
    cpf.opacityFunc = nil
    cpf.hasOpacity  = false
    cpf.opacity     = nil

    cpf.cancelFunc = function()
        if params.onChange then
            params.onChange(origR, origG, origB)
        end
        if params.onCancel then
            params.onCancel()
        end
    end

    -- On legacy, hide first to reset thumb state.
    -- Temporarily nil out cancelFunc so OnHide doesn't fire it.
    if cpf:IsShown() then
        local savedCancel = cpf.cancelFunc
        cpf.cancelFunc = nil
        cpf:Hide()
        cpf.cancelFunc = savedCancel
    end
    if cpf.SetColorRGB then cpf:SetColorRGB(r, g, b) end
    cpf:Show()
end
