-- copy all globals into locals, some locals are prefixed with a G to reduce name clashes
local coroutine,package,string,table,math,io,os,debug,assert,dofile,error,_G,getfenv,getmetatable,ipairs,Gload,loadfile,loadstring,next,pairs,pcall,print,rawequal,rawget,rawset,select,setfenv,setmetatable,tonumber,tostring,type,unpack,_VERSION,xpcall,module,require=coroutine,package,string,table,math,io,os,debug,assert,dofile,error,_G,getfenv,getmetatable,ipairs,load,loadfile,loadstring,next,pairs,pcall,print,rawequal,rawget,rawset,select,setfenv,setmetatable,tonumber,tostring,type,unpack,_VERSION,xpcall,module,require

local pack=require("wetgenes.pack")
local wwin=require("wetgenes.win")
local wstr=require("wetgenes.string")
local tardis=require("wetgenes.tardis")	-- matrix/vector math

local function dprint(a) print(wstr.dump(a)) end

--module
local M={ modname=(...) } ; package.loaded[M.modname]=M

M.bake=function(state,menu)
	local menu=menu or {}
	menu.state=state
	
	menu.modname=M.modname

	local cake=state.cake
	local sheets=cake.sheets
	local opts=state.opts
	local canvas=cake.canvas
	local font=canvas.font
	local flat=canvas.flat
	local gl=state.gl
	local sounds=cake.sounds
	local al=sounds.al
	
	local game=state.rebake("bulb.game")
	local gui=state.rebake("bulb.gui")
	local beep=state.rebake("bulb.beep")
	local coins=state.rebake("bulb.coins")

	local sgui=state.rebake("wetgenes.gamecake.spew.gui")

	local mods={} for i,n in ipairs{
--		"bulb.icles",
	} do mods[i]=state.rebake(n) end
	
--	local icles=state.rebake("bulb.icles")
	
menu.loads=function()

	
--[[
		cake.images.loads({
			"bulbs",
		})
		cake.sheets.create("bulbs"):setimg("bulbs"):chop(50/800,50/50,25/800,25/50)
]]

	for i,v in ipairs(mods) do
		if v.loads then v.loads() end
	end

end
		
menu.setup=function()

--menu.next_page="score"

	for i,v in ipairs(mods) do
		if v.setup then v.setup() end
	end
	
--	icles.feathers.start()

	menu.loads()

--	beep.stream("menu") -- next page function may replace this
	gui.page(menu.next_page or "menu")
	menu.next_page=nil

	coins.pspend(0)
	coins.setup()

	
end

menu.clean=function()

	for i,v in ipairs(mods) do
		if v.clean then v.clean() end
	end

end

menu.msg=function(m)

--	print(wstr.dump(m))

	if m.xraw and m.yraw then	-- we need to fix raw x,y numbers
		m.x,m.y=game.layout.xyscale(m.xraw,m.yraw)	-- local coords, 0,0 is the center of screen
		m.x=m.x+(opts.width/2) -- origin is now top left of canvas view
		m.y=m.y+(opts.height/2)
	end
	
	if sgui.active then
		sgui.msg(m)
		return
	end

	if gui.msg(m) then return end -- gui can eat msgs
	
	if m.class=="key" then
		if m.action==-1 then
			if m.ascii=="e" then
				game.next=state.rebake("bulb.game_edit")
			end
		end
	end

end

menu.update=function()

	
	for i,v in ipairs(mods) do
		if v.update then v.update() end
	end

	if sgui.active then
		sgui.update()
		return
	end

	gui.update()

	coins.update()
end

menu.draw=function()

	if sgui.active then
		if sgui.page_name=="score_list" then
			sheets.get("scoreback"):draw(1,160,240,0,1024,1024)
		else
			sheets.get("patterns/pattern5"):draw(1,160,240,0,1024,1024)
		end
		sgui.draw()
		game.draw_crap(true,true,true)
		return
	end
		
	if gui.page_name~="menu" then
		sheets.get("patterns/pattern5"):draw(1,160,240,0,1024,1024)
	end
	
--print(gui.page_name)
	if gui.page_name=="shop_levels" then
		sheets.get("poplevels"):draw(1,160,240,0,512,512)
	end


	for i,v in ipairs(mods) do
		if v.draw then v.draw() end
	end
	

	gui.draw()


	font.set(cake.fonts.get(1))
	font.set_size(32,0)
	
	coins.draw()

	game.draw_crap(true,true,true)

end

	return menu
end
