include("shared.lua")

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

	if (self:GetCurrentState() == "REFINING") 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()
	-- This Sets the belt scroll speed of the mat
	local Belt_speed

	if (self:GetCurrentState() == "REFINING" or self:GetCurrentState() == "TRANSPORTING") then
		Belt_speed = 2
		self.ScrollMat:SetFloat("$myspeed", Belt_speed)
	else
		Belt_speed = 0
		self.ScrollMat:SetFloat("$myspeed", Belt_speed)
	end

	self:SetNextClientThink(CurTime())

	return true
end

function ENT:Setup_Conveyorbelt_Material()
	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("Refiner_ScrollMat" .. self:EntIndex(), "VertexLitGeneric", params)
	self:SetSubMaterial(1, "!Refiner_ScrollMat" .. self:EntIndex())
end

function ENT:Setup_PaintLack_Material()
	local resourceType = self.RefinerType
	local paintColor

	if (resourceType == "Iron") then
		paintColor = Vector(0.407, 0.3, 0.35)
	elseif (resourceType == "Bronze") then
		paintColor = Vector(0.61, 0.28, 0.15)
	elseif (resourceType == "Silver") then
		paintColor = Vector(0.6, 0.6, 0.6)
	elseif (resourceType == "Gold") then
		paintColor = Vector(0.74, 0.56, 0.19)
	elseif (resourceType == "Coal") then
		paintColor = Vector(0.3, 0.3, 0.3)
	end

	local params = {
		["$basetexture"] = "zerochain/props_mining/refinery/zrms_refiner_diff",
		["$color2"] = paintColor,
		["$blendTintByBaseAlpha"] = 1,
		["$blendTintColorOverBase"] = 0,
		["$bumpMap"] = "zerochain/props_mining/refinery/zrms_refiner_nrm",
		["$normalmapalphaenvmapmask"] = 1,
		["$surfaceprop"] = "metal",
		["$halflambert"] = 1,
		["$model"] = 1,
		["$envmap"] = "env_cubemap",
		["$envmaptint"] = paintColor,
		["$envmapfresnel"] = 1,
		["$phong"] = 1,
		["$phongexponenttexture"] = "zerochain/props_mining/refinery/zrms_refiner_phong",
		["$phongtint"] = paintColor,
		["$phongboost"] = 15,
		["$phongfresnelranges"] = Vector(0.05, 0.5, 1)
	}

	self.PaintMat = CreateMaterial("RefinerPaint" .. self:EntIndex(), "VertexLitGeneric", params)
	self.PaintMat:SetVector("$color2", paintColor)
	self.PaintMat:SetFloat("$blendTintColorOverBase", 0.1)
	self:SetSubMaterial(5, "!RefinerPaint" .. self:EntIndex())
end

function ENT:Initialize()
	self:Setup_Conveyorbelt_Material()
	self:Setup_PaintLack_Material()
	self.PixVis = util.GetPixelVisibleHandle()
	self.PixVisBasket = util.GetPixelVisibleHandle()
	self.InsertEffect = ParticleEmitter(self:GetPos())
end

local material = Material("sprites/light_ignorez")

function ENT:IndicatorLight_Refinery()
	local LightPos = self:GetPos() + self:GetUp() * 44 + self:GetForward() * -20
	render.SetMaterial(material)
	local ViewNormal = self:GetPos() - EyePos()
	ViewNormal:Normalize()
	local Visibile = util.PixelVisible(LightPos, 3, 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

function ENT:IndicatorLight_Basket()
	local LightPos = self:GetPos() + self:GetUp() * 28 + self:GetRight() * 15
	render.SetMaterial(material)
	local ViewNormal = self:GetPos() - EyePos()
	ViewNormal:Normalize()
	local Visibile = util.PixelVisible(LightPos, 3, self.PixVisBasket)
	if (not Visibile or Visibile < 0.1) then return end
	local spriteColor = Color(255, 255, 255, 255)

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

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

function ENT:Draw()
	self:DrawModel()

	if (LocalPlayer():GetPos():Distance(self:GetPos()) < 500 and self:GetBuildMode() == false) then
		self:IndicatorLight_Refinery()
		self:IndicatorLight_Basket()
		self:DrawInfo()
	end

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

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

		if (self:GetSubMaterial(5) ~= "!RefinerPaint" .. self:EntIndex()) then
			self:SetSubMaterial(5, "!RefinerPaint" .. 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 = 15, 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_screen_font1", xpos + offsetX + 50, ypos + offsetY + size * 0.22, 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:GetRight() * -1
	local Ang = self:GetAngles()
	Ang:RotateAroundAxis(self:GetForward(), 90)
	local status
	local aCoal = ": " .. math.Round(self:GetInMachine_Coal(), 2) .. "kg"
	local aIron = ": " .. math.Round(self:GetInMachine_Iron(), 2) .. "kg"
	local aBronze = ": " .. math.Round(self:GetInMachine_Bronze(), 2) .. "kg"
	local aSilver = ": " .. math.Round(self:GetInMachine_Silver(), 2) .. "kg"
	local aGold = ": " .. math.Round(self:GetInMachine_Gold(), 2) .. "kg"

	if (self:GetCurrentState() == "REFINING") then
		status = zrmine.language.Refiner_Refining
	elseif (self:GetCurrentState() == "WAITING_FOR_ORDER") then
		status = zrmine.language.Refiner_Waiting
	elseif (self:GetCurrentState() == "TRANSPORTING") then
		status = zrmine.language.Refiner_Moving
	end

	local aAng = self:GetAngles()
	aAng:RotateAroundAxis(self:GetForward(), -90)
	cam.Start3D2D(Pos, aAng, 0.1)
		draw.RoundedBox(0, -105, -65, 210, 130, Color(5, 5, 5))
		self:Draw_ResourceBar("Coal", 0)
		self:Draw_ResourceBar("Iron", 6)
		self:Draw_ResourceBar("Bronze", 12)
		self:Draw_ResourceBar("Silver", 18)
		self:Draw_ResourceBar("Gold", 24)
	cam.End3D2D()

	cam.Start3D2D(Pos, Ang, 0.1)
		draw.DrawText(self.RefinerTypeName, "zrmine_screen_font2", 15, -55, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER)
		draw.DrawText(status, "zrmine_screen_font3", 15, -15, Color(229, 179, 72), TEXT_ALIGN_CENTER)

		if (self.RefinerType == "Coal") then
			self:DrawResourceItem(aCoal, Color(85, 85, 85), -60, 0, 50)
		elseif (self.RefinerType == "Iron") then
			self:DrawResourceItem(aIron, Color(104, 85, 87), -60, 0, 50)
		elseif (self.RefinerType == "Bronze") then
			self:DrawResourceItem(aBronze, Color(158, 72, 39), -60, 0, 50)
		elseif (self.RefinerType == "Silver") then
			self:DrawResourceItem(aSilver, Color(125, 125, 125), -60, 0, 50)
		elseif (self.RefinerType == "Gold") then
			self:DrawResourceItem(aGold, Color(189, 143, 49), -60, 0, 50)
		end

		surface.SetDrawColor(Color(255, 255, 255))
		surface.SetMaterial(scaleIcon)
		surface.DrawTexturedRect(-110, -53, 75, 105)
		draw.NoTexture()
	cam.End3D2D()
end

function ENT:Draw_ResourceBar(rtype, xOffset)
	local ramount = 0

	if (rtype == "Coal") then
		ramount = self:GetInMachine_Coal()
	elseif (rtype == "Iron") then
		ramount = self:GetInMachine_Iron()
	elseif (rtype == "Bronze") then
		ramount = self:GetInMachine_Bronze()
	elseif (rtype == "Silver") then
		ramount = self:GetInMachine_Silver()
	elseif (rtype == "Gold") then
		ramount = self:GetInMachine_Gold()
	end

	local rpaintColor = Color(121, 111, 92)

	if (rtype == "Iron") then
		rpaintColor = Color(104, 85, 87)
	elseif (rtype == "Bronze") then
		rpaintColor = Color(158, 72, 39)
	elseif (rtype == "Silver") then
		rpaintColor = Color(125, 125, 125)
	elseif (rtype == "Gold") then
		rpaintColor = Color(189, 143, 49)
	elseif (rtype == "Coal") then
		rpaintColor = Color(85, 85, 85)
	end

	local refCap = zrmine.config.Refiner_Capacity
	local r_Bar = (100 / refCap) * ramount

	if (r_Bar > 100) then
		r_Bar = 100
	end

	draw.RoundedBox(0, -87 + xOffset, -50, 5, r_Bar, rpaintColor)
end
------------------------------//
