-- 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,intro)
	local intro=intro or {}
	intro.state=state
	
	intro.modname=M.modname


	intro.onlyone=false

	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 game=state.rebake("bulb.game")
	local gui=state.rebake("bulb.gui")
	local beep=state.rebake("bulb.beep")

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

	sheets.loads_and_chops{
		{"pages/menu/background",1,1,0,0},
		{"story/1",1,1,0.5,0.5},
		{"story/2",1,1,0.5,0.5},
		{"story/3",1,1,0.5,0.5},
		{"story/4",1,1,0.5,0.5},
		{"story/5",1,1,0.5,0.5},
	}

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

end
		
intro.setup=function()

	if intro.onlyone  then -- only play once, except for retarted gamestick version
		game.next=state.rebake("bulb.game_menu")
		return
	end


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

	intro.loads()
	
	intro.slides={}

	if oven.opts.smell=="gamestick" then
		intro.add_slide("preloader")
		intro.add_slide("story/1")
		intro.next=2
	else
		intro.add_slide("story/1")
		intro.add_slide("story/2")
		intro.next=3
	end
	
	gui.page()

	beep.stream("intro")

end

intro.clean=function()
	intro.onlyone=true
	
	for i,v in ipairs(mods) do
		if v.clean then v.clean() end
	end

end

intro.msg=function(m)
	if not intro.slides then return end --ignore input until we are setup

--	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 m.class=="mouse" or m.class=="key" or m.class=="joykey" then
		

		local v=intro.slides[#intro.slides]
		local b=intro.clicked

		if v then
			if m.action==1 and not intro.debounce then

				if v.va==0 then
					beep.play("button2")
				end

--				intro.debounce=10
--				intro.clicked={x=m.x,y=m.y,px=v.px,py=v.py,vx=0,vy=0}
				v.vx=0
				v.vy=0
--				if v.va~=0 then v.pa=0 else v.va=-1/32 end
				v.va=-1/16
--[[
				v.va=0
			elseif m.action==-1 then
				intro.clicked=nil
				if b then
					v.vx=b.vx or 0
					v.vy=b.vy or 0
					v.va=-1/64
				end
]]
			else
				if b then
					b.vx=(b.vx+m.x-(b.lx or b.x))/2
					b.vy=(b.vy+m.y-(b.ly or b.y))/2
					local vx=m.x-b.x
					local vy=m.y-b.y
					if v then
						v.px=b.px + vx
						v.py=b.py + vy
					end
					b.lx=m.x
					b.ly=m.y
				end
			end
		end
		
	end
	
	
	if gui.msg(m) then return end -- gui can eat msgs
	
end

intro.update=function()

	if intro.debounce then
		intro.debounce=intro.debounce-1
		if intro.debounce<0 then intro.debounce=nil end
	end

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

	gui.update()

end

intro.draw=function()
		
--	sheets.get("pages/menu/background"):draw(1,-(960-320)/2,-(960-480)/2)


	for i,v in ipairs(intro.slides) do
		v.draw()
	end


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

	gui.draw()
	
	game.draw_crap(false,false,false,true)


--	font.set(cake.fonts.get(1))
--	font.set_size(32,0)
	
end

intro.add_slide=function(name)

	local t={}
	
	t.name=name
	t.px=320/2
	t.py=480/2
	t.pa=1
	
	t.vx=0
	t.vy=0
	t.va=0
	
	table.insert(intro.slides,1,t)
	
	t.update=function()
		t.px=t.px+t.vx
		t.py=t.py+t.vy
		t.pa=t.pa+t.va
		if t.pa<=0 then -- destroy
			intro.slides[#intro.slides]=nil
			intro.clicked=nil
			if intro.next==6 then
				if #intro.slides==0 then
					game.next=state.rebake("bulb.game_menu")
				end
				if #intro.slides==1 then
					beep.stream_fx("win",1)
				end
			else
				intro.add_slide("story/"..intro.next)
				intro.next=intro.next+1
			end
		end
	end

	t.draw=function()
		local a=t.pa
		if a<=0 then return end
		if a>1 then a=1 end
		gl.Color(a,a,a,a)
		a=1/a
		sheets.get(t.name):draw(1,t.px,t.py,-math.pi*5*(1-a),480*2*a,480*a)
		gl.Color(1,1,1,1)
	end

end


	return intro
end
