surface.CreateFont("ixDeathScreenMain", {
    font = "OperiusRegular",
    size = 180,
    weight = 300,
    extended = true
})

surface.CreateFont("ixDeathScreenSub", {
    font = "OperiusRegular",
    size = 90,
    weight = 300,
    extended = true
})

surface.CreateFont("ixDeathScreenMainFallback", {
    font = "Roboto",
    size = 180,
    weight = 700,
    extended = true
})

surface.CreateFont("ixDeathScreenSubFallback", {
    font = "Roboto",
    size = 90,
    weight = 500,
    extended = true
})

local function GetFont(primary, fallback)
    surface.SetFont(primary)
    local w = surface.GetTextSize("TEST")
    return (w > 0) and primary or fallback
end

local deathScreenPanel = nil

net.Receive("ixDeathScreen", function()
    if not ix.config.Get("deathScreenEnabled") then return end

    if IsValid(deathScreenPanel) then
        deathScreenPanel:Remove()
    end

    local duration = net.ReadFloat()
    local killerName = net.ReadString()

    deathScreenPanel = vgui.Create("DFrame")
    deathScreenPanel:SetSize(ScrW(), ScrH())
    deathScreenPanel:SetPos(0, 0)
    deathScreenPanel:SetTitle("")
    deathScreenPanel:ShowCloseButton(false)
    deathScreenPanel:SetDraggable(false)
    deathScreenPanel:MakePopup()
    deathScreenPanel:SetKeyboardInputEnabled(false)
    deathScreenPanel:SetMouseInputEnabled(false)

    local colorAlpha = 0
    local textAlpha = 0
    local fadeOutStarted = false
    local startTime = RealTime()

    local fadeInSpeed = ix.config.Get("deathScreenFadeInSpeed", 5)
    local textFadeSpeed = ix.config.Get("deathScreenTextFadeSpeed", 0.5)
    local textColor = ix.config.Get("deathScreenTextColor", Color(132, 43, 60))
    local mainText = ix.config.Get("deathScreenMainText", "YOU'RE DEAD")
    local subText = "[Убил: " .. killerName .. "]"

    timer.Simple(duration - 1.5, function()
        if IsValid(deathScreenPanel) then
            fadeOutStarted = true
        end
    end)

    timer.Simple(duration, function()
        if IsValid(deathScreenPanel) then
            deathScreenPanel:Remove()
        end
    end)

    deathScreenPanel.Paint = function(s, w, h)
        if fadeOutStarted then
            colorAlpha = math.Approach(colorAlpha, 0, FrameTime() * 255)
        else
            colorAlpha = math.Approach(colorAlpha, 255, FrameTime() * fadeInSpeed * 255)
        end

        draw.RoundedBox(0, 0, 0, w, h, Color(0, 0, 0, colorAlpha))
    end

    local textLabel = vgui.Create("DLabel", deathScreenPanel)
    textLabel:SetSize(deathScreenPanel:GetWide(), deathScreenPanel:GetTall())
    textLabel:SetPos(0, 0)
    textLabel:SetText("")

    textLabel.Paint = function(s, w, h)
        local elapsed = RealTime() - startTime

        if fadeOutStarted then
            textAlpha = math.Approach(textAlpha, 0, FrameTime() * 255 * 4)
        elseif elapsed > 1 then
            textAlpha = math.Approach(textAlpha, 255, FrameTime() * textFadeSpeed * 255)
        end

        local mainFont = GetFont("ixDeathScreenMain", "ixDeathScreenMainFallback")
        local subFont = GetFont("ixDeathScreenSub", "ixDeathScreenSubFallback")

        draw.SimpleText(mainText, mainFont, w/2, h/2,
            Color(textColor.r, textColor.g, textColor.b, textAlpha),
            TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)

        draw.SimpleText(subText, subFont, w/2, h/2 + 144,
            Color(textColor.r, textColor.g, textColor.b, textAlpha),
            TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)

        local remaining = math.max(0, math.ceil(duration - elapsed))
        draw.SimpleText("ВОЗРОЖДЕНИЕ ЧЕРЕЗ: " .. remaining, subFont, w/2, h/2 + 250,
            Color(textColor.r, textColor.g, textColor.b, textAlpha),
            TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
    end
end)

hook.Add("InitPostEntity", "ixDeathScreenCleanup", function()
    if IsValid(deathScreenPanel) then
        deathScreenPanel:Remove()
    end
end)
