-- [ PIXEL PROBE ] -----------------------------------------------------------------------------------
-- Dev-only, never packaged: /orbitpixel toggles a hover inspector reporting whether the hovered frame's edges land on the physical pixel grid, its scale chain, and Enforce state.
local _, Orbit = ...

local THROTTLE = 0.1
local ON_GRID_EPS = 0.02
local GREEN, RED, GRAY, GOLD = "|cff33ff66", "|cffff4444", "|cff999999", "|cffffd100"

local probe

local function ShortName(f)
    return f:GetName() or (f.GetDebugName and f:GetDebugName():match("[^%.]+$")) or "(anon)"
end

local function EdgeText(label, localV, effScale, pixelScale)
    local px = localV * effScale / pixelScale
    local dev = px - math.floor(px + 0.5)
    local ok = math.abs(dev) < ON_GRID_EPS
    return string.format("%s%s %.1f%+.2f|r", ok and GREEN or RED, label, px, dev)
end

local function ScaleChain(f)
    local parts = {}
    while f and f ~= UIParent do
        local s = f:GetScale() or 1
        if math.abs(s - 1) > 0.0001 then
            parts[#parts + 1] = string.format("%s ×%.3f", ShortName(f), s)
        end
        f = f:GetParent()
    end
    parts[#parts + 1] = string.format("UIParent ×%.4f", UIParent:GetScale())
    return table.concat(parts, "  ")
end

local function FindHovered()
    local cx, cy = GetCursorPosition()
    local best, bestArea
    local f = EnumerateFrames()
    while f do
        if f ~= probe and f:IsVisible() and f.GetLeft then
            local l, b, w, h = f:GetRect()
            if l and not issecretvalue(l) and w > 0 and h > 0 then
                local s = f:GetEffectiveScale()
                if cx >= l * s and cx <= (l + w) * s and cy >= b * s and cy <= (b + h) * s then
                    local area = w * s * h * s
                    if not bestArea or area < bestArea then
                        best, bestArea = f, area
                    end
                end
            end
        end
        f = EnumerateFrames(f)
    end
    return best
end

local function Describe(f)
    local Pixel = Orbit.Engine.Pixel
    local pixelScale = Pixel:GetScale()
    local s = f:GetEffectiveScale()
    local l, b, w, h = f:GetRect()
    local wPx = w * s / pixelScale
    local hPx = h * s / pixelScale

    local lines = {}
    lines[1] = GOLD .. ShortName(f) .. "|r  " .. GRAY .. string.format("%.1f × %.1f px", wPx, hPx) .. "|r"
    lines[2] = EdgeText("L", l, s, pixelScale) .. "  " .. EdgeText("R", l + w, s, pixelScale)
        .. "  " .. EdgeText("B", b, s, pixelScale) .. "  " .. EdgeText("T", b + h, s, pixelScale)

    local flags = {}
    flags[#flags + 1] = f.OrbitNativeSetWidth and (GREEN .. "Enforced|r") or (GRAY .. "unenforced|r")
    if f.orbitEvenSnap then flags[#flags + 1] = GREEN .. "even-snap|r" end
    local point = f:GetPoint(1)
    if point == "CENTER" and math.floor(wPx + 0.5) % 2 == 1 then
        flags[#flags + 1] = RED .. "CENTER + odd width|r"
    end
    if point then flags[#flags + 1] = GRAY .. point .. "|r" end
    lines[3] = table.concat(flags, "  ")
    lines[4] = GRAY .. ScaleChain(f) .. "|r"
    return table.concat(lines, "\n")
end

local function EnsureProbe()
    if probe then return probe end
    probe = CreateFrame("Frame", "OrbitPixelProbe", UIParent, "BackdropTemplate")
    probe:SetFrameStrata(Orbit.Constants.Strata.Topmost)
    probe:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8x8", edgeFile = "Interface\\Buttons\\WHITE8x8", edgeSize = 1 })
    probe:SetBackdropColor(0, 0, 0, 0.85)
    probe:SetBackdropBorderColor(0.3, 0.7, 1, 1)
    probe:EnableMouse(false)
    probe.text = probe:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    probe.text:SetPoint("TOPLEFT", 8, -8)
    probe.text:SetJustifyH("LEFT")
    probe.elapsed = 0
    probe:SetScript("OnUpdate", function(self, elapsed)
        self.elapsed = self.elapsed + elapsed
        if self.elapsed < THROTTLE then return end
        self.elapsed = 0
        local target = FindHovered()
        if not target then self.text:SetText(GRAY .. "no frame under cursor|r") return end
        self.text:SetText(Describe(target))
        self:SetSize(self.text:GetStringWidth() + 16, self.text:GetStringHeight() + 16)
        local x, y = GetCursorPosition()
        local scale = UIParent:GetEffectiveScale()
        self:ClearAllPoints()
        self:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", x / scale + 24, y / scale + 24)
    end)
    return probe
end

SLASH_ORBITPIXEL1 = "/orbitpixel"
SlashCmdList["ORBITPIXEL"] = function()
    local p = EnsureProbe()
    p:SetShown(not p:IsShown())
    print(GOLD .. "PixelProbe|r " .. (p:IsShown() and "on — hover a frame" or "off"))
end
