-- 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 wfred=require("wetgenes.fred")
local wstr=require("wetgenes.string")
local pack=require("wetgenes.pack")

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


M.bake=function(oven,cuts)

	cuts.modname=M.modname

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

	local game=cake.game	
	
	local beep=oven.rebake("bulb.beep")
	local field	=oven.rebake("bulb.field")
	local cells	=oven.rebake("bulb.cells")
	local score	=oven.rebake("bulb.score")
	local emits	=oven.rebake("bulb.emits")
	local can	=oven.rebake("bulb.can")

	local levels=oven.rebake("bulb.levels")

	local play=oven.rebake("bulb.game_play")


--	local fred=wfred.create{}
	
	cuts.setup=function()
		cuts.co=nil

--		fred:clean()
--		cuts.start_boss()
	end
	
	cuts.clean=function()
--		fred:clean()
	end
	
	cuts.update=function()
		if cuts.co then
			if coroutine.status(cuts.co)~="dead" then
				local a1,a2=coroutine.resume(cuts.co)
				if a1 then cuts.codraw=a2 else error(a2) end
			end
		end
--		fred:update()
	end
	
	cuts.draw=function()
		if cuts.co then
			if cuts.codraw then cuts.codraw() end
		end
--		fred:draw()
	end


	local acts={}
	
	acts.create=function(it)
		it=it or {}
		it.perform=acts.perform
		return it
	end
	
	acts.perform=function(it,name,act,...)
		if type(name)=="table" then
			for i,v in ipairs(name) do
				local fs=acts[v]
				if fs then
					local f=fs[act]
					if f then
						f(it,...)
					end
				end
			end
		else
			local fs=acts[name]
			if fs then
				local f=fs[act]
				if f then
					return f(it,...)
				end
			end
		end
	end
	
	acts.fizix={
		setup=function(it)
			it.px=it.px or 0
			it.py=it.py or 0
			it.vx=it.vx or 0
			it.vy=it.vy or 0
		end,
		update=function(it)
			it.px=it.px+it.vx
			it.py=it.py+it.vy
		end,
	}

	acts.boss={
		setup=function(it)
			it.px=game.unit*4+10
			it.py=0
			it.vx=0
			it.vy=1
			it.ss=8
			it.sb=game.unit
		end,
		update=function(it)
			local by=((cells.ch)*game.unit*12/16) - (cells.float) + (game.unit*(1/4)) - ( it.sb*it.ss*0.25 )
			
			it.vy=it.vy+(1)
			
			it.vy=it.vy*(252/256)
			
			if it.py > by then
				it.py = by
				it.vy=-it.vy
				if it.ss<=1 then
					it.done=true
				else
					beep.play("bossmove")
				end
			end

			if it.ss>1 then
				if it.vy<0 then
					it.ss=it.ss-(2/16)
				end
			end
		end,
		draw=function(it)
			local bulbs=cake.sheets.get("bulbs")
			bulbs:draw( 45 , it.px, it.py , 0 , game.unit*it.ss )
		end,
	}

	cuts.start_boss=function()
	
		local draw=coroutine.yield
	
		cuts.co=coroutine.create(function()
		
			beep.play("warning")

			local t=1
			local si=1
			local ss={
				"!*! Warning !*!",
				"!*! Warning !*!",
				"!*! Warning !*!",
				"!*! Giant !*!",
				"!*! Battle !*!",
				"!*! Frog !*!",
				" and ? or ",
				"!*! Mole !*!",
				"!*! Aproaching !*!",
					}
			repeat
				local maxf=80
				
				draw(function()
				
					local t=t
					t=t*2
					if t>1 then t=1 end

					canvas.font.set(cake.fonts.get("ostrich-black"))
					canvas.font.set_size(maxf*t,0)

					local s=ss[si]
					local l=canvas.font.width(s)

					canvas.font.set_xy(160-(l/2) - (t*2) ,240-(maxf*0.5*t) - (t*2)  )
					gl.Color(1,1,1,1)
					canvas.font.draw(s)

					canvas.font.set_xy(160-(l/2) + (t*2) ,240-(maxf*0.5*t) + (t*2)  )
					gl.Color(0,0,0,1)
					canvas.font.draw(s)

					canvas.font.set_xy(160-(l/2),240-(maxf*0.5*t))
					gl.Color(1,0,0,1)
					canvas.font.draw(s)

				end)

				t=t-(1/60)
				if t<=0 then
					si=si+1
					t=1
					beep.play("warning")
				end
				
			until si>#ss
		
		
			local it=acts.create()
			
			it:perform({"fizix","boss"},"setup")
			
			repeat
--print("upd")
				it:perform({"boss","fizix"},"update")

				local cx,cy=cells.pxy_to_cxy(it.px,it.py)
				for cx=0,5 do
					local c=cells.get_cell(cx,cy)
					if c and c.id>0 then c:boom() end
				end
			
				draw(function()
					it:perform({"boss"},"draw")
				end)

			until it.done

			local c=cells.get_cell(3,0)
			c.id=can.id_from_name("boss")
			cells.state_next="clear"


			repeat
				draw(function()
				end)
			until false

--			cuts.co=nil
		end)

	end


	return cuts
end
