local function RX(x) return x * (ScrW() / 1920) end
local function RY(y) return y * (ScrH() / 1080) end

function DrawDisc(x, y, iRadius, iProgress, iOffset)
        iX = iX or 0
        iY = iY or 0
    iOffset = iOffset or 0
    iRadius = iRadius or 25
    iProgress = iProgress or 360

    local tDisc = {
        { x = x, y = y }
    }
    
    for i = 0, iProgress do
        local iRad = math.rad(-i + iOffset)
        table.insert(tDisc, { x = x + math.sin(iRad) * iRadius, y = y + math.cos(iRad) * iRadius })
    end

    table.insert(tDisc, { x = x, y = y })

    draw.NoTexture()
    surface.DrawPoly(tDisc)
end

local function DrawVerticalDisc(x, y, outerR, innerR, progress)
    progress = math.Clamp(progress, 0, 100)
    if progress <= 0 then return end

    local segments = 128
    local topY    = y - outerR
    local bottomY = y + outerR
    local fillY   = bottomY - (progress / 100) * (outerR * 2)

    local points = {}

    for i = 0, segments do
        local iRad = math.rad((i / segments) * 360 - 90)
        local px   = x + math.cos(iRad) * outerR
        local py   = y + math.sin(iRad) * outerR

        if py >= fillY then
            points[#points + 1] = { x = px, y = py }
        end
    end

    if #points < 3 then return end

    local dy = math.Clamp(fillY - y, -outerR, outerR)
    local dx = math.sqrt(outerR * outerR - dy * dy)

    table.insert(points, 1, { x = x - dx, y = fillY })
    table.insert(points,    { x = x + dx, y = fillY })

    draw.NoTexture()
    surface.DrawPoly(points)
    DrawDisc(x, y, innerR, 360, 0)
end

local materials = {
    heart = Material("xyoss/hud/heartIcon.png"),
    hunger = Material("xyoss/hud/hungerIcon.png"),
    job = Material("xyoss/hud/jobIcon.png"),
    money = Material("xyoss/hud/moneyIcon.png"),
    name = Material("xyoss/hud/nameIcon.png"),
    shield = Material("xyoss/hud/shieldIcon.png"),
}

local displayMoney = 0
hook.Add("DarkRPVarChanged", "bladw_HUD_money", function(ply, var, oldVal, newVal)
    if ply == LocalPlayer() and var == "money" then
        displayMoney = newVal
    end
end)

hook.Add("HUDPaint", "bladw_HUD", function()
    local ply = LocalPlayer()
    local x, y = RX(100), RY(830)

    local name   = ply:Nick()
    local job    = ply:getDarkRPVar("job") or "Unknown"
    local health = ply:Health()
    local armor  = ply:Armor()
    local hunger = ply:getDarkRPVar("Energy") or 100

    -- health
    surface.SetDrawColor(255, 63, 63)
    DrawVerticalDisc(x, y + RY(65), RX(40), RX(30), health)
    surface.SetDrawColor(50, 50, 50, 250)
    DrawDisc(x, y + RY(65), RX(35), 360)
    -- armor 
    surface.SetDrawColor(81, 136, 255)
    DrawVerticalDisc(x + RX(90), y + RY(65), RX(40), RX(30), hunger)
    surface.SetDrawColor(50, 50, 50, 250)
    DrawDisc(x + RX(90), y + RY(65), RX(35), 360)

        -- hunger
    surface.SetDrawColor(255, 166, 0)
    DrawVerticalDisc(x + RX(180), y + RY(65), RX(40), RX(30), hunger)
    surface.SetDrawColor(50, 50, 50, 250)
    DrawDisc(x + RX(180), y + RY(65), RX(35), 360)
    
    -- icônes
    surface.SetDrawColor(255,255,255,255)
    surface.SetMaterial(materials.heart)
    surface.DrawTexturedRect(x - RX(12.5), y + RY(65) - RY(12.5), RX(25), RY(25))
    surface.DrawTexturedRect(x - RX(12.5), y + RY(50), RX(25), RY(25))
    surface.SetMaterial(materials.shield)
    surface.DrawTexturedRect(x + RX(90) - RX(12.5), y + RY(50), RX(25), RY(27))
    surface.SetMaterial(materials.hunger)
    surface.DrawTexturedRect(x + RX(180) - RX(15), y + RY(50), RX(25), RY(30))

    -- textes
    surface.SetDrawColor(255, 255, 255)
    surface.SetMaterial(materials.name)
    surface.DrawTexturedRect(x - RX(50), y + RY(120), RX(22), RY(27))
    draw.SimpleText(name, "bladw_text", x + RX(5), y + RY(135), Color(255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)

    surface.SetMaterial(materials.job)
    surface.DrawTexturedRect(x - RX(60), y + RY(160), RX(40), RY(40))
    draw.SimpleText(job, "bladw_text", x + RX(5), y + RY(180), Color(255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)

    surface.SetMaterial(materials.money)
    surface.DrawTexturedRect(x - RX(50), y + RY(215), RX(20), RY(20))
    draw.SimpleText(displayMoney .. " €", "bladw_text", x + RX(5), y + RY(225), Color(85, 255, 116), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
end)

local hideElement = {
    ["CHudHealth"] = true,
    ["CHudBattery"] = true,
    ["CHudAmmo"] = true,
    ["CHudSecondaryAmmo"] = true,
    ["CHudCrosshair"] = true,
    ["CHudWeaponSelection"] = true,
    ["DarkRP_HUD"] = true,
    ["DarkRP_EntityDisplay"] = true,
    ["DarkRP_LocalPlayerHUD"] = true,
    ["DarkRP_Hungermod"] = true,
    ["DarkRP_Agenda"] = true,
    ["DarkRP_LockdownHUD"] = true,
    ["DarkRP_ArrestedHUD"] = true,
    ["DarkRP_ChatReceivers"] = true,
}

hook.Add("HUDShouldDraw", "bladw_HUD_ShouldDraw", function(name)
    if hideElement[name] then return false end
end)
