-- Garry's Mod Binder - Radial HUD
local IsRadialOpen = false
local SelectedSlot = 0

local function GetActiveProfile()
    for _, p in ipairs(Binder.Profiles or {}) do
        if p.Active then return p end
    end
    return nil
end

hook.Add("HUDPaint", "Binder_DrawRadial", function()
    if not IsRadialOpen then return end
    
    local profile = GetActiveProfile()
    if not profile or not profile.Radial then return end
    
    local cx, cy = ScrW() / 2, ScrH() / 2
    local radius = 150
    
    -- Draw Center
    draw.NoTexture()
    surface.SetDrawColor(Color(30, 30, 30, 240))
    surface.DrawCircle(cx, cy, 50, 255, 255, 255, 255)
    draw.SimpleText(Binder.GetPhrase("radial_center"), "Binder_Main", cx, cy, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
    
    local mouseX, mouseY = gui.MousePos()
    if mouseX == 0 and mouseY == 0 then
        mouseX, mouseY = cx, cy -- fallback if not capturing properly
    end
    
    local dist = math.Dist(cx, cy, mouseX, mouseY)
    local angle = math.deg(math.atan2(mouseY - cy, mouseX - cx)) + 90
    if angle < 0 then angle = angle + 360 end
    
    -- Determine selected segment based on angle (8 segments, 45 deg each)
    if dist > 50 then
        SelectedSlot = math.floor((angle + 22.5) / 45) + 1
        if SelectedSlot > 8 then SelectedSlot = 1 end
    else
        SelectedSlot = 0
    end

    -- Draw segments
    for i = 1, 8 do
        local slotData = profile.Radial[i]
        if slotData then
            local segAngle = (i - 1) * 45 - 90
            local rad = math.rad(segAngle)
            local lx, ly = cx + math.cos(rad) * radius, cy + math.sin(rad) * radius
            
            local boxColor = (SelectedSlot == i) and Binder.Config.AccentColor or Color(40, 40, 45, 240)
            
            draw.RoundedBox(8, lx - 40, ly - 20, 80, 40, boxColor)
            draw.SimpleText(slotData.Label, "Binder_Main", lx, ly, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
    end
    
    -- Draw cursor line
    surface.SetDrawColor(255, 255, 255, 50)
    surface.DrawLine(cx, cy, mouseX, mouseY)
end)

local PrevRadialState = false

hook.Add("Think", "Binder_RadialThink", function()
    local cvar = GetConVar("binder_radial_key")
    local radialKey = cvar and cvar:GetInt() or Binder.Config.DefaultRadialKey
    
    local isDown = input.IsButtonDown(radialKey)
    local blockNewInput = gui.IsConsoleVisible() or gui.IsGameUIVisible() or (vgui.GetKeyboardFocus() ~= nil) or (LocalPlayer() and LocalPlayer():IsValid() and LocalPlayer():IsTyping())
    
    if isDown and not PrevRadialState then
        if not blockNewInput then
            PrevRadialState = true
            IsRadialOpen = true
            gui.EnableScreenClicker(true)
            SelectedSlot = 0
        end
    elseif not isDown and PrevRadialState then
        PrevRadialState = false
        if IsRadialOpen then
            IsRadialOpen = false
            gui.EnableScreenClicker(false)
            
            if SelectedSlot > 0 then
                local profile = GetActiveProfile()
                if profile and profile.Radial and profile.Radial[SelectedSlot] then
                    LocalPlayer():ConCommand(profile.Radial[SelectedSlot].Command)
                end
            end
        end
    end
end)
