include("shared.lua")

function ENT:Draw()
	self:DrawModel()

	if (LocalPlayer():GetPos():Distance(self:GetPos()) < 300) then
		self:DrawInfo()
	end
end

function ENT:DrawTranslucent()
	self:Draw()
end

local barIcon = Material("materials/zerochain/zrms/ui/zrms_bar.png", "smooth")
local CircleIcon = Material("materials/zerochain/zrms/ui/zrms_circle.png", "smooth")
local size = 75

function ENT:DrawResourceItem(Info, color, offsetY)
	draw.RoundedBox(1, -2, 69 + offsetY, 4, 7, Color(255, 255, 255, 15))
	surface.SetDrawColor(Color(255, 255, 255, 25))
	surface.SetMaterial(CircleIcon)
	surface.DrawTexturedRect(-38, offsetY, size, size)
	draw.NoTexture()
	surface.SetDrawColor(color)
	surface.SetMaterial(barIcon)
	surface.DrawTexturedRect(-38, offsetY, size, size)
	draw.NoTexture()
	draw.DrawText(Info, "zrmine_gravelcrate_font1", 1, offsetY + 32, Color(255, 255, 255), TEXT_ALIGN_CENTER)
end

function ENT:DrawInfo()
	local Pos = self:GetPos() + Vector(0, 0, 20)
	local Ang = Angle(0, LocalPlayer():EyeAngles().y - 90, 90)
	local rTable = {}

	rTable["Iron"] = {
		a = self:GetbIron(),
		c = Color(104, 85, 87)
	}

	rTable["Bronze"] = {
		a = self:GetbBronze(),
		c = Color(158, 72, 39)
	}

	rTable["Silver"] = {
		a = self:GetbSilver(),
		c = Color(125, 125, 125)
	}

	rTable["Gold"] = {
		a = self:GetbGold(),
		c = Color(189, 143, 49)
	}

	cam.Start3D2D(Pos, Ang, 0.1)
	local oY = -35

	for k, v in pairs(rTable) do
		if (rTable[k].a > 0) then
			self:DrawResourceItem(rTable[k].a, rTable[k].c, oY)
			oY = oY - 70
		end
	end

	cam.End3D2D()
end

function ENT:Think()
	self:SetNextClientThink(CurTime())

	return true
end

net.Receive("zrms_storagecrate_UpdateClientProps", function(len, ply)
	local ent = Entity(net.ReadFloat())

	if (IsValid(ent)) then
		ent:UpdateClientProps()
	end
end)

function ENT:UpdateClientProps()
	if self.ClientBars then
		for i = 1, table.Count(self.ClientBars) do
			local bar = self.ClientBars[i]

			if (IsValid(bar)) then
				bar:Remove()
			end
		end
	end

	self.ClientBars = {}

	for i = 1, self:GetbIron() do
		self:CreateClientBar(0)
	end

	for i = 1, self:GetbBronze() do
		self:CreateClientBar(1)
	end

	for i = 1, self:GetbSilver() do
		self:CreateClientBar(2)
	end

	for i = 1, self:GetbGold() do
		self:CreateClientBar(3)
	end
end

function ENT:OnRemove()
	if self.ClientBars then
		for i = 1, table.Count(self.ClientBars) do
			local bar = self.ClientBars[i]

			if (IsValid(bar)) then
				bar:Remove()
			end
		end
	end
end

function ENT:CreateClientBar(skin)
	local y = 1.5
	local c = 4.5
	local l = -5.5
	local barCount = table.Count(self.ClientBars)

	if (barCount > 5) then
		y = 3.5
	end

	if (barCount == 1 or barCount == 7) then
		c = 0
	elseif (barCount == 2 or barCount == 8) then
		c = -4.5
	elseif (barCount == 3 or barCount == 9) then
		l = 5.5
		c = 4.5
	elseif (barCount == 4 or barCount == 10) then
		l = 5.5
		c = 0
	elseif (barCount == 5 or barCount == 11) then
		l = 5.5
		c = -4.5
	end

	local bar = ents.CreateClientProp()
	bar:SetModel("models/Zerochain/props_mining/zrms_bar.mdl")
	bar:Spawn()
	bar:Activate()
	bar:SetPos(self:GetPos() + self:GetUp() * y + self:GetForward() * l + self:GetRight() * c)
	local ang = self:GetAngles()
	ang:RotateAroundAxis(self:GetUp(), 0)
	ang:RotateAroundAxis(self:GetRight(), 180)
	bar:SetAngles(ang)
	bar:SetParent(self)
	--bar:SetCollisionGroup( COLLISION_GROUP_WORLD  )
	bar:SetSkin(skin)
	table.insert(self.ClientBars, bar)
end
