include("shared.lua")

function ENT:Setup_BeltMaterial()
	local params = {
		["$basetexture"] = "zerochain/props_mining/conveyorbelt/zrms_conveyorbelt_belt_diff",
		["$bumpMap"] = "zerochain/props_mining/conveyorbelt/zrms_conveyorbelt_belt_nrm",
		["$normalmapalphaenvmapmask"] = 1,
		["$surfaceprop"] = "metal",
		["$halflambert"] = 1,
		["$model"] = 1,
		["$envmap"] = "env_cubemap",
		["$envmaptint"] = Vector(0.01, 0.01, 0.01),
		["$envmapfresnel"] = 1,
		["$phong"] = 1,
		["$phongexponenttexture"] = "zerochain/props_mining/conveyorbelt/zrms_conveyorbelt_belt_phong",
		["$phongtint"] = Vector(1, 1, 1),
		["$phongboost"] = 25,
		["$phongfresnelranges"] = Vector(0.05, 0.5, 1),
		["$myspeed"] = 0,
		Proxies = {
			TextureScroll = {
				texturescrollvar = "$baseTexturetransform",
				texturescrollrate = "$myspeed",
				texturescrollangle = -90
			}
		}
	}

	self.ScrollMat = CreateMaterial("scrollmat" .. self:EntIndex(), "VertexLitGeneric", params)
	local id

	if (self:GetClass() == "zrms_conveyorbelt_n") then
		id = 0
	elseif (self:GetClass() == "zrms_conveyorbelt_s") then
		id = 0
	else
		id = 3
	end

	self:SetSubMaterial(id, "!scrollmat" .. self:EntIndex())
end

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

function ENT:CustomAnimation(anim)
	-- This Sets the belt scroll speed of the mat
	local Belt_speed

	if (anim == "output") then
		Belt_speed = 10 / self.TransportSpeed
		self.ScrollMat:SetFloat("$myspeed", Belt_speed)
	else
		Belt_speed = 0
		self.ScrollMat:SetFloat("$myspeed", Belt_speed)
	end
end

function ENT:MoveSound()
	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:EmitSound("zms_drill_start")
			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()
					--self.Entity:EmitSound("zms_drill_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:MoveSound()
	self:SetNextClientThink(CurTime())

	return true
end

------------------------------//
function ENT:Draw()
	self:DrawModel()

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

	if (LocalPlayer():GetPos():Distance(self:GetPos()) < 1000 and CurTime() > (self.lastMatSet or 1)) then
		self.lastMatSet = CurTime() + 3
		local id

		if (self:GetClass() == "zrms_conveyorbelt_n") then
			id = 0
		elseif (self:GetClass() == "zrms_conveyorbelt_s") then
			id = 0
		else
			id = 3
		end

		if (self:GetSubMaterial(id) ~= "!scrollmat" .. self:EntIndex()) then
			self:SetSubMaterial(id, "!scrollmat" .. self:EntIndex())
		end
	end
end

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

local OreIcon = Material("materials/zerochain/zrms/ui/zrms_ore.png", "smooth")
local offsetX, offsetY = -3, 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

local scaleIcon = Material("materials/zerochain/zrms/ui/zrms_melter_scale.png", "smooth")

function ENT:DrawInfo()
	local Pos = self:GetPos() + self:GetUp() * 17
	local Ang = self:GetAngles()
	Ang:RotateAroundAxis(self:GetForward(), 0)
	local aCoal = ": " .. math.Round(self:GetOnBelt_Coal(), 2)
	local aIron = ": " .. math.Round(self:GetOnBelt_Iron(), 2)
	local aBronze = ": " .. math.Round(self:GetOnBelt_Bronze(), 2)
	local aSilver = ": " .. math.Round(self:GetOnBelt_Silver(), 2)
	local aGold = ": " .. math.Round(self:GetOnBelt_Gold(), 2)
	local amount = self:GetOnBelt_Coal() + self:GetOnBelt_Iron() + self:GetOnBelt_Bronze() + self:GetOnBelt_Silver() + self:GetOnBelt_Gold()
	local aAng = self:GetAngles()
	aAng:RotateAroundAxis(self:GetUp(), 180)
	cam.Start3D2D(Pos, aAng, 0.1)
	draw.RoundedBox(0, -65, -75, 130, 150, Color(35, 35, 35))
	draw.RoundedBox(0, -60, -70, 120, 140, Color(5, 5, 5))
	local aBar = (120 / zrmine.config.Belt_Capacity) * amount

	if (aBar > 120) then
		aBar = 120
	end

	draw.RoundedBox(0, -52, -60, 20, aBar, Color(121, 111, 92))
	cam.End3D2D()
	cam.Start3D2D(Pos, Ang, 0.1)
	self:DrawResourceItem(aCoal, Color(85, 85, 85), -55, -75, 30)
	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)
	surface.SetDrawColor(Color(255, 255, 255))
	surface.SetMaterial(scaleIcon)
	surface.DrawTexturedRect(17, -62, 50, 125)
	draw.NoTexture()
	cam.End3D2D()
end
------------------------------//
