menus = {}

function ShowCrawl( file, after_event )
	local text = {}
	for line in io.lines( file ) do
		table.insert( text, line )
	end
	crawl_widgets = {}
	local font, color
	local sx, sy
	local r,g,b,a
	local y = CONFIG.scr_height + 1
	local w
	local username = {}
	for i=1,players_num do
		table.insert( username, config.players[i].name )
	end
	username = table.concat( username, " and " )
	local score = {}
	if players then
		for i=1,players_num do
			table.insert( score, players[i].score )
		end
		score = table.concat( score, ", " )
	else
		score = "0"
	end
	for i=1,#text do
		font = string.match( text[i], "^{font=([^}]+)}" )
		font = font or "default"
		color = string.match( text[i], "^{color=([^}]+)}" )
		color = color or "1,1,1,1"
		r,g,b,a = string.match( color, "([^,]+),([^,]+),([^,]+),([^,]+)" )
		r,g,b,a = tonumber(r),tonumber(g),tonumber(b),tonumber(a)
		color = { r, g, b, a }
		text[i] = string.gsub( text[i], "{[^}]+}", "" )
		text[i] = string.gsub( text[i], "%%username%%", username )
		text[i] = string.gsub( text[i], "%%score%%", score )
		if text[i] == "" then
			sx, sy = GetCaptionSize( font, "I" )
		else
			sx, sy = GetCaptionSize( font, text[i] )
		end
		w = CreateWidget( constants.wt_Label, "crawl", nil, (CONFIG.scr_width-sx)/2, y, 1, 1 )
		WidgetSetCaptionColor( w, color, false )
		WidgetSetCaptionFont( w, font )
		WidgetSetCaption( w, text[i] )
		table.insert( crawl_widgets, { (CONFIG.scr_width-sx)/2, y, w } )
		y = y + sy + 5
	end

	Resume( NewThread( function()
		while #crawl_widgets > 0 do
			for key, value in pairs( crawl_widgets ) do
				value[2] = value[2] - 0.2
				if value[3] == 5 then Log( value[3], " ", value[1], " ", value[2] ) end
				WidgetSetPos( value[3], value[1], value[2] )
				if value[2] < -10 then
					DestroyWidget( value[3] )
					table.remove( crawl_widgets, key )
				end
			end
			Wait(1)
		end
		DestroyWidget( w_crawl_back )
		after_event()
	end ))

	w_crawl_back = CreateWidget( constants.wt_Button, "crawl_back", nil, CONFIG.scr_width-50, CONFIG.scr_height-15, 50, 15 )
	WidgetSetCaptionColor( w_crawl_back, {1,1,1,1}, false )
	WidgetSetCaptionColor( w_crawl_back, {1,.3,.3,1}, true )
	WidgetSetCaption( w_crawl_back,"BACK" )
	WidgetSetLMouseClickProc( w_crawl_back, function()
		for key, value in pairs( crawl_widgets ) do
			DestroyWidget( value[3] )
		end
		crawl_widgets = {}
		DestroyWidget( w_crawl_back )
		after_event()
	end )
end

function ShowText( file, y, after_event )
	local text = {}
	for line in io.lines(file) do
		table.insert( text, line )
	end
	text = table.concat( text )
	text = string.gsub( text, "%*([^%*]+)%*", "/cFFFA90%1/cFFFFFF" )
	text_widget = CreateWidget( constants.wt_Label, "text", nil, 20, y, 560, 800 )
	WidgetSetCaptionColor( text_widget, {1,1,1,1}, false )
	WidgetSetCaptionFont( text_widget, "small" )
	WidgetSetCaption( text_widget, text, true )
	w_event_back = CreateWidget( constants.wt_Button, "crawl_back", nil, CONFIG.scr_width-50, CONFIG.scr_height-15, 50, 15 )
	WidgetSetCaptionColor( w_event_back, {1,1,1,1}, false )
	WidgetSetCaptionColor( w_event_back, {1,.3,.3,1}, true )
	WidgetSetCaption( w_event_back,"BACK" )
	WidgetSetLMouseClickProc( w_event_back, function()
		DestroyWidget( text_widget )
		DestroyWidget( w_event_back )
		after_event()
	end )
end

function HideMenu( num )
	if not menus[num] then
		return
	end
	if not menus[num].widgets then
		menus[num].widgets = {}
	end
	for key, value in pairs( menus[num].widgets ) do
		WidgetSetVisible( value[3], false )
	end
	menus[num].active = false
end

function ShowMenu( num )
	if not menus[num] then
		return
	end
	if not menus[num].widgets then
		menus[num].widgets = {}
	end
	for key, value in pairs( menus[num].widgets ) do
		WidgetSetVisible( value[3], true )
	end
	if menus[num].widgets[1] then
		WidgetGainFocus(menus[num].widgets[1][3])
	end
	menus[num].active = true
end

function ReadKey(num, i, table, key)
	HideMenu( num )
	local sx, sy = GetCaptionSize( "default", "PRESS NEW KEY" )
	local w = CreateWidget( constants.wt_Label, "menu_element", nil, (CONFIG.scr_width-sx)/2, (CONFIG.scr_height-sy)/2, sx, sy )
	local tkey = key
	WidgetSetCaptionColor( w, {1,1,1,1}, false )
	WidgetSetCaption( w, "PRESS NEW KEY" )
	function keyWaiter(key)
		table[tkey] = key
		DestroyWidget( w )
		local sx, sy = GetCaptionSize( "default", tkey..": "..GetKeyName(key) )
		menus[num].widgets[i][1] = (CONFIG.scr_width-sx)/2
		WidgetSetCaption( menus[num].widgets[i][3], tkey..": "..GetKeyName(key)  )
		WidgetSetPos( menus[num].widgets[i][3], menus[num].widgets[i][1], menus[num].widgets[i][2] )
		GlobalSetKeyReleaseProc(nil)
		ShowMenu( num )
		return true
	end
	GlobalSetKeyReleaseProc(keyWaiter)
end

b2p = {}

function RewriteConfig()
	local out = io.open( "config/starstuff.lua", "w" )
		out:write( "config =\n{" )
		out:write( "\tkeys = {\""..table.concat(config.keys, "\", \"").."\"},\n" )
		out:write( "\tcontrols =\n\t{\n")
			out:write("\t\t--singleplayer\n\t\t[0] =\n\t\t{\n")
				out:write("\t\t\ttype = \""..config.controls[0].type.."\",\n")
				for i=1, #config.keys do
					out:write("\t\t\t"..config.keys[i].." = keys."..GetKeyName(config.controls[0][config.keys[i]])..",\n")
				end
			out:write("\t\t},\n")
			out:write("\t\t--player 1\n\t\t{\n")
				out:write("\t\t\ttype = \""..config.controls[1].type.."\",\n")
				for i=1, #config.keys do
					out:write("\t\t\t"..config.keys[i].." = keys."..GetKeyName(config.controls[1][config.keys[i]])..",\n")
				end
			out:write("\t\t},\n")
			out:write("\t\t--player 2\n\t\t{\n")
				out:write("\t\t\ttype = \""..config.controls[2].type.."\",\n")
				for i=1, #config.keys do
					out:write("\t\t\t"..config.keys[i].." = keys."..GetKeyName(config.controls[2][config.keys[i]])..",\n")
				end
			out:write("\t\t},\n")
		out:write( "\t},\n" )
		out:write( "\tplayers =\n\t{\n" )
			out:write("\t\t--player 1\n\t\t{\n")
				out:write("\t\t\tname = \""..config.players[1].name.."\",\n")
				out:write("\t\t\tcolor = {"..table.concat(config.players[1].color, ", ").."},\n")
			out:write("\t\t},\n")
			out:write("\t\t--player 2\n\t\t{\n")
				out:write("\t\t\tname = \""..config.players[2].name.."\",\n")
				out:write("\t\t\tcolor = {"..table.concat(config.players[2].color, ", ").."},\n")
			out:write("\t\t},\n")
		out:write( "\t},\n" )
		out:write( "\tlights = "..tostring(config.lights)..",\n" )
		out:write( "\tmode = "..config.mode..",\n" )
		out:write( "\tplayers_num = "..config.players_num..",\n" )
	out:write( "}\n" )
	out:close()
end

function Button( widget )
	PlaySnd( "menu_select.wav", true )
	local num, w = unpack(b2p[widget])
	local param = menus[num].widgets[w]
	param[4]( num, w )
end

function Unfocus()
	PlaySnd( "menu_move.wav", true )
end

function AddButton( num, text, event, options )
	if not menus[num] then
		menus[num] = { h = 0, e = 0, widgets = {} }
	end
	local o = options or {}
	o.font = o.font or "default"
	o.p_color = o.p_color or { 1, 1, 1, 1 }
	o.a_color = o.a_color or {.8, 0, 0, 1}
	local sx, sy = GetCaptionSize( o.font, text )
	local w = CreateWidget( constants.wt_Button, "menu_element", nil, (CONFIG.scr_width-sx)/2, (CONFIG.scr_height/2) + menus[num].h, sx, sy )
	WidgetSetCaptionColor( w, o.p_color, false )
	WidgetSetCaptionColor( w, o.a_color, true )
	WidgetSetCaptionFont( w, o.font )
	WidgetSetCaption( w, text )
	b2p[w] = { num, #menus[num].widgets+1 }
	table.insert( menus[num].widgets, { (CONFIG.scr_width-sx)/2, (CONFIG.scr_height/2) + menus[num].h, w, event } )
	menus[num].h = menus[num].h + sy + 10
	WidgetSetVisible( w, false )
	WidgetSetLMouseClickProc( w, Button )
	WidgetSetUnFocusProc( w, Unfocus )
end

function InputField( widget )
	local num, w = unpack(b2p[widget])
	local param = menus[num].widgets[w]
	param[1]( WidgetGetCaption(widget) )
end

function AddInputField( num, text, event, options )
	if not menus[num] then
		menus[num] = { h = 0, e = 0, widgets = {} }
	end
	local o = options or {}
	o.font = o.font or "small"
	o.p_color = o.p_color or { .8, .8, .8, 1 }
	o.a_color = o.a_color or { 1, 1, 1, 1}
	local sx, sy = GetCaptionSize( o.font, text[1] )
	local w = CreateWidget( constants.wt_Textfield, "menu_element", nil, (CONFIG.scr_width)/2+15, (CONFIG.scr_height/2) + menus[num].h, sx, sy )
	b2p[w] = { num, #menus[num].widgets+1 }
	table.insert( menus[num].widgets, { event, event, w } )
	WidgetSetVisible( w, false )
	WidgetSetKeyInputProc( w, InputField )
	WidgetSetCaptionColor( w, o.p_color, false )
	WidgetSetCaptionColor( w, o.a_color, true )
	WidgetSetCaptionFont( w, o.font )
	WidgetSetCaption( w, text[2] )
	w = CreateWidget( constants.wt_Label, "menu_element", nil, (CONFIG.scr_width)/2-sx, (CONFIG.scr_height/2) + menus[num].h, sx, sy )
	b2p[w] = { num, #menus[num].widgets+1 }
	WidgetSetCaptionColor( w, o.p_color, false )
	WidgetSetCaptionColor( w, o.a_color, true )
	WidgetSetCaptionFont( w, o.font )
	WidgetSetCaption( w, text[1] )
	WidgetSetVisible( w, false )
	table.insert( menus[num].widgets, { nil, nil, w } )
	menus[num].h = menus[num].h + sy + 10
end

function AddColorField( num, text, event, options )
	if not menus[num] then
		menus[num] = { h = 0, e = 0, widgets = {} }
	end
	local o = options or {}
	o.font = o.font or "default"
	o.p_color = o.p_color or { .8, .8, .8, 1 }
	o.a_color = o.a_color or { 1, 1, 1, 1 }
	o.m_color = o.m_color or { 1, 1, 1, 1 }
	local sx1, sy1 = GetCaptionSize( o.font, text[1] )
	local sx2, sy2 = GetCaptionSize( o.font, text[1] )
	local sx3, sy3 = GetCaptionSize( o.font, text[1] )
	local w = CreateWidget( constants.wt_Button, "menu_element", nil, (CONFIG.scr_width-(sx1+sx2+sx3+30))/2, (CONFIG.scr_height/2) + menus[num].h, sx1, sy1 )
	WidgetSetCaptionColor( w, o.p_color, false )
	WidgetSetCaptionColor( w, o.a_color, true )
	WidgetSetCaptionFont( w, o.font )
	WidgetSetCaption( w, text[1] )
	local id = #menus[num].widgets+1
	b2p[w] = { num, id }
	table.insert( menus[num].widgets, { 0, 0, w, event, value=options.value or 1.0 } )
	WidgetSetVisible( w, false )
	WidgetSetLMouseClickProc( w, function() menus[num].widgets[id].value = math.max(0, menus[num].widgets[id].value-0.1) event(menus[num].widgets[id].value) end )
	w = CreateWidget( constants.wt_Label, "menu_element", nil, (CONFIG.scr_width-(sx1+sx2+sx3+30))/2+15+sx1, (CONFIG.scr_height/2) + menus[num].h, sx2, sy2 )
	WidgetSetCaptionColor( w, o.m_color, false )
	WidgetSetCaptionFont( w, o.font, true )
	WidgetSetCaption( w, text[2] )
	b2p[w] = { num, #menus[num].widgets+1 }
	table.insert( menus[num].widgets, { 0, 0, w} )
	WidgetSetVisible( w, false )
	w = CreateWidget( constants.wt_Button, "menu_element", nil, (CONFIG.scr_width-(sx1+sx2+sx3+30))/2+30+sx1+sx2, (CONFIG.scr_height/2) + menus[num].h, sx1, sy1 )
	WidgetSetCaptionColor( w, o.p_color, false )
	WidgetSetCaptionColor( w, o.a_color, true )
	WidgetSetCaptionFont( w, o.font )
	WidgetSetCaption( w, text[3] )
	b2p[w] = { num, #menus[num].widgets+1 }
	table.insert( menus[num].widgets, { 0, 0, w, event, value=options.value or 1.0 } )
	WidgetSetVisible( w, false )
	WidgetSetLMouseClickProc( w, function() menus[num].widgets[id].value = math.min(1, menus[num].widgets[id].value+0.1) event(menus[num].widgets[id].value) end )
	menus[num].h = menus[num].h + sy1 + 10
end


