ENT.Base = "base_gmodentity"
ENT.Type = "anim"
ENT.PrintName = "Helicoper"
ENT.RenderGroup = RENDERGROUP_TRANSLUCENT
ENT.Model = Model("models/tdmg/heli.mdl")
ENT.AutomaticFrameAdvance = true

function ENT:Initialize()
    self:SetModel(self.Model)

    if SERVER then
        self:SetHealth(1)         
        self:SetPos(self:GetPos() + Vector(0, 0, 10))

        local tr = {
            start = self:GetPos(),
            endpos = self:GetPos() + Vector(0, 0, 512)
        }

        if tr.HitSky then
            self:SetAngles(Angle(0, math.Rand(0, 360), 0))
        else
            local testpos = self:GetPos() + Vector(0, 0, 512)

            local amt = 15

            local best = 0
            local best_dist = math.huge

            local offset = math.Rand(0, 360)

            for i = 1, amt do
                local angle = math.Rand(0, 360)

                local str = util.TraceLine({
                    start = testpos,
                    endpos = testpos + Angle(0, angle + offset, 0):Forward() * 10000
                })

                if str.HitSky then
                    best = angle
                    break
                elseif str.Fraction == 1 then
                    best = angle
                    break
                elseif str.Fraction * 10000 > best_dist then
                    best = angle
                    best_dist = str.Fraction * 10000
                end
            end

            self:SetAngles(Angle(0, best + offset + 180 - 10, 0))
            self:SetPos(self:LocalToWorld(-Vector(107.472321, -70.542793, 0)))
        end
    end

    timer.Simple(14, function()
        if !IsValid(self) then return end
        self:Finish()
    end)

    timer.Simple(31.6, function()
        if !IsValid(self) then return end

        self:SetRenderFX(kRenderFxFadeSlow)
    end)

    timer.Simple(36.6, function()
        if !IsValid(self) then return end

        if SERVER then
            self:Remove()
        end
    end)

    if CLIENT then return end
    self:ResetSequence("spawn")

    self.mod1, self.mod1_1 = self:CreateSoldierModel(1)
    self.mod2, self.mod2_1 = self:CreateSoldierModel(2)
    self.mod3, self.mod3_1 = self:CreateSoldierModel(3)
    self.mod4, self.mod4_1 = self:CreateSoldierModel(4)

    timer.Simple(11.5, function()
        if IsValid(self.mod1) then
            self:CreateSoldierNPCFromModel(1)
            self.mod1:Remove()
        end
    end)
    timer.Simple(12.5, function()
        if IsValid(self.mod2) then
            self:CreateSoldierNPCFromModel(2)
            self.mod2:Remove()
        end
    end)
    timer.Simple(14, function()
        if IsValid(self.mod3) then
            self:CreateSoldierNPCFromModel(3)
            self.mod3:Remove()
        end
    end)
end

function ENT:Finish()
    if CLIENT then return end
    self:SetBodygroup(3, 1)
    if IsValid(self.mod4) then
        self.mod4:Remove()
    end
end

function ENT:CreateSoldierModel(type)
    local friend = ents.Create("prop_dynamic")
    local friend2 = ents.Create("base_anim")
    if type == 1 then
        friend:SetModel("models/tdmg/guy01.mdl")
    elseif type == 2 then
        friend:SetModel("models/tdmg/guy02.mdl")
    elseif type == 3 then
        friend:SetModel("models/tdmg/guy03.mdl")
    elseif type == 4 then
        friend:SetModel("models/tdmg/chiefl.mdl")
    end
    friend:SetPos(self:GetPos())
    friend:SetAngles(self:GetAngles())
    friend:SetParent(self)
    friend:Spawn()
    friend:ResetSequence('spawn')
    friend2:SetModel("models/player/swat.mdl")
    friend2:SetPos(self:GetPos())
    friend2:SetAngles(self:GetAngles())
    friend2:SetParent(friend)
    friend2:AddEffects(1)
    friend2:Spawn()
    return friend, friend2
end

function ENT:CreateSoldierNPCFromModel(type)
    local pos = self:GetPos()
    local ang = self:GetAngles()
    local bgs = self
    if type == 1 then
        pos = self:LocalToWorld(Vector(112.3, -73.6, -10))
        ang = self.mod1:GetAngles()+Angle(0,180,0)
        bgs = self.mod1_1   
    elseif type == 2 then
        pos = self:LocalToWorld(Vector(66.85, -115.5, -10))
        ang = self.mod2:GetAngles()+Angle(0,180,0)
        bgs = self.mod2_1  
    elseif type == 3 then
        pos = self:LocalToWorld(Vector(73.1, -40.8, -10))
        ang = self.mod3:GetAngles()+Angle(0,180,0)
        bgs = self.mod3_1      
    end
    local class = "sur_support_npc"
    local friend = ents.Create(class)
    friend:SetPos(pos)
    friend:SetAngles(ang)
    friend:Spawn()
end

function ENT:Think()
    self:NextThink(CurTime())
    return true
end