if !CLIENT then return end


local wMod = ScrW() / 1920
local hMod = ScrH() / 1080

local ButtonIcon = Material("materials/zerochain/zpizmak/ui/zpizmak_button.png", "smooth")
local squareIcon = Material("materials/zerochain/zpizmak/ui/zpizmak_square.png", "smooth")
local circleeIcon = Material("materials/zerochain/zpizmak/ui/zpizmak_circle.png", "smooth")
local BgPanel = Material("materials/zerochain/zpizmak/ui/zpizmak_ui_panel.png", "smooth")
local Button02Icon = Material("materials/zerochain/zpizmak/ui/zpizmak_button02.png", "smooth")

// The RecipeMenu
local zpizmak_RecipeMenu = {}
local zpizmak_Main = {}

function zpizmak_RecipeMenu:Init()
	self:SetSize( 800*wMod, 1000*hMod )
	self:Center()
	self:MakePopup()

	zpizmak_Main.Title = vgui.Create( "DLabel", self )
	zpizmak_Main.Title:SetPos( 45*wMod, 25*hMod )
	zpizmak_Main.Title:SetSize( 260*wMod, 125*hMod )
	zpizmak_Main.Title:SetFont( "zpizmak_vgui_font01" )
	zpizmak_Main.Title:SetText( zpizmak.language.VGUI_Recipe )
	zpizmak_Main.Title:SetColor(Color(255,255,255,255))

	zpizmak_Main.close = vgui.Create( "DButton", self )
	zpizmak_Main.close:SetText("")
	zpizmak_Main.close:SetPos( 685*wMod, 55*hMod  )
	zpizmak_Main.close:SetSize( 70*wMod, 70*hMod )
	zpizmak_Main.close.DoClick = function()
		self:SetVisible(false)
		//self:Remove()
	end
	zpizmak_Main.close.Paint = function( self, w, h )
		if zpizmak_Main.close:IsHovered() then
			surface.SetDrawColor( 255, 160, 160, 255 )
		else
			surface.SetDrawColor( 255, 125, 125, 255 )
		end
		surface.SetMaterial( Button02Icon )
		surface.DrawTexturedRect( 0, 0, w, h )

		surface.SetFont( "zpizmak_vgui_font02" )
		surface.SetTextColor( 255, 255, 255, 255 )
		surface.SetTextPos( 20*wMod, 10*hMod )
		surface.DrawText( "X" )
	end

	RecipeItems( self, 1 )
end
function zpizmak_RecipeMenu:Paint( w, h )
	surface.SetDrawColor( 179, 135, 84, 255 )
	surface.SetMaterial(BgPanel)
	surface.DrawTexturedRect( 0, 0, w, h )
end

function RecipeItems( parent, content )
	zpizmak_items = {}
	zpizmak_items.panel = vgui.Create( "DScrollPanel", parent )
	zpizmak_items.panel:SetSize( 300*wMod, 200*hMod )
	zpizmak_items.panel:DockMargin(50*wMod,150*hMod,50*wMod,50*hMod)
	zpizmak_items.panel:Dock(FILL)

	zpizmak_items.panel:GetVBar().Paint = function() return true end
	zpizmak_items.panel:GetVBar().btnUp.Paint = function() return true end
	zpizmak_items.panel:GetVBar().btnDown.Paint = function() return true end
	//zpizmak_items.panel:GetVBar().btnGrip.Paint = function() return true end

	zpizmak_items.panel.Paint = function( self, w, h )
		surface.SetDrawColor( 0, 0, 0, 100 )
		surface.SetMaterial( squareIcon )
		surface.DrawTexturedRect( 0, 0, w, h )
	end

	zpizmak_items.list = vgui.Create( "DIconLayout", zpizmak_items.panel)
	zpizmak_items.list:SetSize( 685*wMod, 200*hMod )
	zpizmak_items.list:SetPos( 0*wMod, 17*hMod )
	zpizmak_items.list:SetSpaceY( 10 )

	if zpizmak.config.Pizzas ~= nil then

		for i = 1,table.Count(zpizmak.config.Pizzas) do

			zpizmak_items[i] = zpizmak_items.list:Add("DPanel")
			zpizmak_items[i]:SetSize( zpizmak_items.list:GetWide(), 100 )
			zpizmak_items[i].Paint = function( self, w, h )
				local panelcolor = Color(0,0,0,200)
				if zpizmak_items[i].button:IsHovered() then
					surface.SetDrawColor( panelcolor.r, panelcolor.g, panelcolor.b ,panelcolor.a)
				else
					surface.SetDrawColor( panelcolor.r, panelcolor.g, panelcolor.b ,panelcolor.a*0.5)
				end
				surface.SetMaterial( squareIcon )
				surface.DrawTexturedRect( 15*wMod, 0, w-25, h  )
			end

			zpizmak_items[i].ImageBG = vgui.Create( "DImage", zpizmak_items[i] )
			zpizmak_items[i].ImageBG:SetPos( 24*wMod, 1*hMod )
			zpizmak_items[i].ImageBG:SetSize( 120*wMod, 120*hMod )
			zpizmak_items[i].ImageBG.Paint = function( self, w, h )
				surface.SetDrawColor(204,112,55)
				surface.SetMaterial( circleeIcon )
				surface.DrawTexturedRect( 0, 0, w, h  )
			end

			zpizmak_items[i].Image = vgui.Create( "DImage", zpizmak_items[i] )
			zpizmak_items[i].Image:SetPos( 34.5*wMod, 10.5*hMod )
			zpizmak_items[i].Image:SetSize( 100*wMod, 100*hMod )
			zpizmak_items[i].Image.Paint = function( self, w, h )
				surface.SetDrawColor(255,255,255)
				surface.SetMaterial( zpizmak.config.Pizzas[i].PizzaTop )
				surface.DrawTexturedRect( 0, 0, w, h  )
			end

			/*
			zpizmak_items[i].model = vgui.Create( "DModelPanel", zpizmak_items[i] )
			zpizmak_items[i].model:SetSize( 150*wMod, 150*hMod )
			zpizmak_items[i].model:SetPos( 15*wMod, -20*hMod )
			zpizmak_items[i].model:SetModel(zpizmak.config.Pizzas[i].Model)
			zpizmak_items[i].model.LayoutEntity = function( self )
				local size1, size2 = self.Entity:GetRenderBounds()
				local size = (-size1+size2):Length()
				self:SetFOV(25)
				self:SetCamPos(Vector(size*2,size*1,size*1))
				self:SetLookAt((size1+size2)/2)
			end
			*/
			zpizmak_items[i].button = vgui.Create( "DButton", zpizmak_items[i])
			zpizmak_items[i].button:SetPos( 0*wMod, 0*hMod )
			zpizmak_items[i].button:SetSize( zpizmak_items.list:GetWide(), 100 )
			zpizmak_items[i].button:SetText("")
			zpizmak_items[i].button.Paint = function( self, w, h )

			end
			zpizmak_items[i].button.DoClick = function()
				SELECTED_PIZZA = i
				if SELECTED_PIZZA ~= nil then
					net.Start("zpizmak_select_pizza")
					net.WriteInt(SELECTED_PIZZA,16)
					net.WriteEntity(LocalPlayer().zpizmak_ConnectedWorkTable)
					net.SendToServer()
					zpizmak_RecipeMenu_panel:SetVisible(false)
				end
			end

			zpizmak_items[i].PizzaName = vgui.Create( "DLabel", zpizmak_items[i] )
			zpizmak_items[i].PizzaName:SetPos( 150*wMod, -40*hMod )
			zpizmak_items[i].PizzaName:SetSize( 260*wMod, 125*hMod )
			zpizmak_items[i].PizzaName:SetFont( "zpizmak_vgui_font03" )
			zpizmak_items[i].PizzaName:SetText(zpizmak.config.Pizzas[i].Name )
			zpizmak_items[i].PizzaName:SetColor(Color(255,255,255,255))

			zpizmak_items[i].PizzaDesc = vgui.Create( "DLabel", zpizmak_items[i] )
			zpizmak_items[i].PizzaDesc:SetPos( 150*wMod, -15*hMod )
			zpizmak_items[i].PizzaDesc:SetSize( 400*wMod, 125*hMod )
			zpizmak_items[i].PizzaDesc:SetFont( "zpizmak_vgui_font04" )
			zpizmak_items[i].PizzaDesc:SetText(zpizmak.config.Pizzas[i].Desc )
			zpizmak_items[i].PizzaDesc:SetColor(Color(255,255,255,255))

			//local recipe = {}
			local xPos = 0
			for k, v in pairs(zpizmak.config.Pizzas[i].Recipe) do
				if(v > 0)then
					//table.insert(recipe,tostring(k..": "..v))

					local icon = zpizmak.f.GetIngredientByName(k).Icon
					if(icon == nil)then
						icon = Material("materials/zerochain/zpizmak/ui/zpizmak_square", "smooth")
					end

					zpizmak_items[i].Ingredient_BG = vgui.Create( "DImage", zpizmak_items[i] )
					zpizmak_items[i].Ingredient_BG:SetPos( (152+xPos)*wMod, 64*hMod )
					zpizmak_items[i].Ingredient_BG:SetSize( 40*wMod, 40*hMod )
					zpizmak_items[i].Ingredient_BG.Paint = function( self, w, h )
						surface.SetDrawColor(75,75,75)
						surface.SetMaterial( circleeIcon )
						surface.DrawTexturedRect( 0, 0, w, h  )
					end

					zpizmak_items[i].Ingredient = vgui.Create( "DImage", zpizmak_items[i] )
					zpizmak_items[i].Ingredient:SetPos( (160+xPos)*wMod, 71*hMod )
					zpizmak_items[i].Ingredient:SetSize( 25*wMod, 25*hMod )
					zpizmak_items[i].Ingredient.Paint = function( self, w, h )
						surface.SetDrawColor(255,255,255)
						surface.SetMaterial( icon )
						surface.DrawTexturedRect( 0, 0, w, h  )
					end

					zpizmak_items[i].IngredientValue = vgui.Create( "DLabel", zpizmak_items[i] )
					zpizmak_items[i].IngredientValue:SetPos( (160+xPos)*wMod, 79*hMod )
					zpizmak_items[i].IngredientValue:SetSize( 50*wMod, 25*hMod )
					zpizmak_items[i].IngredientValue:SetFont( "zpizmak_vgui_font04" )
					zpizmak_items[i].IngredientValue:SetText(tostring(v))
					zpizmak_items[i].IngredientValue:SetColor(Color(255,255,255,255))

					xPos = xPos + 45
				end
			end

			zpizmak_items[i].IngredientBG = vgui.Create( "DImage", zpizmak_items[i] )
			zpizmak_items[i].IngredientBG:SetPos( 150*wMod, 62*hMod )
			zpizmak_items[i].IngredientBG:SetSize( 500*wMod, 46*hMod )
			zpizmak_items[i].IngredientBG.Paint = function( self, w, h )
				surface.SetDrawColor(0,0,0,50)
				surface.SetMaterial( squareIcon )
				surface.DrawTexturedRect( 0, 0, w, h  )
			end


			zpizmak_items[i].PizzaInfo = vgui.Create( "DLabel", zpizmak_items[i] )
			zpizmak_items[i].PizzaInfo:SetPos( 300*wMod, -15*hMod )
			zpizmak_items[i].PizzaInfo:SetSize( 350*wMod, 125*hMod )
			zpizmak_items[i].PizzaInfo:SetFont( "zpizmak_vgui_font04" )
			zpizmak_items[i].PizzaInfo:SetText(zpizmak.language.VGUI_RecipeBakeTime.." "..zpizmak.config.Pizzas[i].BakeTime.."s   /   "..zpizmak.language.VGUI_RecipeProfit.." "..zpizmak.config.Pizzas[i].Price..zpizmak.config.Currency )
			zpizmak_items[i].PizzaInfo:SetColor(Color(255,255,255,255))
			zpizmak_items[i].PizzaInfo:SetContentAlignment( 6 )
		end
	end
end

vgui.Register( "zpizmak_vgui_RecipeMenu", zpizmak_RecipeMenu, "Panel" )

net.Receive( "zpizmak_pizzadough_net", function( len )
	LocalPlayer().zpizmak_ConnectedWorkTable = net.ReadEntity()

	if IsValid(zpizmak_RecipeMenu_panel) then
		zpizmak_RecipeMenu_panel:SetVisible(true)
	else
		zpizmak_RecipeMenu_panel = vgui.Create( "zpizmak_vgui_RecipeMenu" )
	end
end)
/////////////////////////////////////////////


// The Fridge
local zpizmak_fridge = {}
local zpizmak_Fridge_Main = {}

function zpizmak_fridge:Init()
	self:SetSize( 800*wMod, 1000*hMod )
	self:Center()
	self:MakePopup()

	zpizmak_Fridge_Main.Title = vgui.Create( "DLabel", self )
	zpizmak_Fridge_Main.Title:SetPos( 45*wMod, 25*hMod )
	zpizmak_Fridge_Main.Title:SetSize( 700*wMod, 125*hMod )
	zpizmak_Fridge_Main.Title:SetFont( "zpizmak_vgui_font01" )
	zpizmak_Fridge_Main.Title:SetText( zpizmak.language.VGUI_ShopTitle )
	zpizmak_Fridge_Main.Title:SetColor(Color(255,255,255,255))

	zpizmak_Fridge_Main.close = vgui.Create( "DButton", self )
	zpizmak_Fridge_Main.close:SetText("")
	zpizmak_Fridge_Main.close:SetPos( 685*wMod, 55*hMod  )
	zpizmak_Fridge_Main.close:SetSize( 70*wMod, 70*hMod )
	zpizmak_Fridge_Main.close.DoClick = function()
		self:SetVisible(false)
		//self:Remove()
	end
	zpizmak_Fridge_Main.close.Paint = function( self, w, h )
		if zpizmak_Fridge_Main.close:IsHovered() then
			surface.SetDrawColor( 255, 160, 160, 255 )
		else
			surface.SetDrawColor( 255, 125, 125, 255 )
		end
		surface.SetMaterial( Button02Icon )
		surface.DrawTexturedRect( 0, 0, w, h )

		surface.SetFont( "zpizmak_vgui_font02" )
		surface.SetTextColor( 255, 255, 255, 255 )
		surface.SetTextPos( 20*wMod, 10*hMod )
		surface.DrawText( "X" )
	end

	ShopItems( self, 1 )
end
function zpizmak_fridge:Paint( w, h )
	//surface.SetDrawColor( 155, 148, 104, 255 )
	surface.SetDrawColor( 108, 137, 174, 255 )
	surface.SetMaterial(BgPanel)
	surface.DrawTexturedRect( 0, 0, w, h )
end
function ShopItems( parent, content )
	zpizmak_shopitems = {}
	zpizmak_shopitems.panel = vgui.Create( "DScrollPanel", parent )
	zpizmak_shopitems.panel:SetSize( 300*wMod, 200*hMod )
	zpizmak_shopitems.panel:DockMargin(50*wMod,150*hMod,50*wMod,50*hMod)
	zpizmak_shopitems.panel:Dock(FILL)

	zpizmak_shopitems.panel:GetVBar().Paint = function() return true end
	zpizmak_shopitems.panel:GetVBar().btnUp.Paint = function() return true end
	zpizmak_shopitems.panel:GetVBar().btnDown.Paint = function() return true end
	//zpizmak_shopitems.panel:GetVBar().btnGrip.Paint = function() return true end

	zpizmak_shopitems.panel.Paint = function( self, w, h )
		surface.SetDrawColor( 0, 0, 0, 100 )
		surface.SetMaterial( squareIcon )
		surface.DrawTexturedRect( 0, 0, w, h )
	end

	zpizmak_shopitems.list = vgui.Create( "DIconLayout", zpizmak_shopitems.panel)
	zpizmak_shopitems.list:SetSize( 690*wMod, 200*hMod )
	zpizmak_shopitems.list:SetPos( 0*wMod, 17*hMod )
	zpizmak_shopitems.list:SetSpaceY( 10 )

	if zpizmak.config.Ingredients ~= nil then

		for i = 1,table.Count(zpizmak.config.Ingredients) do

			zpizmak_shopitems[i] = zpizmak_shopitems.list:Add("DPanel")
			zpizmak_shopitems[i]:SetSize( zpizmak_shopitems.list:GetWide(), 100 )
			zpizmak_shopitems[i].Paint = function( self, w, h )
				local panelcolor = Color(0,0,0,200)
				if zpizmak_shopitems[i].button:IsHovered() then
					surface.SetDrawColor( panelcolor.r, panelcolor.g, panelcolor.b ,panelcolor.a)
				else
					surface.SetDrawColor( panelcolor.r, panelcolor.g, panelcolor.b ,panelcolor.a*0.5)
				end
				surface.SetMaterial( squareIcon )
				surface.DrawTexturedRect( 15*wMod, 0, w-25, h  )
			end

			/*
			zpizmak_shopitems[i].model = vgui.Create( "DModelPanel", zpizmak_shopitems[i] )
			zpizmak_shopitems[i].model:SetSize( 150*wMod, 150*hMod )
			zpizmak_shopitems[i].model:SetPos( 15*wMod, -20*hMod )
			zpizmak_shopitems[i].model:SetModel(zpizmak.config.Ingredients[i].Model)
			zpizmak_shopitems[i].model.LayoutEntity = function( self )
				local size1, size2 = self.Entity:GetRenderBounds()
				local size = (-size1+size2):Length()
				self:SetFOV(25)
				self:SetCamPos(Vector(size*2,size*1,size*1))
				self:SetLookAt((size1+size2)/2)
			end
			*/


			zpizmak_shopitems[i].Image = vgui.Create( "DImage", zpizmak_shopitems[i] )
			zpizmak_shopitems[i].Image:SetPos( 34.5*wMod, 10.5*hMod )
			zpizmak_shopitems[i].Image:SetSize( 100*wMod, 100*hMod )
			zpizmak_shopitems[i].Image.Paint = function( self, w, h )
				surface.SetDrawColor(255,255,255)
				surface.SetMaterial( zpizmak.config.Ingredients[i].Icon )
				surface.DrawTexturedRect( 0, 0, w, h  )
			end

			zpizmak_shopitems[i].button = vgui.Create( "DButton", zpizmak_shopitems[i])
			zpizmak_shopitems[i].button:SetPos( 0*wMod, 0*hMod )
			zpizmak_shopitems[i].button:SetSize( zpizmak_shopitems.list:GetWide(), 100 )
			zpizmak_shopitems[i].button:SetText("")
			zpizmak_shopitems[i].button.Paint = function( self, w, h )

			end
			zpizmak_shopitems[i].button.DoClick = function()
				SELECTED_INGREDIENT = i

				if SELECTED_INGREDIENT ~= nil then

					net.Start("zpizmak_fridge_buy")
					net.WriteInt(SELECTED_INGREDIENT,16)
					net.WriteEntity(LocalPlayer().zpizmak_ConnectedFridge)
					net.SendToServer()
				end

			end

			zpizmak_shopitems[i].IngName = vgui.Create( "DLabel", zpizmak_shopitems[i] )
			zpizmak_shopitems[i].IngName:SetPos( 160*wMod, -20*hMod )
			zpizmak_shopitems[i].IngName:SetSize( 300*wMod, 125*hMod )
			zpizmak_shopitems[i].IngName:SetFont( "zpizmak_vgui_item_title" )
			zpizmak_shopitems[i].IngName:SetText(zpizmak.config.Ingredients[i].Name )
			zpizmak_shopitems[i].IngName:SetColor(Color(240,240,240,255))

			zpizmak_shopitems[i].IngDesc = vgui.Create( "DLabel", zpizmak_shopitems[i] )
			zpizmak_shopitems[i].IngDesc:SetPos( 160*wMod, 15*hMod )
			zpizmak_shopitems[i].IngDesc:SetSize( 400*wMod, 125*hMod )
			zpizmak_shopitems[i].IngDesc:SetFont( "zpizmak_vgui_font03" )
			zpizmak_shopitems[i].IngDesc:SetText(zpizmak.config.Ingredients[i].Desc )
			zpizmak_shopitems[i].IngDesc:SetColor(Color(200,200,200,255))

			zpizmak_shopitems[i].IngPrice = vgui.Create( "DLabel", zpizmak_shopitems[i] )
			zpizmak_shopitems[i].IngPrice:SetPos( 260*wMod, 0*hMod )
			zpizmak_shopitems[i].IngPrice:SetSize( 400*wMod, 125*hMod )
			zpizmak_shopitems[i].IngPrice:SetFont( "zpizmak_vgui_font05" )
			zpizmak_shopitems[i].IngPrice:SetText(zpizmak.config.Ingredients[i].Price..zpizmak.config.Currency)
			zpizmak_shopitems[i].IngPrice:SetColor(Color(34,197,34,255))
			zpizmak_shopitems[i].IngPrice:SetContentAlignment( 6 )
		end
	end
end
vgui.Register( "zpizmak_vgui_fridge", zpizmak_fridge, "Panel" )

net.Receive( "zpizmak_fridge_net", function( len )
	LocalPlayer().zpizmak_ConnectedFridge = net.ReadEntity()

	if IsValid(zpizmak_fridge_panel) then
		zpizmak_fridge_panel:SetVisible(true)
	else
		zpizmak_fridge_panel = vgui.Create( "zpizmak_vgui_fridge" )
	end
end)
/////////////////////////////////////////////
