AddCSLuaFile()

ENT.Base = "base_gmodentity"
ENT.Type = "anim"
ENT.PrintName = "Exfil Point"
ENT.Spawnable = true
ENT.AutomaticFrameAdvance = true

if CLIENT then
    local exfil_icon = Material("dmz/exfil.png", "smooth")

    hook.Add("HUDPaint", "DMZ_Exfil_Point", function()
        if IsValid(LocalPlayer():GetActiveWeapon()) and LocalPlayer():GetActiveWeapon():GetClass() == "agrp_flare_gun" then
            for _, ent in ipairs(ents.FindByClass("dmz_exfil_point")) do
                if not ent.alpha then
                    ent.alpha = 0
                end
                if ent:GetNWBool('Ready') then
                    local pos = ent:GetPos():ToScreen()

                    surface.SetDrawColor(255,255,255,ent.alpha)
                    surface.SetMaterial(exfil_icon)
                    surface.DrawTexturedRect(pos.x-36, pos.y-48, 72, 72)

                    local num = math.Round(LocalPlayer():GetPos():Distance(ent:GetPos()) * 2/100)
                    if num < 10 or num > 500 then
                        ent.alpha = math.Clamp(ent.alpha-FrameTime()/0.005, 0, 255)
                    else
                        ent.alpha = math.Clamp(ent.alpha+FrameTime()/0.005, 0, 255)
                    end
                    if num < 10 then
                        draw.SimpleText("[G] Вызвать эвакуацию", "GModToolSubtitle", ScrW()/2-20, ScrH()/2+150, color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
                    end
                    draw.SimpleText(num.."m", "Trebuchet24", pos.x, pos.y+12, Color(255,255,255,ent.alpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
                end
            end
        end
    end)

    net.Receive("DMZ_Exfil_Info", function()
        local alpha = 0
        local hide = false
        timer.Simple(5, function()
            hide = true
        end)
        hook.Add("HUDPaint", "DMZ_Exfil_Info", function()
            if hide then
                alpha = math.Clamp(alpha-FrameTime()/0.001, 0, 255)
                if alpha <= 0 then
                    hook.Remove("HUDPaint", "DMZ_Exfil_Info")
                end
            else
                alpha = math.Clamp(alpha+FrameTime()/0.001, 0, 255)
            end

            surface.SetDrawColor(255,255,255,alpha)
            surface.SetMaterial(exfil_icon)
            surface.DrawTexturedRect(ScrW()/2-64, ScrH()/2-270, 128, 128)

            draw.SimpleText("ЭВАКУАЦИОННЫЙ ВЕРТОЛЕТ ВЫЛЕТЕЛ", "DermaLarge", ScrW()/2, ScrH()/2-150, Color(255,255,255,alpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
        end)
    end)
end
