include("shared.lua")

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

	if (LocalPlayer():GetPos():Distance(self:GetPos()) < 300) then
		self:Draw_MainInfo()
		self:Draw_CoalInfo()
	end

	if (LocalPlayer():GetPos():Distance(self:GetPos()) < 600) then
		self:MelterLight()

		if (IsValid(self) and self:GetCurrentState() == "WAITING_FOR_ORDER") then
			self:StopParticlesNamed("zrms_melter_unload")
			self:StopParticlesNamed("zrms_melter_chimney")
			self:StopParticlesNamed("zrms_melter_head")
			self:StopParticlesNamed("zrms_melter_coalpit")
		end
	end
end

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

function ENT:Draw_CoalInfo()
	local nPos = self:GetPos() + self:GetForward() * 80 + self:GetUp() * 10
	local nAng = self:GetAngles()
	nAng:RotateAroundAxis(self:GetRight(), 90)
	nAng:RotateAroundAxis(self:GetForward(), -90)
	local amount = tostring(math.Round(self:GetCoalAmount(), 2))
	local aBar = (100 / zrmine.config.Melter_Coal_Capacity) * amount

	if (aBar > 100) then
		aBar = 100
	end

	cam.Start3D2D(nPos, nAng, 0.1)
	draw.RoundedBox(0, -105, -65, 210, 130, Color(5, 5, 5))
	draw.RoundedBox(0, -92, -50, 30, aBar, Color(85, 85, 85))
	cam.End3D2D()
	local Pos = self:GetPos() + self:GetForward() * 80 + self:GetUp() * 10
	local Ang = self:GetAngles()
	Ang:RotateAroundAxis(self:GetForward(), 90)
	Ang:RotateAroundAxis(self:GetUp(), 90)
	cam.Start3D2D(Pos, Ang, 0.1)
	surface.SetDrawColor(Color(85, 85, 85))
	surface.SetMaterial(OreIcon)
	surface.DrawTexturedRect(-50, -75, 100, 100)
	draw.NoTexture()
	draw.DrawText(amount .. zrmine.config.BuyerNPC_Mass, "zrmine_screen_font2", 0, 10, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER)
	surface.SetDrawColor(Color(255, 255, 255))
	surface.SetMaterial(scaleIcon)
	surface.DrawTexturedRect(-115, -53, 75, 105)
	draw.NoTexture()
	cam.End3D2D()
end

function ENT:Draw_MainInfo()
	local status = ""
	local amount = math.Round(self:GetResourceAmount(), 2)
	local resourceType = self:GetResourceType()
	local paintColor = Color(255, 0, 0)
	local cState = self:GetCurrentState()

	if (cState == "WAITING_FOR_ORDER") then
		status = zrmine.language.Melter_InsertOre
	elseif (cState == "CHAMBER_FULL" and self:GetCoalAmount() < zrmine.config.Melter_Vars[resourceType].CoalAmount) then
		status = zrmine.language.Melter_NeedCoal
	elseif (cState == "MELTING") then
		status = zrmine.language.Melter_Melting
	end

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

	local aBar = 0

	if (resourceType ~= "Empty") then
		aBar = (100 / zrmine.config.Melter_Vars[resourceType].OreAmount) * amount
	end

	if (aBar > 100) then
		aBar = 100
	end

	local nPos = self:GetAttachment(self:LookupAttachment("screen")).Pos + self:GetRight() * 0.2
	local nAng = self:GetAngles()
	nAng:RotateAroundAxis(self:GetForward(), -90)
	cam.Start3D2D(nPos, nAng, 0.1)
	draw.RoundedBox(0, -105, -65, 210, 130, Color(5, 5, 5))
	draw.RoundedBox(0, -92, -50, 30, aBar, paintColor)
	cam.End3D2D()
	local rType = "nil"

	if (resourceType == "Coal") then
		rType = zrmine.language.Ore_Coal
	elseif (resourceType == "Iron") then
		rType = zrmine.language.Ore_Iron
	elseif (resourceType == "Bronze") then
		rType = zrmine.language.Ore_Bronze
	elseif (resourceType == "Silver") then
		rType = zrmine.language.Ore_Silver
	elseif (resourceType == "Gold") then
		rType = zrmine.language.Ore_Gold
	end

	local rtype = rType

	if (amount > 0) then
		rtype = rType
	else
		rtype = zrmine.language.Ore_Empty
	end

	local Pos = self:GetAttachment(self:LookupAttachment("screen")).Pos + self:GetRight() * 0.2 + self:GetForward() * 1
	local Ang = self:GetAngles()
	Ang:RotateAroundAxis(self:GetForward(), 90)
	cam.Start3D2D(Pos, Ang, 0.1)

	if (status ~= nil) then
		draw.DrawText(rtype, "zrmine_screen_font2", 0, -50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER)
		draw.DrawText(status, "zrmine_screen_font3", 0, 16, Color(229, 179, 72), TEXT_ALIGN_CENTER)
		draw.DrawText(amount .. zrmine.config.BuyerNPC_Mass, "zrmine_screen_font1", 0, -20, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER)
		surface.SetDrawColor(Color(255, 255, 255))
		surface.SetMaterial(scaleIcon)
		surface.DrawTexturedRect(-125, -53, 75, 105)
		draw.NoTexture()
	end

	cam.End3D2D()
end

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

------------------------------//
function ENT:MelterSound()
	local MeltSound = CreateSound(self, "zrmine_sfx_melter_loop")
	local CoolSound = CreateSound(self, "zrmine_sfx_meltercool_loop")

	if (self:GetCurrentState() == "MELTING") then
		if self.SoundObj01 == nil then
			self.SoundObj01 = MeltSound
		end

		if self.SoundObj01:IsPlaying() == false then
			--self:EmitSound("zms_drill_start")
			self.SoundObj01:Play()
			self.SoundObj01:ChangeVolume(0, 0)
			self.SoundObj01:ChangeVolume(1, 1)
		end
	elseif (self:GetCurrentState() == "UNLOADING") then
		if self.SoundObj02 == nil then
			self.SoundObj02 = CoolSound
		end

		if self.SoundObj02:IsPlaying() == false then
			--self:EmitSound("zms_drill_start")
			self.SoundObj02:Play()
			self.SoundObj02:ChangeVolume(0, 0)
			self.SoundObj02:ChangeVolume(1, 1)
		end
	else
		if self.SoundObj01 == nil then
			self.SoundObj01 = MeltSound
		end

		if self.SoundObj01:IsPlaying() == true then
			self.SoundObj01: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.SoundObj01:Stop()
				end
			end)
		end

		if self.SoundObj02 == nil then
			self.SoundObj02 = CoolSound
		end

		if self.SoundObj02:IsPlaying() == true then
			self.SoundObj02: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.SoundObj02:Stop()
				end
			end)
		end
	end
end

function ENT:MelterLight()
	if (self:GetCurrentState() == "MELTING" or self:GetCurrentState() == "UNLOADING") then
		local dlight01 = DynamicLight(self:EntIndex())

		if (dlight01) then
			dlight01.pos = self:GetPos() + self:GetUp() * 15
			dlight01.r = 255
			dlight01.g = 75
			dlight01.b = 0
			dlight01.brightness = 1
			dlight01.Decay = 1000
			dlight01.Size = 512
			dlight01.DieTime = CurTime() + 1
		end
	end
end

function ENT:Think()
	self:MelterSound()

	if (IsValid(self.csModel)) then
		self.csModel:SetPos(self:GetPos())
		self.csModel:SetAngles(self:GetAngles())
	else
		self:CreateClientSideModel()
	end

	if (IsValid(self.csModel_Coal)) then
		self.csModel_Coal:SetPos(self:GetPos())
		self.csModel_Coal:SetAngles(self:GetAngles())
	else
		self:CreateClientSideModel_Coal()
	end

	self:SetNextClientThink(CurTime())

	return true
end

function ENT:OnRemove()
	self.csModel:Remove()
	self.csModel_Coal:Remove()
end

-- Stencil stuff
ZRMS_CSM_MELTER = ZRMS_CSM_MELTER or {}

function ENT:CreateClientSideModel()
	self.csModel = ClientsideModel("models/zerochain/props_mining/zrms_melter_burnchamber.mdl")

	if (IsValid(self.csModel) and IsValid(self)) then
		self.csModel:SetPos(self:GetPos())
		self.csModel:SetAngles(self:GetAngles())
		self.csModel:SetParent(self)
		self.csModel:SetNoDraw(true)
	end
end

function ENT:CreateClientSideModel_Coal()
	self.csModel_Coal = ClientsideModel("models/zerochain/props_mining/zrms_melter_burningcoal.mdl")

	if (IsValid(self.csModel_Coal) and IsValid(self)) then
		self.csModel_Coal:UseClientSideAnimation()
		self.csModel_Coal:SetPos(self:GetPos())
		self.csModel_Coal:SetAngles(self:GetAngles())
		self.csModel_Coal:SetParent(self)
		self.csModel_Coal:SetNoDraw(true)
	end
end

function ENT:Initialize()
	ZRMS_CSM_MELTER[self:EntIndex()] = self
end

hook.Add("PreDrawTranslucentRenderables", "zrmsRenderMelter", function(depth, skybox)
	if skybox then return end
	if depth then return end

	for k, s in pairs(ZRMS_CSM_MELTER) do
		if not IsValid(s) then continue end
		if (LocalPlayer():GetPos():Distance(s:GetPos()) > 700) then continue end
		if s:GetIsLowered() then continue end
		render.ClearStencil()
		render.SetStencilEnable(true)
		render.SetStencilWriteMask(255)
		render.SetStencilTestMask(255)
		render.SetStencilReferenceValue(57)
		render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_ALWAYS)
		render.SetStencilPassOperation(STENCILOPERATION_REPLACE)
		render.SetStencilFailOperation(STENCIL_ZERO)
		render.SetStencilZFailOperation(STENCIL_ZERO)
		local angle = s:GetAngles()
		cam.Start3D2D(s:GetPos(), angle, 1)
		surface.SetDrawColor(0, 200, 255, 200)
		draw.NoTexture()
		draw.RoundedBox(0, -23, -23, 46, 46, Color(255, 255, 255, 255))
		cam.End3D2D()
		render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_EQUAL)
		render.SuppressEngineLighting(false)
		render.DepthRange(0, 0.8)

		if (IsValid(s.csModel) and IsValid(s.csModel_Coal)) then

			if (s:GetCurrentState() == "UNLOADING") then
				s.csModel_Coal:DrawModel()
			end

			s.csModel:DrawModel()
		end

		render.SuppressEngineLighting(false)
		render.SetStencilEnable(false)
		render.DepthRange(0, 1)
	end
end)
