-- 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 apps=apps
local win=win


local function print(...) _G.print(...) end

local wstr=require("wetgenes.string")

module("bulb.coins")

function bake(state,coins)
	local coins=coins or {}

	local cake=state.cake
	local game=cake.game
	local field	=state.rebake("bulb.field")
	local cells	=state.rebake("bulb.cells")
	local ball	=state.rebake("bulb.ball")
	local play	=state.rebake("bulb.game_play")
	local gui	=state.rebake("bulb.gui")
	local can	=state.rebake("bulb.can")
	
	local gl=state.gl
	local canvas=cake.canvas

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


	coins.price=function(b)
		return math.ceil(b.base * math.pow( b.power , b.sold ))
	end
	coins.buy=function(b)
		coins.spend(b:price())
		b.sold=b.sold+1
	end

	coins.buys={
		{
			name="ice",
			base=16,
			power=4,
			popup="Ice Ice Baby!",
		},
		{
			name="boom",
			base=16,
			power=4,
			popup="Big bada boom!",
		},
		{
			name="rock",
			base=1,
			power=2,
			popup="Form of Crushinator!",
		},
		{
			name="clear",
			base=128,
			power=4,
			popup="Bye Bye sweetness!",
		},
		{
			name="fire",
			base=32,
			power=2,
			popup="Twisted fire starter!",
		},
		{
			name="sponge",
			base=2,
			power=2,
			popup="A little drinky!",
		},
		{
			name="dopple",
			base=1,
			power=2,
			popup="When one becomes two!",
		},		
		{
			name="upturn",
			base=5,
			power=2,
			popup="Flippy Flopsy!",
		},		
		{
			name="refill",
			base=5,
			power=2,
			popup="More of the same!",
		},		

		{
			name="stick",
			base=5,
			power=2,
			popup="Soulless velcro machine!",
		},		
		{
			name="skip",
			base=23,
			power=3,
			popup="The what? what? now?",
		},		
		{
			name="reset",
			base=5,
			power=2,
			popup="Try again!",
		},		
		{
			name="suicide",
			base=0,
			power=2,
			popup="We are born to suffer!",
		},		

		{
			name="flea",
			base=5,
			power=2,
			popup="Bugaboo!",
		},		
		{
			name="mole",
			base=5,
			power=2,
			popup="Monty!",
		},		

	}
	for i,b in ipairs(coins.buys) do
		b.sold=0
		b.id=can.id_from_name(b.name)
		b.price=coins.price
		b.buy=coins.buy
	end


	function coins.loads()
	end
	
	coins.num=0
	coins.gain=0
	coins.spent=0

	function coins.setup()
	
		coins.num=sprofiles.get("coins") or 0
		
		coins.gain=0

		coins.spent=coins.pspent or 0	-- buying a level causes a prespend
		coins.level_spent=0				-- reset on level up
		
		for i,v in ipairs(coins.buys) do
			v.sold=0
		end

		coins.tab={}


	end
	
	function coins.clean()
		coins.save()
	end

	function coins.save()
		sprofiles.set("coins",coins.num)
	end

	function coins.update()
		if coins.add_fade then
			coins.add_fade=coins.add_fade - 1/256
			if coins.add_fade < 0 then
				coins.add_fade=nil
			end
		end
	end

	function coins.draw()


		canvas.font.set(cake.fonts.get("ostrich-black"))
		canvas.font.set_size(16,0)
		local s=wstr.str_insert_number_commas(coins.available()).."c"
		local sw=canvas.font.width(s)

		gl.Color(0,0,0,1)	
		canvas.font.set_xy(320-4-(sw)+2,480-20+2)
		canvas.font.draw(s)
		
		gl.Color(1,1,1,1)	
		canvas.font.set_xy(320-4-(sw),480-20)
		canvas.font.draw(s)

		if coins.add_fade then
			local s="+"..wstr.str_insert_number_commas(coins.add_num).."c"
			local sw=canvas.font.width(s)
			local a=coins.add_fade
			a=a*a
			gl.Color(a,a,a,a)
			canvas.font.set_xy(320-4-(sw),480-40)
			canvas.font.draw(s)
		end
		
	end
	
	function coins.available()
		return coins.num-coins.spent
	end

	function coins.affordable( num )
		if coins.available() >= num then return true end
		return false
	end
	
	function coins.spend( num )
		coins.spent=coins.spent+num
		coins.level_spent=coins.level_spent+num
	end

	function coins.pspend( num )
		coins.pspent=num
	end

	function coins.add( num )
		if coins.add_fade then
			coins.add_num=coins.add_num+num
			coins.add_fade=1
		else
			coins.add_num=num
			coins.add_fade=1
		end
		coins.num=coins.num+num -- won some coins
		coins.gain=coins.gain+num
	end
	
	function coins.addscore( old , num )
	
		local n=math.floor((old+num)/1000) - math.floor(old/1000)
		if n>0 then
			coins.add( n)
		end
	end


	function coins.level_price(i)
		local r=0
		for p=0,i-1 do r=r+p end
		return r*i
	end
	
	return coins
end
