-- 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 function print(...) _G.print(...) end

local wstr=require("wetgenes.string")
local pack=require("wetgenes.pack")

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


M.bake=function(oven,pages)

	pages.modname=M.modname

	local gl=oven.gl
	local cake=oven.cake
	local sounds=cake.sounds
	local sheets=cake.sheets

	local wdata=oven.rebake("wetgenes.gamecake.widgets.data")
	local gui=oven.rebake("bulb.gui")

	pages.data={}
	pages.data.menu=require("bulb.pages.menu")

	function pages.loads()
		for n,p in pairs(pages.data) do
			for i,w in ipairs(p) do
				local sheetname="pages/"..n.."/"..w.name
				w.id=n.."_"..w.name
				w.sheetname=sheetname
				sheets.loads_and_chops{
					{sheetname,1,1,0,0},
				}
			end
		end
	end

	function pages.setup()
	end

	function pages.clean()

	end
	
	function pages.update()
		if pages.w then
			local w=pages.w["lights"]
			if w then
				w.color=math.random(0,1)*0xff000000 + 0xffffff
			end
		end
	end
	
	function pages.widget_anim(w,size)
		if not w.hooks then return end
		w.anim={
			widget=w,
			num=size or 1,
			vel=0,
			update=function(anim)
				anim.vel=anim.vel-(anim.num*1/16)
				if w.master.over ~= w then
					anim.vel=anim.vel*14/16
				end
				anim.num=anim.num+anim.vel
				if (math.abs(anim.vel) + math.abs(anim.num)) <(1/1024) then
					anim.widget.anim=nil
				end
				anim.widget:set_dirty()

				local s=1+anim.num
				w.sx=s
				w.sy=s
			end,
			draw=function(anim)
--[[
				local s=1+anim.num
				gl.Translate(w.hx/2,w.hy/2,0)
				gl.Scale(s,s,1)
				gl.Translate(-s*w.hx/2,-s*w.hy/2,0)
]]
			end
		}
		return w
	end

	function pages.add(top,name)
		local tab
		
		local d
-- fix the positioning as we have a margin
		local cx=-(960-320)/2
		local cy=-(960-480)/2
		
			d=pages.data.menu
			local button={
				highlight="dark",
				hooks=gui.hooks
			}
			local hide={
				hide=true
			}
			tab={
				["settings"]=button,
				["play"]=button,
				["gamemode"]=hide,
				["butt_down"]=hide,
				["butt_up"]=hide,
				["hello"]=button,
				["quit"]=hide,
			}
			if oven.opts.smell=="gamestick" then
				tab={
					["settings"]=button,
					["play"]=button,
					["gamemode"]=hide,
					["butt_down"]=hide,
					["butt_up"]=hide,
					["hello"]=hide,
					["quit"]=button,
				}
			end
			
			pages.w={}
			for i,v in ipairs(d) do
			
				local x=tab[v.name]
				if not(x and x.hide) then
					local w=top:add{px=v.x/2+cx,py=v.y/2+cy,hx=v.w/2,hy=v.h/2,
						color=0xffffffff,
						sheet=v.sheetname,
						id=v.id,
						highlight=x and x.highlight or "none",
						hooks=x and x.hooks,
						}
					pages.w[v.name]=w
				end
					
--				pages.widget_anim(w)

			end

	end

	function pages.draw()
	

	end
	
	return pages
end
