zrmine = zrmine or {}
zrmine.f = zrmine.f or {}

if SERVER then
	-- Animation
	util.AddNetworkString("zrmine_AnimEvent")

	local function ServerAnim(prop, anim, speed)
		local sequence = prop:LookupSequence(anim)
		prop:SetCycle(0)
		prop:ResetSequence(sequence)
		prop:SetPlaybackRate(speed)
		prop:SetCycle(0)
	end

	function zrmine.f.CreateAnimTable(prop, anim, speed)
		ServerAnim(prop, anim, speed)
		net.Start("zrmine_AnimEvent")
		local animInfo = {}
		animInfo.anim = anim
		animInfo.speed = speed
		animInfo.parent = prop
		net.WriteTable(animInfo)
		net.SendPVS(prop:GetPos())
	end

	--Effects
	util.AddNetworkString("zrmine_FX")

	function zrmine.f.CreateEffectTable(effect, sound, parent, angle, position, attach)
		net.Start("zrmine_FX")
		local effectInfo = {}
		effectInfo.effect = effect
		effectInfo.sound = sound
		effectInfo.pos = position
		effectInfo.ang = angle
		effectInfo.parent = parent
		effectInfo.attach = attach
		net.WriteTable(effectInfo)
		net.SendPVS(parent:GetPos())
	end

	util.AddNetworkString("zrmine_remove_FX")

	function zrmine.f.RemoveEffectNamed(prop, effect)
		local effectInfo = {}
		effectInfo.effect = effect
		effectInfo.parent = prop
		net.Start("zrmine_remove_FX")
		net.WriteTable(effectInfo)
		net.SendPVS(prop:GetPos())
	end

	--Custom Emitter
	util.AddNetworkString("zrmine_insert_FX")

	function zrmine.f.CreateInsertEffect(prop, rtype)
		local effectInfo = {}
		effectInfo.parent = prop
		effectInfo.rtype = rtype
		net.Start("zrmine_insert_FX")
		net.WriteTable(effectInfo)
		net.SendPVS(prop:GetPos())
	end
end

if CLIENT then
	-- Animation
	local function ClientAnim(prop, anim, speed)
		local sequence = prop:LookupSequence(anim)
		prop:SetCycle(0)
		prop:ResetSequence(sequence)
		prop:SetPlaybackRate(speed)
		prop:SetCycle(0)
	end

	net.Receive("zrmine_AnimEvent", function(len, ply)
		local animInfo = net.ReadTable()

		if (animInfo and IsValid(animInfo.parent) and animInfo.anim) then
			ClientAnim(animInfo.parent, animInfo.anim, animInfo.speed)

			if (string.sub(animInfo.parent:GetClass(), 1, 17) == "zrms_conveyorbelt") then
				animInfo.parent:CustomAnimation(animInfo.anim)
			end

			if (string.sub(animInfo.parent:GetClass(), 1, 12) == "zrms_crusher") then
				animInfo.parent:CustomAnimation(animInfo.anim, animInfo.speed)
			end
		end
	end)

	-- Effects
	net.Receive("zrmine_FX", function(len, ply)
		local effectInfo = net.ReadTable()

		if (effectInfo and IsValid(effectInfo.parent)) then
			if (effectInfo.sound) then
				effectInfo.parent:EmitSound(effectInfo.sound)
			end

			if (effectInfo.effect and LocalPlayer():GetPos():Distance(effectInfo.parent:GetPos()) < 500) then
				if (effectInfo.attach) then
					ParticleEffectAttach(effectInfo.effect, PATTACH_POINT_FOLLOW, effectInfo.parent, effectInfo.attach)
				else
					ParticleEffect(effectInfo.effect, effectInfo.pos, effectInfo.ang, effectInfo.parent)
				end
			end
		end
	end)

	net.Receive("zrmine_remove_FX", function(len, ply)
		local effectInfo = net.ReadTable()

		if (effectInfo and IsValid(effectInfo.parent) and effectInfo.effect) then
			effectInfo.parent:StopParticlesNamed(effectInfo.effect)
		end
	end)

	--Custom Emitter
	local function Emit_InsertEffect(prop, rtype)
		if (GetConVar("zrms_cl_FillIndicator"):GetInt() == 0) then return end
		local pos = prop:GetPos()
		local vel = Vector(0, 0, 200)

		if (prop:GetClass() == "zrms_ore") then
			pos = prop:GetPos() + prop:GetRight() * math.random(-50, 50) + prop:GetForward() * math.random(-50, 50)
			vel = Vector(0, 0, math.random(300, 350))
		end

		local icon = prop.InsertEffect:Add("zerochain/zrms/particles/zrms_ore", pos)
		icon:SetVelocity(vel)
		icon:SetDieTime(2)
		icon:SetStartAlpha(255)
		icon:SetEndAlpha(0)
		icon:SetStartSize(7)
		icon:SetEndSize(10)
		local iconColor = Color(255, 255, 255)

		if (rtype == "Iron") then
			iconColor = Color(104, 85, 87)
		elseif (rtype == "Bronze") then
			iconColor = Color(158, 72, 39)
		elseif (rtype == "Silver") then
			iconColor = Color(125, 125, 125)
		elseif (rtype == "Gold") then
			iconColor = Color(189, 143, 49)
		elseif (rtype == "Coal") then
			iconColor = Color(0, 0, 0)
		else
			iconColor = Color(255, 255, 255)
		end

		icon:SetColor(iconColor.r, iconColor.g, iconColor.b)
		icon:SetGravity(Vector(0, 0, 0))
		icon:SetAirResistance(256)
	end

	net.Receive("zrmine_insert_FX", function(len, ply)
		local effectInfo = net.ReadTable()

		-- Triggers our insert effect
		if (IsValid(effectInfo.parent) and LocalPlayer():GetPos():Distance(effectInfo.parent:GetPos()) < 500) then
			Emit_InsertEffect(effectInfo.parent, effectInfo.rtype)
		end
	end)
end
