include("shared.lua")


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

	self:DrawModel()

	if(LocalPlayer():GetPos():Distance(self:GetPos()) < 300)then
		self:Draw_MainInfo()
		if (table.Count(self.ConnectedPizzas) > 0) then
			if(self.ConnectedPizzas[1] ~= nil)then
				self:BakeLight(0)
			end
			if(self.ConnectedPizzas[2] ~= nil)then
				self:BakeLight(15)
			end
		end
	end

end

function ENT:Initialize()
	self.ConnectedPizzas = {}
end

net.Receive( "zpizmak_Oven_PizzaTransfer", function( len, ply )
	local oven = net.ReadEntity()
	local PizzaTable = net.ReadTable()
	if(PizzaTable && IsValid(oven))then
		table.CopyFromTo( PizzaTable, oven.ConnectedPizzas )
	end
end)

function ENT:Draw_PizzaProgress(pizza,pos)
	if(IsValid(pizza))then
		local pizzaInfo = zpizmak.f.GetPizzaByName(pizza:GetPizzaType())
		local pizzaState = pizza:GetCurrentState_Index()
		local pizzaBakeTime = pizza:GetBakeTime()

		local status = zpizmak.language.Oven_PizzaBaking

		if(pizzaState == 3)then
			status = zpizmak.language.Oven_PizzaReady
		elseif(pizzaState == 4)then
			status = zpizmak.language.Oven_PizzaBurned
		end


		local aBar = 0
		aBar = (400/pizzaInfo.BakeTime) * pizzaBakeTime

		if(aBar > 400)then
			aBar = 400
		end
		local t = (1/pizzaInfo.BakeTime) * pizzaBakeTime
		local barColor =  zpizmak.f.LerpColor(t, Color(238,207,167), Color(192,125,46))

		if(pizzaBakeTime >= pizzaInfo.BakeTime + zpizmak.config.PizzaBurnTime)then
			barColor = Color(160,0,0)
		end

		local YOffset = 0
		if(pos == 1)then
			YOffset = 0
		elseif(pos == 2)then
			YOffset = -177
		end

		draw.RoundedBox( 0, -205, 25+YOffset, 410, 25, Color(5,5,5) )
		draw.RoundedBox( 0, -200, 27+YOffset, aBar, 20, barColor )
		draw.DrawText(status, "zpizmak_oven_font01",0, 30+YOffset,  Color(255,255,255), TEXT_ALIGN_CENTER  )
	end
end

function ENT:BakeLight(y)

	//debugoverlay.Sphere( self:GetPos()+self:GetUp()*(45+y),15, 0.1, Color( 0, 255, 0,100 ), false )

	local dlight01 = DynamicLight(self:EntIndex() )
	if ( dlight01 ) then
		dlight01.pos = self:GetPos()+self:GetUp()*(45+y)
		dlight01.r = 255
		dlight01.g = 75
		dlight01.b = 0
		dlight01.brightness = 1
		dlight01.Decay = 1000
		dlight01.Size = 256
		dlight01.DieTime = CurTime() + 1
	end
end

function ENT:Draw_MainInfo()

	local nPos = self:GetPos() + self:GetForward()*24.5 + self:GetUp()*54 + self:GetRight()*1.7
	local nAng = self:GetAngles()

	nAng:RotateAroundAxis(self:GetForward(),-90)
	nAng:RotateAroundAxis(self:GetUp(),90)
	nAng:RotateAroundAxis(self:GetRight(),180)

	cam.Start3D2D(nPos , nAng, 0.1)

		if(table.Count(self.ConnectedPizzas) > 0)then

			for k, v in pairs(self.ConnectedPizzas) do
				self:Draw_PizzaProgress(v,k)
			end
		end

		//draw.RoundedBox( 0, -200, 0, 400, 50, Color(5,5,5) )
	cam.End3D2D()
end

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


function ENT:HasPizza()
	local hasPizza = false
	for k, v in pairs(self.ConnectedPizzas) do
		if(IsValid(v))then
			hasPizza = true
			break
		end
	end
	return hasPizza
end

function ENT:BakeSound()
	local BakeSound =  CreateSound(self,"zpizmak_sfx_ovenbake")
	if (self:HasPizza()) then
		if self.SoundObj == nil then self.SoundObj = BakeSound 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 = BakeSound 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(1,function()
				if(IsValid(self))then
					self.SoundObj:Stop()
				end
			end)
		end
	end
end


function ENT:Think()
	self:SetNextClientThink( CurTime())
	self:BakeSound()
    return true
end

function ENT:OnRemove() end
