local FISH = {}

FISH.ClassName = "aqua_mine"
FISH.Model = Model("models/roller.mdl")
FISH.Base = "fishing_fish_base_swimming"

FISH.target = NULL

FISH.Rareness = 0.9

if SERVER then
	function FISH:FindBabies()
		for key, ent in pairs(ents.FindInSphere(self:GetPos(), 500)) do

			if ent.ClassName == FISH.ClassName then continue end

			if
				ent:GetClass() == "fishing_fish_angry_baby" and
				not ent.dead
			then
				self.target = ent
				return
			end
		end
	end

	function FISH:PreMove(phys)
		if not self.target:IsValid() then return end

		local mult = (self:WaterLevel() > 0 and phys:GetMass() or 1) * 0.05

		phys:AddVelocity((self.target:GetPos() - self:GetPos()):GetNormalized() * mult)

		return true
	end

	function FISH:OnDeath()
		self:EmitSound("buttons/combine_button5.wav", 120, math.random(90, 110) / self:GetSize())
	end

	function FISH:OnThink()
		if not self.last_think or self.last_think < CurTime() then
			self:FindBabies()

			for _, ent in pairs(ents.FindInSphere(self:GetPos(), 16 * self:GetSize())) do

				if ent.ClassName == FISH.ClassName then continue end

				if ent:IsPlayer() or ent:IsNPC() or ent.IsFishingFish then

					local params = DamageInfo()
						params:SetDamage(math.random(8,13))
						params:SetAttacker(self)
						params:SetInflictor(self)
						params:SetDamageType(DMG_SHOCK)
						params:SetDamageForce((ent:GetPos() - self:GetPos()):GetNormalized()*50)
					ent:TakeDamageInfo(params)

					ent:EmitSound("ambient/energy/spark"..math.random(1,6)..".wav", 100, math.random(90,110) / self:GetSize())

					local params = EffectData()
						params:SetOrigin(ent:LocalToWorld(ent:OBBCenter()))
						params:SetNormal((self:GetPos()-ent:GetPos()):GetNormalized()*2)
						params:SetMagnitude(1)
						params:SetScale(2)
						params:SetRadius(2)
					util.Effect("Sparks", params)
				end
			end

			self.last_think = CurTime() + 1
		end
	end
end
