include("shared.lua")

function ENT:Draw()
	self:DrawModel()

	if (LocalPlayer():GetPos():Distance(self:GetPos()) < 250) then
		self:DrawInfo()

		if (LocalPlayer():GetPos():Distance(self:GetPos()) < 100) then
			self:DrawDetailInfo()
		end
	end
end

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

function ENT:DrawInfo()
	local Pos = self:GetPos() + self:GetUp() * 80
	Pos = Pos + self:GetUp() * math.abs(math.sin(CurTime()) * 1)
	local Ang = Angle(0, LocalPlayer():EyeAngles().y - 90, 90)
	cam.Start3D2D(Pos, Ang, 0.1)
	draw.RoundedBox(25, -200, 50, 400, 50, Color(0, 0, 0, 200))
	local sellProfit = self:GetBuyRate()
	sellProfit = sellProfit - 100
	local pColor = Color(255, 30, 30)

	if (sellProfit < 0) then
		sellProfit = sellProfit
		pColor = Color(255, 30, 30)
	else
		sellProfit = "+ " .. sellProfit
		pColor = Color(30, 255, 30)
	end

	draw.DrawText(zrmine.language.NPC_SellProfit, "zrmine_npc_font2", -175, 60, Color(255, 255, 255), TEXT_ALIGN_LEFT)
	draw.DrawText(sellProfit .. "%", "zrmine_npc_font2", 190, 62, pColor, TEXT_ALIGN_RIGHT)
	draw.SimpleTextOutlined(zrmine.language.NPC_Title, "zrmine_npc_font1", 0, 20, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 3, Color(0, 0, 0, 25))
	cam.End3D2D()
end

local barIcon = Material("materials/zerochain/zrms/ui/zrms_bar.png", "smooth")
local offsetX, offsetY = -140, 185

function ENT:DrawResourceItem(Info, color, xpos, ypos, size)
	surface.SetDrawColor(color)
	surface.SetMaterial(barIcon)
	surface.DrawTexturedRect(xpos + offsetX, ypos + offsetY, size, size)
	draw.NoTexture()
	draw.DrawText(Info, "zrmine_npc_font4", xpos + offsetX + 30, ypos + offsetY + size * 0.25, Color(255, 255, 255), TEXT_ALIGN_LEFT)
end

function ENT:DrawDetailInfo()
	local Pos = self:GetPos() + self:GetUp() * 80
	Pos = Pos + self:GetUp() * math.abs(math.sin(CurTime()) * 1)
	local Ang = Angle(0, LocalPlayer():EyeAngles().y - 90, 90)
	cam.Start3D2D(Pos, Ang, 0.1)
	draw.RoundedBox(25, -200, 110, 120, 160, Color(0, 0, 0, 200))
	draw.DrawText(zrmine.language.NPC_CashPerBar, "zrmine_npc_font3", -140, 120, Color(255, 255, 255), TEXT_ALIGN_CENTER)
	local aIron = ": " .. math.Round(zrmine.config.BarValue["Iron"] * (self:GetBuyRate() / 100)) .. zrmine.config.BuyerNPC_Currency
	local aBronze = ": " .. math.Round(zrmine.config.BarValue["Bronze"] * (self:GetBuyRate() / 100)) .. zrmine.config.BuyerNPC_Currency
	local aSilver = ": " .. math.Round(zrmine.config.BarValue["Silver"] * (self:GetBuyRate() / 100)) .. zrmine.config.BuyerNPC_Currency
	local aGold = ": " .. math.Round(zrmine.config.BarValue["Gold"] * (self:GetBuyRate() / 100)) .. zrmine.config.BuyerNPC_Currency
	self:DrawResourceItem(aIron, Color(104, 85, 87), -55, -50, 30)
	self:DrawResourceItem(aBronze, Color(158, 72, 39), -55, -25, 30)
	self:DrawResourceItem(aSilver, Color(125, 125, 125), -55, 0, 30)
	self:DrawResourceItem(aGold, Color(189, 143, 49), -55, 25, 30)
	cam.End3D2D()
end

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

	return true
end
