local PLUGIN = PLUGIN

local doomsday_active = false
local doomsday_start_time = 0

net.Receive("doomsday_night_hud_update", function()
    doomsday_active = net.ReadBool()
    doomsday_start_time = net.ReadInt(32)
end)

function PLUGIN:DoomsdayHUDPaint()
    local active = doomsday_active or GetGlobalBool("doomsday_night_active", false)
    local start_time = doomsday_start_time == 0 and GetGlobalInt("doomsday_night_start_time", 0) or doomsday_start_time
    local alive_count = GetGlobalInt("doomsday_night_alive_count", 0)
    
    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 x, y = ScrW() - 280, 20
    local w, h = 260, 120
    
    surface.SetDrawColor(20, 20, 40, 200)
    surface.DrawRect(x, y, w, h)
    
    local border_alpha = 150 + math.abs(math.sin(CurTime() * 2)) * 105
    surface.SetDrawColor(100, 0, 100, border_alpha)
    surface.DrawOutlinedRect(x, y, w, h, 3)
    
    surface.SetFont("DermaDefaultBold")
    surface.SetTextColor(200, 100, 255, 255)
    local title = "🌙 СУДНАЯ НОЧЬ"
    local tw, th = surface.GetTextSize(title)
    surface.SetTextPos(x + (w - tw) / 2, y + 8)
    surface.DrawText(title)
    
    surface.SetFont("DermaDefault")
    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, y + th + 15)
    surface.DrawText(time_text)
    
    local alive_pulse = 200 + math.abs(math.sin(CurTime() * 3)) * 55
    surface.SetTextColor(255, 150, 150, alive_pulse)
    local alive_text = string.format("Выживших: %d", alive_count)
    tw, th = surface.GetTextSize(alive_text)
    surface.SetTextPos(x + (w - tw) / 2, y + th + 45)
    surface.DrawText(alive_text)
    
    if (LocalPlayer():GetObserverMode() != OBS_MODE_NONE) then
        surface.SetTextColor(255, 100, 100, alive_pulse)
        local spec_text = "РЕЖИМ НАБЛЮДЕНИЯ"
        tw, th = surface.GetTextSize(spec_text)
        surface.SetTextPos(x + (w - tw) / 2, y + h - 25)
        surface.DrawText(spec_text)
    end
end

function PLUGIN:DoomsdayPlayerStats()
    if (!GetGlobalBool("doomsday_night_active", false)) then return end
    local ply = LocalPlayer()
    
    local x, y, w, h = 20, 20, 220, 120
    surface.SetDrawColor(0, 0, 0, 150)
    surface.DrawRect(x, y, w, h)
    surface.SetDrawColor(100, 0, 100, 200)
    surface.DrawOutlinedRect(x, y, w, h, 2)
    
    surface.SetFont("DermaDefaultBold")
    surface.SetTextColor(200, 200, 255, 255)
    surface.SetTextPos(x + 10, y + 10)
    surface.DrawText("📊 Ваша статистика:")
    
    surface.SetFont("DermaDefault")
    surface.SetTextColor(255, 255, 255, 255)
    surface.SetTextPos(x + 10, y + 35)
    surface.DrawText(string.format("❤️ HP: %d", ply:Health()))
    surface.SetTextPos(x + 10, y + 55)
    surface.DrawText(string.format("🛡️ Armor: %d", ply:Armor()))
    
    local weapon = ply:GetActiveWeapon()
    if (IsValid(weapon)) then
        local w_name = string.gsub(weapon:GetClass(), "tfa_ins2_", ""):upper()
        surface.SetTextPos(x + 10, y + 75)
        surface.DrawText("🔫 Оружие: " .. w_name)
    end
end

function PLUGIN:DoomsdayDeathEffect()
    if (!GetGlobalBool("doomsday_night_active", false)) then return end
    local ply = LocalPlayer()
    if (ply:Alive()) then return end
    
    local alpha = 50 + math.abs(math.sin(CurTime() * 2)) * 30
    surface.SetDrawColor(150, 0, 0, alpha)
    surface.DrawRect(0, 0, ScrW(), ScrH())
    
    surface.SetFont("DermaLarge")
    local title_pulse = 200 + math.abs(math.sin(CurTime() * 3)) * 55
    surface.SetTextColor(255, 50, 50, title_pulse)
    local text = "💀 ВЫБЫЛ 💀"
    local tw, th = surface.GetTextSize(text)
    surface.SetTextPos((ScrW() - tw) / 2, ScrH() / 2 - 100)
    surface.DrawText(text)
end

function PLUGIN:HUDPaint()
    self:DrawEventHUD()
    self:DoomsdayHUDPaint()
    self:DoomsdayPlayerStats()
    self:DoomsdayDeathEffect()
end
