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)
	local phys = self:GetPhysicsObject()

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

	self:UpdateVisuals()
end

function ENT:Add_Resource(rtable, ply)
	for k, v in pairs(rtable) do
		if (v > 0) then
			if (k == "Iron") then
				self:SetcIron(self:GetcIron() + v)
			elseif (k == "Bronze") then
				self:SetcBronze(self:GetcBronze() + v)
			elseif (k == "Silver") then
				self:SetcSilver(self:GetcSilver() + v)
			elseif (k == "Gold") then
				self:SetcGold(self:GetcGold() + v)
			elseif (k == "Coal") then
				self:SetcCoal(self:GetcCoal() + v)
			end
		end
	end

	self:UpdateVisuals()
end

function ENT:Add_ResourceJunk(gravel)
	local rtype = gravel:GetResourceType()
	local ramount = gravel:GetResourceAmount()

	if (rtype == "Iron") then
		self:SetcIron(self:GetcIron() + ramount)
	elseif (rtype == "Bronze") then
		self:SetcBronze(self:GetcBronze() + ramount)
	elseif (rtype == "Silver") then
		self:SetcSilver(self:GetcSilver() + ramount)
	elseif (rtype == "Gold") then
		self:SetcGold(self:GetcGold() + ramount)
	elseif (rtype == "Coal") then
		self:SetcCoal(self:GetcCoal() + ramount)
	end

	gravel:Remove()
	self:UpdateVisuals()
end

function ENT:Return_StoredAmount()
	local storedAmount = self:GetcIron() + self:GetcBronze() + self:GetcSilver() + self:GetcGold() + self:GetcCoal()

	return storedAmount
end

function ENT:Decrease_Resource(amount, rtype)
	if (rtype == "Iron") then
		self:SetcIron(self:GetcIron() - amount)
	elseif (rtype == "Bronze") then
		self:SetcBronze(self:GetcBronze() - amount)
	elseif (rtype == "Silver") then
		self:SetcSilver(self:GetcSilver() - amount)
	elseif (rtype == "Gold") then
		self:SetcGold(self:GetcGold() - amount)
	elseif (rtype == "Coal") then
		self:SetcCoal(self:GetcCoal() - amount)
	end

	self:UpdateVisuals()

	if (self:Return_StoredAmount() <= 0) then
		if (zrmine.config.GravelCrates_ReUse == false) then
			self:Remove()
		else
			self:Empty_Basket()
		end
	end
end

function ENT:Empty_Basket()
	self:SetcIron(0)
	self:SetcBronze(0)
	self:SetcSilver(0)
	self:SetcGold(0)
	self:SetcCoal(0)
	self:UpdateVisuals()
end

function ENT:SpawnFromInventory()
	timer.Simple(0.1, function()
		if (IsValid(self)) then
			self:UpdateVisuals()
		end
	end)
end

function ENT:UpdateVisuals()
	local storedAmount = self:GetcIron() + self:GetcBronze() + self:GetcSilver() + self:GetcGold() + self:GetcCoal()

	if (storedAmount >= zrmine.config.GravelCrates_Capacity) then
		self:SetBodygroup(0, 3)
	elseif (storedAmount >= zrmine.config.GravelCrates_Capacity / 2) then
		self:SetBodygroup(0, 2)
	elseif (storedAmount > 0) then
		self:SetBodygroup(0, 1)
	elseif (storedAmount <= 0) then
		self:SetBodygroup(0, 0)
	end
end

function ENT:StartTouch(ent)
	if (ent:GetClass() == "zrms_crusher" and self:Return_StoredAmount() > 0) then
		ent:Add_GravelCrate(self, nil)
	end
end
