include("shared.lua")

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

	if (anim == "crushing") then
		Belt_speed = speed * 2
		self.ScrollMat:SetFloat("$myspeed", Belt_speed)
	else
		Belt_speed = 0
		self.ScrollMat:SetFloat("$myspeed", Belt_speed)
	end
end

function ENT:RefineSound()
	local MoveSound = CreateSound(self, "zrmine_sfx_refinery_loop")

	if (self:GetCurrentState() == "CRUSHING") 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()
				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:RefineSound()
	self:SetNextClientThink(CurTime())

	return true
end

function ENT:Initialize()
	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 = 2
	self:SetSubMaterial(id, "!scrollmat" .. self:EntIndex())
	-- The Light Pixel handler
	self.PixVis = util.GetPixelVisibleHandle()
end

------------------------------//
local material = Material("sprites/light_ignorez")

function ENT:IndicatorLight()
	local LightPos = self:GetPos() + self:GetUp() * 42 + self:GetForward() * -19
	render.SetMaterial(material)
	local ViewNormal = self:GetPos() - EyePos()
	ViewNormal:Normalize()
	local Visibile = util.PixelVisible(LightPos, 4, self.PixVis)
	if (not Visibile or Visibile < 0.1) then return end
	local spriteColor = Color(255, 255, 255, 255)

	if (self:GetHasChild()) then
		spriteColor = Color(0, 255, 0, 255)
	else
		spriteColor = Color(255, 0, 0, 255)
	end

	render.DrawSprite(LightPos, 25, 25, spriteColor)
end

local OreIcon = Material("materials/zerochain/zrms/ui/zrms_ore.png", "smooth")
local offsetX, offsetY = 55, 18

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:GetAttachment(self:LookupAttachment("screen")).Pos + self:GetForward() * -0.3
	local Ang = self:GetAngles()
	Ang:RotateAroundAxis(self:GetForward(), 90)
	Ang:RotateAroundAxis(self:GetUp(), 90)

	if (self:GetCurrentState() == "CRUSHING") then
		status = zrmine.language.Crusher_Crushing
	elseif (self:GetCurrentState() == "WAITING_FOR_ORDER") then
		status = zrmine.language.Crusher_Waiting
	end

	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)
	local aAng = self:GetAngles()
	aAng:RotateAroundAxis(self:GetForward(), -90)
	aAng:RotateAroundAxis(self:GetUp(), 90)
	local amount = self:GetInMachine_Coal() + self:GetInMachine_Iron() + self:GetInMachine_Bronze() + self:GetInMachine_Silver() + self:GetInMachine_Gold()
	cam.Start3D2D(Pos, aAng, 0.1)
	draw.RoundedBox(0, -105, -65, 210, 130, Color(5, 5, 5))
	local aBar = (100 / zrmine.config.Crusher_Capacity) * amount

	if (aBar > 100) then
		aBar = 100
	end

	draw.RoundedBox(0, -87, -50, 30, aBar, Color(121, 111, 92))
	cam.End3D2D()
	cam.Start3D2D(Pos, Ang, 0.1)
	self:DrawResourceItem(aCoal, Color(85, 85, 85), -105, -75, 30)
	self:DrawResourceItem(aIron, Color(104, 85, 87), -105, -55, 30)
	self:DrawResourceItem(aBronze, Color(158, 72, 39), -105, -35, 30)
	self:DrawResourceItem(aSilver, Color(125, 125, 125), -105, -15, 30)
	self:DrawResourceItem(aGold, Color(189, 143, 49), -105, 5, 30)
	surface.SetDrawColor(Color(255, 255, 255))
	surface.SetMaterial(scaleIcon)
	surface.DrawTexturedRect(-110, -53, 75, 105)
	draw.NoTexture()
	cam.End3D2D()
end

function ENT:Draw()
	self:DrawModel()

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

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