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.PhysgunDisabled = true
	self:UpdateVisuals()
end

function ENT:Use(activator, caller)
	local refinery = self:GetParent()
	if IsValid(caller) and caller:IsPlayer() and IsValid(refinery) and string.sub(refinery:GetClass(), 1, 12) == "zrms_refiner" then

		if (zrmine.config.ResourceCrates_Sharing) then
			refinery:Detach_Basket(self)
		else
			if not zrmine.f.IsOwner(activator, self) then
				zrmine.f.Notify(activator, zrmine.language.Module_DontOwn, 1)

				return
			end

			refinery:Detach_Basket(self)
		end
	end
end

function ENT:Add_Resource(amount, rtype)
	self:SetResourceType(rtype)
	self:SetResourceAmount(self:GetResourceAmount() + amount)
	self:UpdateVisuals()
end

function ENT:Decrease_Resource(amount, rtype)
	self:SetResourceType(rtype)
	self:SetResourceAmount(self:GetResourceAmount() - amount)
	self:UpdateVisuals()
end

function ENT:Empty_Basket()
	self:SetSkin(0)
	self:SetBodygroup(0, 0)
	self:SetResourceType("Empty")
	self:SetResourceAmount(0)
end

function ENT:UpdateVisuals()
	if (self:GetResourceType() == "Dirt") then
		self:SetSkin(0)
	elseif (self:GetResourceType() == "Iron") then
		self:SetSkin(1)
	elseif (self:GetResourceType() == "Bronze") then
		self:SetSkin(2)
	elseif (self:GetResourceType() == "Silver") then
		self:SetSkin(3)
	elseif (self:GetResourceType() == "Gold") then
		self:SetSkin(4)
	elseif (self:GetResourceType() == "Coal") then
		self:SetSkin(5)
	end

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

function ENT:OnRemove()
	if (IsValid(self:GetParent())) then
		self:GetParent():Detach_Basket(self)
	end
end

function ENT:StartTouch(ent)
	if ent:GetClass() == "zrms_melter" then
		rType = self:GetResourceType()

		if (rType == "Coal") then
			ent:Add_Coal(self)
		elseif (rType ~= "Empty") then
			ent:Add_Resource(self, nil)
		end
	elseif (string.sub(ent:GetClass(), 1, 12) == "zrms_refiner") then
		if (ent.connected_Parent ~= nil and ent.connected_Basket == nil) then
			ent:Place_Basket(self)
		end
	end
end
