require("player")
require("enemies")
require("bullets")
require("gui")
require("timed_events")
require("items")
require("menus")
require("lights")
require("lib")

TICK = 7

function PauseGame()
	paused = true
	local pause_widget = CreateWidget(constants.wt_Widget, "fade_box", nil, 0, 0, CONFIG.scr_width, CONFIG.scr_height)
	WidgetSetZ(pause_widget, 1)
	WidgetSetVisible(pause_widget, true)
	WidgetSetColorBox(pause_widget, {0, 0, 0, 0.5} )
	pause_widgets = { pause_widget }
	local sx, sy = GetCaptionSize( "default", "PAUSED" )
	pause_widget = CreateWidget(constants.wt_Label, "fade_box", nil, (CONFIG.scr_width-sx)/2, (CONFIG.scr_height-sy)/2, 1, 1)
	WidgetSetVisible(pause_widget, true)
	WidgetSetCaptionColor(pause_widget, {1,1,1,1}, false )
	WidgetSetCaption( pause_widget, "PAUSED" )
	table.insert( pause_widgets, pause_widget )
	sx, sy = GetCaptionSize( "default", "PRESS Q TO RETURN TO MENU" )
	pause_widget = CreateWidget(constants.wt_Label, "fade_box", nil, (CONFIG.scr_width-sx)/2, (CONFIG.scr_height-sy)/2+sy*2, 1, 1)
	WidgetSetVisible(pause_widget, true)
	WidgetSetCaptionColor(pause_widget, {1,1,1,1}, false )
	WidgetSetCaption( pause_widget, "PRESS Q TO RETURN TO MENU" )
	table.insert( pause_widgets, pause_widget )
	TogglePause()
end

function UnpauseGame()
	paused = false
	for i=1, #pause_widgets do
		DestroyWidget( pause_widgets[i] )
	end
	TogglePause()
end

function ProcessWin()
	if red_destroyed then
		red_destroyed = nil
		green_destroyed = nil
		HideGUI( false )
		in_game = false
		ShutDownEverything()
		InitNewGame()
		CreateParticleSystem( "pmenu", 0, 0 )
		PlayBackMusic("music/highscore.mp3")
		ShowCrawl( "texts/ending1.txt", function()
			ShowCrawl( "texts/credits.txt", function()
				Loader.start( "menu" )
			end )
		end )
	elseif green_destroyed then
		red_destroyed = nil
		green_destroyed = nil
		HideGUI( false )
		in_game = false
		ShutDownEverything()
		InitNewGame()
		CreateParticleSystem( "pmenu", 0, 0 )
		PlayBackMusic("music/highscore.mp3")
		ShowCrawl( "texts/ending2.txt", function()
			ShowCrawl( "texts/credits.txt", function()
				Loader.start( "menu" )
			end )
		end )
	end
end

function NewGame()
	InitNewGame()
	last_update = GetCurTime()
	PreparePlayers( players_num )
	start_time = GetCurTime()
	local_time = 0
	return function()
		local last_time = start_time
		local spare_time = 0
		in_game = true
		Resume( NewThread( function()
			local alt_time
			while in_game do
				alt_time = GetCurTime()
				if last_update and alt_time - last_update > 3000 then
					in_game = false
					local widget = CreateWidget(constants.wt_Widget, "fade_box", nil, CONFIG.scr_width/2-200, CONFIG.scr_width/2-50, 400, 100)
					WidgetSetZ(widget, 0.9)
					WidgetSetVisible(widget, true)
					WidgetSetColorBox(widget, {1, 0, 0, 1} )
					widget = CreateWidget(constants.wt_Label, "fade_box", nil, CONFIG.scr_width/2-190, CONFIG.scr_width/2-40, 380, 80)
					WidgetSetZ(widget, 1)
					WidgetSetVisible(widget, true)
					WidgetSetCaption( widget, "SOMETHING TERRIBLE HAPPENED. Think about what you have done and never do it again. Or you can report this error, possiblu along with your logs, of course. Not that anyone would listen, mind you. And do remind them that I said it would happen. (f11 to take screenshot)", true )
					WidgetSetCaptionFont( widget, "small" )
					WidgetSetCaptionColor(widget, {0, 0, 0, 1}, false )
					
					local error_message = {}
					local em
					for line in io.lines("log/script.log") do
						em = string.match( line, "%[ERROR%](.+)" )
						if em then table.insert( error_message, em.."/n" ) end
						em = string.match( line, "%[WARNING%](.+)" )
						if em then table.insert( error_message, em.."/n" ) end
					end
					for line in io.lines("log/iichantra.log") do
						em = string.match( line, "%[ERROR%](.+)" )
						if em then table.insert( error_message, em.."/n" ) end
						em = string.match( line, "%[WARNING%](.+)" )
						if em then table.insert( error_message, em.."/n" ) end
					end
					error_message = table.concat( error_message ) or "I have no idea what just happened."
					
					widget = CreateWidget(constants.wt_Widget, "fade_box", nil, 0, CONFIG.scr_width/2+100, CONFIG.scr_width, 800 )
					WidgetSetZ(widget, 0.9)
					WidgetSetVisible(widget, true)
					WidgetSetColorBox(widget, {0, .1, 0, 1} )
					widget = CreateWidget(constants.wt_Label, "fade_box", nil, 10, CONFIG.scr_width/2+110, CONFIG.scr_width-20, 800)
					WidgetSetZ(widget, 1)
					WidgetSetVisible(widget, true)
					WidgetSetCaption( widget, error_message, true )
					WidgetSetCaptionFont( widget, "small" )
					WidgetSetCaptionColor(widget, {1, 1, 1, 1}, false )
				end
				Wait(1)
			end
		end ))
		while in_game do
			real_time = GetCurTime()
			time = real_time
			spare_time = spare_time + (real_time - last_time)
			last_update = real_time
			while in_game and spare_time > TICK do
				last_update = real_time
				ProcessInput()
				if not paused  and in_game then
					time = time + TICK
					local_time = local_time + TICK
					ProcessPlayer()
					ProcessEnemies()
					ProcessBullets()
					ProcessWin()
					ProcessGUI()
					ProcessTimedEvents()
					ProcessItems()
					ProcessLights()
					if ProcessBackground then
						ProcessBackground()
					end
					FinishDeletion()
				else
					if keys_held[keys.q] then
						StopThreads()
						UnpauseGame()
						HideGUI()
						in_game = false
						ShutDownEverything()
						Loader.start( "menu" )
					end
				end
				spare_time = spare_time - TICK
			end
			last_time = time
			Wait(1)
		end
	end
end
