if game.GetMap() ~= "ttt_sabiru" then return end

game.AddParticles( "particles/fire01.pcf" )
game.AddParticles( "particles/grenade_fx.pcf" )
game.AddParticles( "particles/weapon_fx.pcf" )
game.AddParticles( "particles/blood_impact.pcf" )
game.AddParticles( "particles/advisor_fx.pcf" )
game.AddParticles( "particles/hunter_projectile.pcf" )
PrecacheParticleSystem( "grenade_explosion_01" )
PrecacheParticleSystem( "grenade_explosion_01c" )
PrecacheParticleSystem( "explosion_huge_c" )
PrecacheParticleSystem( "Weapon_Combine_Ion_Cannon_Explosion_f" )
PrecacheParticleSystem( "Weapon_Combine_Ion_Cannon_Exlposion_c" )
PrecacheParticleSystem( "Advisor_Psychic_Blast" )

local function InitEntities() 
	if !SERVER then return end
	
	if (engine.ActiveGamemode() == "terrortown") then 
		for i, ent in ipairs(ents.FindByClass([[npc_zombine]])) do 
			if (ent:GetName() == '') then 
				ent:SetName('sabiru'..ent:EntIndex()) 
			end 
			ent:SetModel('models/sabiru_monsters/sabiru.mdl') 
			ent:SetMaterial('models/sabiru_monsters/whitenoise') 
			ent:SetHealth(900) 
			ent:SetMaxHealth(900) 
			ent:SetBloodColor(-1) 
			ent:SetModelScale(ent:GetModelScale() + 0.3) 
			ent:SetLagCompensated(true)
		end
	else
		for i, ent in ipairs(ents.FindByClass([[npc_zombine]])) do 
			if (ent:GetName() == '') then 
				ent:SetName('sabiru'..ent:EntIndex()) 
			end 
			ent:SetModel('models/sabiru_monsters/sabiru.mdl') 
			ent:SetMaterial('models/sabiru_monsters/whitenoise') 
			ent:SetHealth(500) 
			ent:SetMaxHealth(500) 
			ent:SetBloodColor(-1) 
			ent:SetModelScale(ent:GetModelScale() + 0.3) 
			ent:SetLagCompensated(true)
		end
	end
end

hook.Add( "InitPostEntity", "SabiruBossInit", function()
	InitEntities()
end )

hook.Add( "PostCleanupMap", "SabiruBossInit", function()
	InitEntities()
end )


local function DoScriptedSequence(npc, anim) 
	local sequence = ents.Create("scripted_sequence")
	sequence:SetPos(npc:GetPos())
	sequence:SetKeyValue("spawnflags", 4708)
	sequence:SetKeyValue("m_iszPlay", anim)
	sequence:SetKeyValue("m_iszEntity", npc:GetName())
	sequence:SetKeyValue("m_fMoveTo", 0)
	
	sequence:Spawn()
	sequence:Fire("BeginSequence")
	timer.Simple(3, function()
		if (IsValid(sequence)) then sequence:Remove() end
	end)
end


if SERVER then
	util.AddNetworkString("angelDLight")
end

if CLIENT then
	net.Receive("angelDLight", function()
		local vector = net.ReadVector()
		
		local dlight = DynamicLight( LocalPlayer():EntIndex() ) 
		if ( dlight ) then
			dlight.pos = Vector (vector.x, vector.y, vector.z + 30)
			dlight.r = 100
			dlight.g = 130
			dlight.b = 255
			dlight.brightness = 10
			dlight.Decay = 4000
			dlight.Size = 6000
			dlight.DieTime = CurTime() + 3
		end
	end)
end

local function DoBossEffect(ent, player, shoudPlayFlinch) 
	if (player:IsPlayer()) then
		net.Start('angelDLight') 
		net.WriteVector(ent:GetPos())
		net.Send(player)
	end
	
	ParticleEffect( "Weapon_Combine_Ion_Cannon_Explosion_f", ent:GetPos(), Angle( 0, 0, 0 )) -- draw the particle
	ent:EmitSound("NPC_Sabiru.Yell1")
	ent:EmitSound("NPC_Sabiru.BallAttack")
	
	if (shoudPlayFlinch) then DoScriptedSequence(ent, "laflinch") end
	
	local explosioneffect = ents.Create("env_physexplosion")
	explosioneffect:SetKeyValue("spawnflags", 3)
	explosioneffect:SetKeyValue("magnitude", 2000)
	explosioneffect:SetKeyValue("radius", 300)
	explosioneffect:SetPos(ent:GetPos())
	explosioneffect:Spawn()
	explosioneffect:Fire( "Explode" )
end


--Handling boss dmg
hook.Add("EntityTakeDamage", "BossDmgHandler", function(target, dmg)
	local attacker = dmg:GetAttacker()
	local anim = target:GetSequenceName(target:GetSequence())
	
	if attacker:GetClass() == "npc_zombine" then 
		target:EmitSound("NPC_Sabiru.Hit") 
		if target:IsNPC() then
			dmg:ScaleDamage(10) 
			dmg:SetDamageForce(dmg:GetDamageForce() * 10)
		end
	end	
	
	if target:GetClass() == "npc_zombine" then
		local correctPos = Vector(target:GetPos().x, target:GetPos().y - 40, target:GetPos().z + 70)
		
		dmg:SetDamageType(DMG_NEVERGIB)
		ParticleEffectAttach( "grenade_explosion_01c", 4, target, 1) -- draw the particle
		target:EmitSound("NPC_Sabiru.Yell2")
		
		if (attacker:IsPlayer()) then
			local rng = math.random(1,40)
			if (rng > 35) and (anim != "laflinch")  then
				DoBossEffect(target, attacker, true)
			end
		end
		
		if (target:Health() < target:GetMaxHealth() / 2) and (target:GetMaterial() != "models/sabiru_monsters/rednoise") then
			target:SetMaterial("models/sabiru_monsters/rednoise")
			DoBossEffect(target, attacker, true)
		end
	end
end)

hook.Add( "OnNPCKilled", "BossDeathHandler", function( npc, attacker, inflictor )
	if npc:GetClass() != "npc_zombine" then return end
	
	local correctPos = Vector(npc:GetPos().x, npc:GetPos().y, npc:GetPos().z - 150)
	
	ParticleEffect( "explosion_huge_c", correctPos, Angle( 0, 0, 0 )) -- draw the particle
	ParticleEffect( "Weapon_Combine_Ion_Cannon_Explosion_f", npc:GetPos(), Angle( 0, 0, 0 )) -- draw the particle
	ParticleEffect( "Advisor_Psychic_Blast", npc:GetPos(), Angle( 0, 0, 0 )) -- draw the particle
	
	npc:SetMaterial("models/sabiru_monsters/rednoise")
	--attacker:EmitSound("NPC_Sabiru.Die1")
	attacker:EmitSound("NPC_Sabiru.Die2")
	
	npc:Remove()
end)

--Replace zombines when spawned via the spawnmenu
hook.Add("PlayerSpawnedNPC", "SabiruBossReplacer", function(ply, ent)
	if ent:GetClass() != "npc_zombine" then return end
	if (!IsValid(ent)) then return end
	
	local tr = util.TraceLine( {
		start = ply:EyePos(),
		endpos = ply:EyePos() + ply:EyeAngles():Forward() * 10000,
		filter = ply
	} )

	ent:SetName("sabiru"..ent:EntIndex())
	ent:SetModel("models/sabiru_monsters/sabiru.mdl") 
	ent:SetMaterial("models/sabiru_monsters/whitenoise") 
	ent:SetHealth(500) 
	ent:SetMaxHealth(500) 
	ent:SetBloodColor(-1)
	ent:SetModelScale(ent:GetModelScale() + 0.3)
	ent:SetPos(Vector (tr.HitPos.x, tr.HitPos.y, tr.HitPos.z + 20))

	DoScriptedSequence(ent, "eatbodystand")
	DoBossEffect(ent, ply, false)
	
	
end)