AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

------------------------------//
function ENT:SpawnFunction(ply, tr)
	if (not tr.Hit) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 1
	local ent = ents.Create(self.ClassName)
	local angle = ply:GetAimVector():Angle()
	angle = Angle(0, angle.yaw, 0)
	angle:RotateAroundAxis(angle:Up(), 180)
	ent:SetAngles(angle)
	ent:SetPos(SpawnPos)
	ent:Spawn()
	ent:Activate()
	zrmine.f.SetOwnerID(ent, ply)

	return ent
end

function ENT:Initialize()
	self:SetModel(self.Model)
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType(SIMPLE_USE)
	self:SetCollisionGroup(COLLISION_GROUP_WEAPON)
	self:UseClientSideAnimation()
	local phys = self:GetPhysicsObject()

	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableMotion(true)
	end

	self.PhysgunDisabled = true
	self.BarCount = 0
	self.wait = false
	self.closed = false
end

function ENT:Use(ply, caller)
	if IsValid(caller) and caller:IsPlayer() then
		if (zrmine.f.IsMetalBuyerCustomer(ply)) then
			if (self.wait) then return end

			if (zrmine.config.StorageCrateFull and self.BarCount < 12) then
				zrmine.f.Notify(ply, zrmine.language.StorageCrate_NotFull, 1)

				return
			end

			--self:CreateEffect_Table(nil,"progress_collecting",self,self:GetAngles(),self:GetPos())
			if (self.BarCount < 12 and zrmine.config.StorageCrateFull == false) then
				if (self.BarCount > 0) then
					if (self.closed == false) then
						self:Crate_Close()
					end
				else
					zrmine.f.Notify(ply, zrmine.language.StorageCrate_NeedMetalBar, 1)
				end
			end

			if (self.closed) then
				if (self:GetbIron() > 0) then
					ply:zrms_AddMetalBar("Iron", self:GetbIron())
				end

				if (self:GetbBronze() > 0) then
					ply:zrms_AddMetalBar("Bronze", self:GetbBronze())
				end

				if (self:GetbSilver() > 0) then
					ply:zrms_AddMetalBar("Silver", self:GetbSilver())
				end

				if (self:GetbGold() > 0) then
					ply:zrms_AddMetalBar("Gold", self:GetbGold())
				end

				--local string = string.Replace(/*zmlab.config.Language.transportcrate.collect*/ "+ $metalValue $" , "$metalValue", tostring(collectedValue) )
				zrmine.f.Notify(ply, zrmine.language.StorageCrate_Collect, 0)
				self:Remove()
			end
		else
			zrmine.f.Notify(ply, zrmine.language.WrongJob, 1)
		end
	end
end

function ENT:Crate_Close()
	self.wait = true
	self:CreateAnim_Table("close", 1)

	timer.Simple(1.2, function()
		if (IsValid(self)) then
			self.closed = true
			self.wait = false
		end
	end)
end

function ENT:Add_Bar(bar)
	if (bar:GetMetalType() == "Iron") then
		self:SetbIron(self:GetbIron() + 1)
	elseif (bar:GetMetalType() == "Bronze") then
		self:SetbBronze(self:GetbBronze() + 1)
	elseif (bar:GetMetalType() == "Silver") then
		self:SetbSilver(self:GetbSilver() + 1)
	elseif (bar:GetMetalType() == "Gold") then
		self:SetbGold(self:GetbGold() + 1)
	end

	self.BarCount = self.BarCount + 1
	self:CreateEffect_Table(nil, "zrmine_sfx_addbar", self, self:GetAngles(), self:GetPos())
	bar:Remove()
	self:UpdateClientVisuals()

	if (self.BarCount >= 12) then
		self:Crate_Close()
	end
end

function ENT:StartTouch(ent)
	if ent:GetClass() == "zrms_bar" and self.BarCount < 12 then
		self:Add_Bar(ent)
	end
end

function ENT:SpawnFromInventory()
	timer.Simple(0.1, function()
		if (IsValid(self)) then
			net.Start("zrms_storagecrate_UpdateClientProps")
			net.WriteFloat(self:EntIndex())
			net.SendPVS(self:GetPos())
			self:CreateAnim_Table("close", 10)
			self.closed = true
			self.BarCount = self:GetbIron() + self:GetbBronze() + self:GetbSilver() + self:GetbGold()
		end
	end)
end

util.AddNetworkString("zrms_storagecrate_UpdateClientProps")

function ENT:UpdateClientVisuals()
	timer.Simple(0.1, function()
		if (IsValid(self)) then
			net.Start("zrms_storagecrate_UpdateClientProps")
			net.WriteFloat(self:EntIndex())
			net.SendPVS(self:GetPos())
		end
	end)
end

--Effects
function ENT:CreateEffect_Table(effect, sound, parent, angle, position)
	zrmine.f.CreateEffectTable(effect, sound, parent, angle, position)
end

-- Animation
util.AddNetworkString("zrms_storagecrate_AnimEvent")

function ENT:CreateAnim_Table(anim, speed)
	if (self:LookupSequence(anim) == self:GetSequence()) then return end
	zrmine.f.CreateAnimTable(self, anim, speed)
end
