include("shared.lua")


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

net.Receive("zpizmak_update_cl_ingredients",function(len,ply)
	local ent = net.ReadEntity()
	ent.cl_NeededIngredients = net.ReadTable()
end)
net.Receive("zpizmak_add_cl_ingredient",function(len,ply)
	local ent = net.ReadEntity()
	local ing = net.ReadString()
	if(IsValid(ent) && ing ~= nil)then
		table.insert(ent.cl_AddedIngredients,ing)
	end
end)

function ENT:Build_IngredientsTable(ingTable)
	local newIngTable = {}
	for k, v in pairs(ingTable) do
		local offSize = 25
		local offset = self:GetRight()*math.random(-offSize,offSize)
		offset = offset + self:GetForward()*math.random(-offSize,offSize)
		offset = offset + self:GetUp()*math.random(-offSize/25,offSize/25)

		newIngTable[k] = {Name = v, Pos = offset}
	end
	return newIngTable
end

// A Clean way do display the ingredients
local CircleIcon = Material("materials/zerochain/zpizmak/ui/zpizmak_circle.png", "smooth")
local size = 60
function ENT:DrawIngredientItem(icon,y)

	surface.SetDrawColor(  Color(0,0,0,100) )
	surface.SetMaterial(CircleIcon)
	surface.DrawTexturedRect( size*-0.5, 0 + y ,size*1.5,size*1.5 )
	draw.NoTexture()

	surface.SetDrawColor( Color(255,255,255,255) )
	surface.SetMaterial(icon)
	surface.DrawTexturedRect( size*-0.25 , size*0.25 + y ,size,size )
	draw.NoTexture()
end
function ENT:Draw_DetailInfo01()
	local Pos = self:GetPos() + self:GetUp()*15
	local Ang = Angle(0,LocalPlayer():EyeAngles().y - 90,90)
	cam.Start3D2D(Pos , Ang, 0.1)

		if(table.Count(self.cl_NeededIngredients) > 0 && self:GetCurrentState_Index() == 1)then
			local y = 0
			for k, v in pairs(self.cl_NeededIngredients) do

				self:DrawIngredientItem(zpizmak.f.GetIngredientByName(v).Icon,y)
				y = y -80
			end

		end
	cam.End3D2D()
end



local IngredientTopIcon = Material("materials/zerochain/zpizmak/ui/zpizmak_ingredient_top.png", "smooth")
function ENT:DrawIngredientDetail(color,rot)

	surface.SetDrawColor( color )
	surface.SetMaterial(IngredientTopIcon)
	surface.DrawTexturedRectRotated( 0 , 0,200,200 , rot )

	draw.NoTexture()
end
function ENT:Draw_Detail02()
	local cState = self:GetCurrentState_Index()

	if(cState > 2)then return end

	local Pos = self:GetPos() + self:GetUp()*1.6
	local Ang = self:GetAngles()

	cam.Start3D2D(Pos , Ang, 0.1)

		if(table.Count(self.cl_AddedIngredients) > 0 )then
			local rot = 0

			for k, v in pairs(self.cl_AddedIngredients) do
				if(v ~= "TomatoSauce" && v ~= "Dough")then
					self:DrawIngredientDetail(zpizmak.f.GetIngredientByName(v).iColor,rot)
					rot = rot + 90
				end
			end

		end

	cam.End3D2D()
end

function ENT:Draw()
	self:DrawModel()
	if(LocalPlayer():GetPos():Distance(self:GetPos()) < 300)then

		self:Draw_DetailInfo01()
		self:Draw_Detail02()

		self:Draw_MainInfo()
	end
end

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


local ButtonIcon = Material("materials/zerochain/zpizmak/ui/zpizmak_button.png", "smooth")
function ENT:Draw_MainInfo()

	local cState = self:GetCurrentState_Index()

	local Ang = self:GetAngles()

	local pType = self:GetPizzaType()

	local aPos = self:GetPos() + self:GetUp() * 2

	cam.Start3D2D(aPos , Ang, 0.1)
		if(cState == 3)then
			surface.SetDrawColor( 255, 255, 255, 255 )
			surface.SetMaterial( zpizmak.f.GetPizzaByName(pType).PizzaTop )
			surface.DrawTexturedRect( -110, -110, 220, 220 )
		elseif(cState == 4)then
			surface.SetDrawColor( 116, 81, 61, 255 )
			surface.SetMaterial( zpizmak.f.GetPizzaByName(pType).PizzaTop )
			surface.DrawTexturedRect( -113, -113, 226, 226 )
		end
	cam.End3D2D()
end

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