local PLUGIN = PLUGIN

local event_active = false
local event_start_time = 0
local event_zone_active = false
local event_zone_min = nil
local event_zone_max = nil

-- Net Receivers
net.Receive("events_system_notification", function()
    local message = net.ReadString()
    local lines = string.Split(message, "\n")
    for _, line in ipairs(lines) do
        if (string.Trim(line) != "") then
            chat.AddText(Color(255, 100, 100), " [EVENT] ", Color(255, 255, 255), line)
        end
    end
    surface.PlaySound("buttons/button15.wav")
end)

net.Receive("events_system_hud_update", function()
    event_active = net.ReadBool()
    event_start_time = net.ReadInt(32)
end)

net.Receive("events_system_zone_update", function()
    event_zone_active = net.ReadBool()
    if (event_zone_active) then
        event_zone_min = net.ReadVector()
        event_zone_max = net.ReadVector()
    else
        event_zone_min = nil
        event_zone_max = nil
    end
end)

-- Fonts
surface.CreateFont("EventHUD_Title", { font = "Roboto", size = 24, weight = 700, antialias = true, shadow = true })
surface.CreateFont("EventHUD_Time", { font = "Roboto", size = 32, weight = 800, antialias = true, shadow = false })
surface.CreateFont("EventHUD_Label", { font = "Roboto", size = 16, weight = 500, antialias = true })
surface.CreateFont("EventHUD_Warning", { font = "Roboto", size = 14, weight = 600, antialias = true })

local function DrawGradientBox(x, y, w, h, color1, color2, vertical)
    local steps = 20
    local step_size = vertical and (h / steps) or (w / steps)
    for i = 0, steps - 1 do
        local t = i / steps
        local r = Lerp(t, color1.r, color2.r)
        local g = Lerp(t, color1.g, color2.g)
        local b = Lerp(t, color1.b, color2.b)
        local a = Lerp(t, color1.a, color2.a)
        surface.SetDrawColor(r, g, b, a)
        if (vertical) then
            surface.DrawRect(x, y + i * step_size, w, step_size + 1)
        else
            surface.DrawRect(x + i * step_size, y, step_size + 1, h)
        end
    end
end

function PLUGIN:DrawEventHUD()
    local active = event_active or GetGlobalBool("events_system_active", false)
    local start_time = event_start_time == 0 and GetGlobalInt("events_system_start_time", 0) or event_start_time
    
    if (!active or start_time == 0) then return end
    
    local elapsed = CurTime() - start_time
    local minutes = math.floor(elapsed / 60)
    local seconds = math.floor(elapsed % 60)
    
    local padding = 20
    local x = ScrW() - 320 - padding
    local y = padding
    local w, h = 320, 140
    
    DrawGradientBox(x, y, w, h, Color(15, 15, 20, 220), Color(25, 25, 35, 200), true)
    
    local pulse = math.abs(math.sin(CurTime() * 2))
    DrawGradientBox(x, y, w, 4, Color(255, 50, 80, 180 + pulse * 75), Color(255, 100, 50, 180 + pulse * 75), false)
    
    surface.SetDrawColor(255, 80, 100, 100 + pulse * 80)
    for i = 1, 2 do surface.DrawOutlinedRect(x - i, y - i, w + i * 2, h + i * 2, 1) end
    
    local offsetY = y + 15
    surface.SetFont("EventHUD_Title")
    surface.SetTextColor(255, 80 + pulse * 50, 80 + pulse * 50, 255)
    local title = "⚔ БОЕВОЙ ИВЕНТ"
    local tw, th = surface.GetTextSize(title)
    surface.SetTextPos(x + (w - tw) / 2, offsetY)
    surface.DrawText(title)
    
    offsetY = offsetY + th + 10
    surface.SetFont("EventHUD_Time")
    surface.SetTextColor(255, 255, 255, 255)
    local time_text = string.format("%02d:%02d", minutes, seconds)
    tw, th = surface.GetTextSize(time_text)
    surface.SetTextPos(x + (w - tw) / 2, offsetY)
    surface.DrawText(time_text)
    
    offsetY = offsetY + th + 10
    surface.SetFont("EventHUD_Warning")
    surface.SetTextColor(255, 220, 100, 200 + pulse * 55)
    local warning = "⚠ При смерти выбываете"
    tw, th = surface.GetTextSize(warning)
    surface.SetTextPos(x + (w - tw) / 2, offsetY)
    surface.DrawText(warning)
end

function PLUGIN:PostDrawTranslucentRenderables()
    if (!event_zone_active or !event_zone_min or !event_zone_max) then return end
    
    local min, max = event_zone_min, event_zone_max
    local corners = {
        Vector(min.x, min.y, min.z), Vector(max.x, min.y, min.z), Vector(max.x, max.y, min.z), Vector(min.x, max.y, min.z),
        Vector(min.x, min.y, max.z), Vector(max.x, min.y, max.z), Vector(max.x, max.y, max.z), Vector(min.x, max.y, max.z)
    }
    
    local color = Color(0, 255, 100, 150)
    render.DrawLine(corners[1], corners[2], color, true)
    render.DrawLine(corners[2], corners[3], color, true)
    render.DrawLine(corners[3], corners[4], color, true)
    render.DrawLine(corners[4], corners[1], color, true)
    render.DrawLine(corners[5], corners[6], color, true)
    render.DrawLine(corners[6], corners[7], color, true)
    render.DrawLine(corners[7], corners[8], color, true)
    render.DrawLine(corners[8], corners[5], color, true)
    render.DrawLine(corners[1], corners[5], color, true)
    render.DrawLine(corners[2], corners[6], color, true)
    render.DrawLine(corners[3], corners[7], color, true)
    render.DrawLine(corners[4], corners[8], color, true)
end
