include("shared.lua")

function ENT:Initialize()
	self.TableProp = {
		{
			prop = "models/props_fortifications/concrete_barrier001_96_reference.mdl",
			health = 2500,
			damage = 0,
			breaksound = "physics/concrete/concrete_break"..math.random(2,3)..".wav",
		},
		{
			prop = "models/props_fortifications/barricade_razorwire001_128_reference.mdl",
			health = 250,
			damage = 10,
			breaksound = "physics/metal/metal_chainlink_impact_hard"..math.random(1,3)..".wav",
		},
		{
			prop = "models/props_wasteland/dockplank01b.mdl",
			health = 500,
			damage = 0,
			breaksound = "physics/wood/wood_plank_break"..math.random(1,4)..".wav",
		},
		{
			prop = "models/props_debris/metal_panel01a.mdl",
			health = 1125,
			damage = 0,
			breaksound = "physics/metal/metal_chainlink_impact_hard"..math.random(1,3)..".wav",
		},
		{
			prop = "models/props_combine/combine_light001a.mdl",
			health = 100,
			damage = 0,
			breaksound = "physics/glass/glass_bottle_impact_hard"..math.random(1,3)..".wav",
		},
		{
			prop = "",
			health = self.healthmax,
			damage = 0,
			breaksound = "physics/concrete/concrete_break"..math.random(2,3)..".wav",
		},
	}
	if not self.MapCade then
		if not self.id then
			self.id = 1
		end
		self.barricade = true
		self:SetModel(self.TableProp[self.id].prop)
		if self.id != 2 then
			self:SetSolid(SOLID_VPHYSICS)
			if self.id == 5 then
				self:SetNWBool('lamp', true)
			end
		end
		self.health = self.TableProp[self.id].health
	else
		self:SetSolid(SOLID_VPHYSICS)
	end
	self:SetNWFloat('healthmax', self.TableProp[self.id].health)
	self:SetCustomCollisionCheck(true)
end

function ENT:Think()
	self:SetColor(Color(255,255*(self.health/self.TableProp[self.id].health),255*(self.health/self.TableProp[self.id].health)))
	self:SetHealth(self.health)
	if self.TableProp[self.id].damage > 0 and not self.cantdamage then
		self.cantdamage = true
		for _, ent in ipairs(ents.FindInSphere(self:GetPos(), 40)) do
			if (ent:IsPlayer() or ent:IsNextBot()) then
				local d = DamageInfo()
				if ent:IsNextBot() then
					d:SetDamage(self.TableProp[self.id].damage)
				end
				d:SetAttacker(self)
				d:SetDamageType(DMG_FALL)

				if d:GetDamage() > 0 then
					ent:TakeDamageInfo(d)
					
					self:EmitSound('physics/metal/metal_barrel_impact_soft'..math.random(1,4)..'.wav')
					self:TakeDamage(10)
				end
			end
		end
		timer.Simple(0.5, function()
			if IsValid(self) then
				self.cantdamage = false
			end
		end)
	end
end

function ENT:OnTakeDamage(dmg)
	if dmg:GetAttacker():IsPlayer() and dmg:GetAttacker():IsSurvivor() then
		return 0
	end
	self.health = self.health - dmg:GetDamage()
	if self.health <= 0 then
		self:EmitSound(self.TableProp[self.id].breaksound)
		self:Remove()
	end
end
