include("shared.lua")

function ENT:Initialize()
	self.InsertEffect = ParticleEmitter(self:GetPos())
end

function ENT:SplittSound()
	local MoveSound = CreateSound(self, "zrmine_sfx_conveyorbelt_move")

	if (self:GetCurrentState() == "TRANSPORTING") then
		if self.SoundObj == nil then
			self.SoundObj = MoveSound
		end

		if self.SoundObj:IsPlaying() == false then
			self.SoundObj:Play()
			self.SoundObj:ChangeVolume(0, 0)
			self.SoundObj:ChangeVolume(1, 1)
		end
	else
		if self.SoundObj == nil then
			self.SoundObj = MoveSound
		end

		if self.SoundObj:IsPlaying() == true then
			self.SoundObj:ChangeVolume(0, 1)
			if ((self.lastSoundStop or CurTime()) > CurTime()) then return end
			self.lastSoundStop = CurTime() + 5

			timer.Simple(2, function()
				if (IsValid(self)) then
					self.SoundObj:Stop()
				end
			end)
		end
	end
end

function ENT:OnRemove()
	if (self.SoundObj ~= nil and self.SoundObj:IsPlaying()) then
		self.SoundObj:Stop()
	end
end

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

	return true
end

function ENT:Draw()
	self:DrawModel()

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

	if (self.ShowInfo or false == true) then
		self:DrawInfo02()
	end
end

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

local OreIcon = Material("materials/zerochain/zrms/ui/zrms_ore.png", "smooth")
function ENT:DrawInfo()
	local Pos = self:GetPos() + self:GetUp() * 18.9
	local Ang = self:GetAngles()
	Ang:RotateAroundAxis(self:GetForward(), 0)
	cam.Start3D2D(Pos, Ang, 0.1)
		draw.RoundedBox(0, -55, -55, 110, 110, Color(25, 25, 25))
		local col = Color(85, 85, 85)

		if (self.FilterType == 0) then
			col = Color(85, 85, 85)
		elseif (self.FilterType == 1) then
			col = Color(104, 85, 87)
		elseif (self.FilterType == 2) then
			col = Color(158, 72, 39)
		elseif (self.FilterType == 3) then
			col = Color(125, 125, 125)
		elseif (self.FilterType == 4) then
			col = Color(189, 143, 49)
		end

		surface.SetDrawColor(col)
		surface.SetMaterial(OreIcon)
		surface.DrawTexturedRect(-50, -50, 100, 100)
		draw.NoTexture()
	cam.End3D2D()
end


local offsetX, offsetY = 1, 10

function ENT:DrawResourceItem(Info, color, xpos, ypos, size)
	surface.SetDrawColor(color)
	surface.SetMaterial(OreIcon)
	surface.DrawTexturedRect(xpos + offsetX, ypos + offsetY, size, size)
	draw.NoTexture()
	draw.DrawText(Info .. zrmine.config.BuyerNPC_Mass, "zrmine_screen_font3", xpos + offsetX + 30, ypos + offsetY + size * 0.25, color, TEXT_ALIGN_LEFT)
end

function ENT:DrawInfo02()
	local Pos = self:GetPos() + self:GetUp() * 11 + self:GetRight() * 11.3
	local Ang = self:GetAngles()
	Ang:RotateAroundAxis(self:GetForward(), 90)
	local aCoal = ": " .. math.Round(self:GetInMachine_Coal(), 2)
	local aIron = ": " .. math.Round(self:GetInMachine_Iron(), 2)
	local aBronze = ": " .. math.Round(self:GetInMachine_Bronze(), 2)
	local aSilver = ": " .. math.Round(self:GetInMachine_Silver(), 2)
	local aGold = ": " .. math.Round(self:GetInMachine_Gold(), 2)
	cam.Start3D2D(Pos, Ang, 0.1)
		draw.RoundedBox(0, -105, -55, 210, 110, Color(25, 25, 25))
		draw.RoundedBox(0, -100, -50, 200, 100, Color(5, 5, 5))
		self:DrawResourceItem(aCoal, Color(85, 85, 85), -90, -60, 30)
		self:DrawResourceItem(aIron, Color(104, 85, 87), -90, -35, 30)
		self:DrawResourceItem(aBronze, Color(158, 72, 39), -90, -10, 30)
		self:DrawResourceItem(aSilver, Color(125, 125, 125), -5, -60, 30)
		self:DrawResourceItem(aGold, Color(189, 143, 49), -5, -35, 30)
	cam.End3D2D()
end
