AddCSLuaFile()

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

if CLIENT then
    function ENT:OnRemove()
        if self:GetModel() == "models/exfil/deliveryvan_armored_driveable.mdl" then
            self:StopSound("surrounded/vehicles/van/engine_idle.wav")
        else
            self:StopSound("surrounded/vehicles/palfa/engine_idle.wav")
        end
    end

    function ENT:Initialize()
        if self:GetModel() == "models/exfil/deliveryvan_armored_driveable.mdl" then
            self:EmitSound("surrounded/vehicles/van/engine_idle.wav", 100, 95, 1, CHAN_STATIC)
        else
            self:EmitSound("surrounded/vehicles/palfa/engine_idle.wav", 100, 95, 1, CHAN_STATIC)
        end
    end

    function ENT:Think()
        local dlight = DynamicLight(self:EntIndex())
        if dlight then
            dlight.pos = self:GetPos()-Vector(0,0,128)
            dlight.r = 255
            dlight.g = 255
            dlight.b = 200
            dlight.brightness = 2
            dlight.decay = 1000
            dlight.size = 1000
            dlight.dietime = CurTime() + 1
        end
    end

    local exfil_icon = Material("surrounded/exfil.png")
    local last_exfil_icon = Material("surrounded/lastexfil.png")
    net.Receive("SuR.ExfilIcon", function()
        local bool = net.ReadBool()
        local alpha = 0
        local hide = false
        timer.Simple(8, function()
            hide = true
        end)
        local text = SuR.Language["evac_start"]
        local mat = exfil_icon
        if bool then
            text = SuR.Language["evac_last_start"]
            mat = last_exfil_icon
        end
        hook.Add("HUDPaint", "SuR_Exfil_Info", function()
            if hide then
                alpha = math.Clamp(alpha-FrameTime()/0.002, 0, 255)
                if alpha <= 0 then
                    hook.Remove("HUDPaint", "SuR_Exfil_Info")
                end
            else
                alpha = math.Clamp(alpha+FrameTime()/0.002, 0, 255)
            end

            surface.SetDrawColor(255,255,255,alpha)
            surface.SetMaterial(mat)
            surface.DrawTexturedRect(ScrW()/2-We(100), ScrH()/2-He(350), We(200), He(200))

            draw.SimpleText(text, "SuR_SmallFont2", ScrW()/2, ScrH()/2-He(140), Color(255,255,255,alpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
        end)
    end)
end
