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


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

	sheets.loads_and_chops{
		{"pages/menu/background",1,1,0,0},
		{"story/end_1",1,1,0.5,0.5},
	}

end
		
win.setup=function()

	win.loads()
	
	gui.page()

	beep.stream("bigwin")

	local n=coins.num
	local s=coins.spent
	if n<1 then n=1 end
	
	win.bonus=(n-s)/n
	if win.bonus==1 then win.bonus=2 end -- no spend super bonus
	
	win.pct=math.floor(win.bonus*100)
	
	score.add( math.floor(score.num * win.pct / 100 ) ) -- hand out bonus multi

end

win.clean=function()

end

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

		if m.action==1 then
		
			score.final()

		end
		
	end

	if gui.msg(m) then return end -- gui can eat msgs

end

win.update=function()

	gui.update()

end

win.draw=function()

	local a=1
	gl.Color(a,a,a,a)
	sheets.get("story/end_1"):draw(1,320/2,480/2,0,480*2*a,480*a)
	
			

	canvas.font.set(cake.fonts.get("Chunk"))
	canvas.font.set_size(32,0)
	local s=win.pct.."% Bonus"
	local sw=canvas.font.width(s)

	gl.Color(0,0,0,1)	
	canvas.font.set_xy(160-(sw/2)+2,2+480-64)
	canvas.font.draw(s)
	
	gl.Color(1,1,1,1)	
	canvas.font.set_xy(160-(sw/2),480-64)
	canvas.font.draw(s)


	canvas.font.set_size(16,0)
	local s="for the win"
	local sw=canvas.font.width(s)

	gl.Color(0,0,0,1)	
	canvas.font.set_xy(160-(sw/2)+2,2+480-32)
	canvas.font.draw(s)
	
	gl.Color(1,1,1,1)	
	canvas.font.set_xy(160-(sw/2),480-32)
	canvas.font.draw(s)


	gl.Color(1,1,1,1)

	gui.draw()

end


	return win
end
