require("menus")
require("lib")
dofile("config/starstuff.lua")

InitNewGame()

if not first_menu_created then
	first_menu_created = true

	options_types =
	{
		{
			enemy_reload = 1,
			player_size = 1,
			player_reload = 1,
			enemy_health = 1,
			mode = "classic"
		},
		{
			mode = "touhota",
			enemy_reload = 0.75,
			player_size = 1/3,
			player_reload = 0.25,
			enemy_health = 4,
		},
		{
			mode = "shmup",
			enemy_reload = 3,
			player_size = 2,
			player_reload = 1,
			enemy_health = 1,
		},
		{
			mode = "easy",
			enemy_reload = 3,
			player_size = 1,
			player_reload = 0.75,
			enemy_health = 1.25,
		}
	}

	options = options_types[config.mode]
	options_num = config.mode

	players_num = config.players_num

	AddButton( 1, "START!", function() 
					in_menu = false 
					HideMenu(1)
					DestroyWidget( w_tip )
					require( "game" )
					Loader.start( "level1" )
				end )
	AddButton( 1, "ENTER PASSWORD", function() 
					HideMenu(1)
					ShowMenu(10)
				end )
	AddButton( 1, "OPTIONS", function()
					HideMenu(1)
					ShowMenu(9)
				end )
	AddButton( 9, "CHANGE KEYS", function()
					HideMenu(9)
					ShowMenu(2)
				end )
	AddButton( 9, "CHANGE PLAYERS", function()
					HideMenu(9)
					ShowMenu(6)
				end )
	AddButton( 9, "MODE: "..options.mode, function( num, w )
					options_num = options_num + 1
					if options_num > #options_types then
						options_num = 1
					end
					config.mode = options_num
					options = options_types[options_num]
					WidgetSetCaption( menus[num].widgets[w][3], "MODE: "..options.mode )
					local sx, sy = GetCaptionSize( "default", "MODE: "..options.mode )
					menus[num].widgets[w][1] = (CONFIG.scr_width - sx)/2
					WidgetSetPos( menus[num].widgets[w][3], menus[num].widgets[w][1], menus[num].widgets[w][2] )
				end )
	AddButton( 9, "PLAYERS: "..config.players_num, function( num, w )
					players_num = 3 - players_num
					config.players_num = players_num
					WidgetSetCaption( menus[num].widgets[w][3], "PLAYERS: "..players_num )
				end )
	AddButton( 1, "STORY SO FAR", function()
					HideMenu(1)
					WidgetSetVisible( w_tip, false )
					SetObjInvisible( logo1, true )
					SetObjInvisible( logo2, true )
					ShowText( "texts/story.txt", 200, function()
						ShowMenu(1)
						WidgetSetVisible( w_tip, true )
						SetObjInvisible( logo1, false )
						SetObjInvisible( logo2, false )
					end )
				end )
	
	function ShowHowto()
		howto_sprites = {}
		local spr = CreateSprite( "sprite", 47, 206 )
		SetObjPos( spr, 52-300, 206-400 )
		table.insert( howto_sprites, spr )
		spr = CreateSprite( "sprite", 47, 206 )
		SetObjPos( spr, 52-300, 206-400+45 )
		table.insert( howto_sprites, spr )
		spr = CreateSprite( "sprite", 47, 206 )
		SetObjAnim(spr, 20, false)
		SetObjPos( spr, 52-300, 331-400 )
		table.insert( howto_sprites, spr )
		spr = CreateSprite( "sprite2", 47, 402 )
		SetObjAnim(spr, 3, false)
		SetObjPos( spr, 52-300, 402-400 )
		table.insert( howto_sprites, spr )
		spr = CreateSprite( "sprite", 47, 402 )
		SetObjAnim(spr, 7, false)
		SetObjPos( spr, 52-300, 478+15-400 )
		table.insert( howto_sprites, spr )
		local w = CreateWidget(constants.wt_Widget, "fade_box", nil, 52-options.player_size*16, 206+45-options.player_size*16, options.player_size*32, options.player_size*32)
		WidgetSetZ(w, 1)
		WidgetSetVisible(w, true)
		WidgetSetColorBox(w, {1, 0, 0, 0.5} )
		howto_widget = w
		Resume( NewThread( function()
			in_howto = true
			local frames = { 7, 23, 24 }
			local tiem
			while in_howto do
				tiem = GetCurTime()/1000
				WidgetSetColorBox(howto_widget, {1, 0, 0, 0.5+0.5*math.cos(tiem)} )
				SetObjAnim( howto_sprites[#howto_sprites], frames[math.floor(tiem % 3)+1], false )
				Wait(1)
			end
		end ))
	end
	
	function HideHowto()
		in_howto = false
		for key, value in pairs(howto_sprites) do
			SetObjDead( value )
		end
		DestroyWidget( howto_widget )
	end
				
	AddButton( 1, "HOW TO PLAY", function()
					HideMenu(1)
					ShowHowto()
					WidgetSetVisible( w_tip, false )
					SetObjInvisible( logo1, true )
					SetObjInvisible( logo2, true )
					ShowText( "texts/howto.txt", 200, function()
						ShowMenu(1)
						HideHowto()
						WidgetSetVisible( w_tip, true )
						SetObjInvisible( logo1, false )
						SetObjInvisible( logo2, false )
					end )
				end )
	AddButton( 1, "CREDITS", function()
					HideMenu(1)
					SetObjInvisible( logo1, true )
					SetObjInvisible( logo2, true )
					PlayBackMusic("music/highscore.mp3")
					WidgetSetVisible( w_tip, false )
					ShowCrawl( "texts/credits.txt", function()
						ShowMenu(1)
						WidgetSetVisible( w_tip, true )
						PlayBackMusic("music/come_in.mp3")
						SetObjInvisible( logo1, false )
						SetObjInvisible( logo2, false )
					end )
				end )
--[[
	AddButton( 1, "ENDING", function()
					HideMenu(1)
					SetObjInvisible( logo1, true )
					SetObjInvisible( logo2, true )
					PlayBackMusic("music/highscore.mp3")
					WidgetSetVisible( w_tip, false )
					ShowCrawl( "texts/ending1.txt", function()
						ShowCrawl( "texts/credits.txt", function()
							ShowMenu(1)
							WidgetSetVisible( w_tip, true )
							PlayBackMusic("music/come_in.mp3")
							SetObjInvisible( logo1, false )
							SetObjInvisible( logo2, false )
						end )
					end )
				end )
--]]
	AddButton( 2, "SINGLEPLAYER", function() HideMenu(2) ShowMenu(3) end )
	AddButton( 2, "PLAYER 1", function() HideMenu(2) ShowMenu(4) end )
	AddButton( 2, "PLAYER 2", function() HideMenu(2) ShowMenu(5) end )
	AddButton( 2, "BACK", function() RewriteConfig() HideMenu(2) ShowMenu(9) end )
	
	local function CreateOptionsPage( num, table )
		for i=1, #config.keys do
			AddButton( num, config.keys[i]..": "..GetKeyName(table[config.keys[i]]), function() ReadKey(num, i, table, config.keys[i]) end )
		end
		AddButton( num, "MOVEMENT TYPE: "..table.type, function( num ,w )
			if table.type == "keyboard" then table.type = "mouse"
			else table.type = "keyboard" end
			local sx, sy = GetCaptionSize("default", "MOVEMENT TYPE: "..table.type)
			menus[num].widgets[w][1] = (CONFIG.scr_width-sx)/2
			WidgetSetPos( menus[num].widgets[w][3], menus[num].widgets[w][1], menus[num].widgets[w][2] )
			WidgetSetCaption( menus[num].widgets[w][3], "MOVEMENT TYPE: "..table.type )
		end )
		AddButton( num, "BACK", function() HideMenu(num) ShowMenu(2) end )
	end

	CreateOptionsPage( 3, config.controls[0] )
	CreateOptionsPage( 4, config.controls[1] )
	CreateOptionsPage( 5, config.controls[1] )
	
	function CreateMenuShip(  num )
		menu_ship = { -200, 450, CreateSprite("sprite", -50, 450) , active = true}
		if num == 2 then
			menu_ship[1] = 200
		end
		SetObjAnim( menu_ship[3], 0, false )
		SetObjSpriteColor( menu_ship[3], config.players[num].color )
		SetObjSpriteColor( menu_ship[3],  {1, 1, 1, 1}, 0 )
		Resume( NewThread( function()
			while menu_ship.active and in_menu and menu_ship[2] > 50 do
					menu_ship[2] = menu_ship[2] - math.min( (menu_ship[2] - 45) * 0.1, 3 )
					SetObjPos( menu_ship[3], menu_ship[1], menu_ship[2] )
					Wait(1)
			end
			local anim = 0
			local pos = 1
			local frames = { 0, 21  }
			while menu_ship.active and in_menu do
				anim = anim + 1
				if anim > 100 then
					anim = 0
					pos = 3 - pos
					SetObjAnim( menu_ship[3], frames[pos], false )
					SetObjSpriteMirrored( menu_ship[3], random( {true, false} ) )
				end
				Wait(1)
			end
			SetObjAnim( menu_ship[3], 0, false )
			while in_menu and menu_ship[2] > -450 do
				menu_ship[2] = menu_ship[2] - 3
				SetObjPos( menu_ship[3], menu_ship[1], menu_ship[2] )
				Wait(1)
			end
			SetObjDead( menu_ship[3] )
			menu_ship = nil
		end ))
	end
	
	local function CreatePlayerPage( num, table )
		AddInputField( num, {"Name:", table.name}, function(value) table.name = value end )
		AddColorField( num, {"-", "R", "+"} , function(value) table.color[1] = value SetObjSpriteColor(menu_ship[3], table.color) end, {a_color = {1, 0, 0, 1}, p_color = {.3, .2, .2, 1}, m_color = {1, 0, 0, 1}, value = table.color[1]} )
		AddColorField( num, {"-", "G", "+"} , function(value) table.color[2] = value SetObjSpriteColor(menu_ship[3], table.color) end, {a_color = {0, 1, 0, 1}, p_color = {.3, .3, .2, 1}, m_color = {0, 1, 0, 1}, value = table.color[2]} )
		AddColorField( num, {"-", "B", "+"} , function(value) table.color[3] = value SetObjSpriteColor(menu_ship[3], table.color) end, {a_color = {0, 0, 1, 1}, p_color = {.2, .2, .7, 1}, m_color = {0, 0, 1, 1}, value = table.color[3]} )
		AddButton( num, "BACK", function() HideMenu(num) menu_ship.active = false ShowMenu(9) RewriteConfig() end )
	end
	
	AddButton( 6, "PLAYER 1", function() CreateMenuShip(1) HideMenu(6) ShowMenu(7) end )
	AddButton( 6, "PLAYER 2", function() CreateMenuShip(2) HideMenu(6) ShowMenu(8) end )
	AddButton( 6, "BACK", function() HideMenu(6) ShowMenu(9) end )
	
	CreatePlayerPage( 7, config.players[1] )
	CreatePlayerPage( 8, config.players[2] )

	lights_c = { [0]="OFF", "LOW", "HIGH" }

	AddButton( 9, "LIGHTS: "..lights_c[config.lights], function( num, w )
					config.lights = config.lights + 1
					if config.lights > 2 then
						config.lights = 0
					end
					WidgetSetCaption( menus[num].widgets[w][3], "LIGHTS: "..lights_c[config.lights] )
				end )
	AddButton( 9, "BACK", function() RewriteConfig() HideMenu(9) ShowMenu(1) end )
	
	AddInputField( 10, {"Password:", ""}, function(value) in_password = value end )
	AddButton( 10, "START!", function() 
					in_menu = false 
					HideMenu(10)
					DestroyWidget( w_tip )
					require( "game" )
					Loader.start( GetPassword(in_password) )
				end )
	AddButton( 10, "BACK", function() HideMenu(10) ShowMenu(1) end )
end

tips = {}
for line in io.lines("texts/tips.txt") do
	if line and #line > 1 then
		table.insert( tips, line )
	end
end

local rtip = random( tips )

w_tip = CreateWidget(constants.wt_Label, "tip", nil, 10, CONFIG.scr_height-35, 580, 35)
WidgetSetVisible(w_tip, true)
--WidgetSetCaptionColor(w_tip, {1,1,1,1}, false )
WidgetSetCaptionFont(w_tip, "small")
WidgetSetCaption( w_tip, "TIP OF THE DAY: "..rtip, true )

CreateParticleSystem( "pmenu", 0, 0 )

logo1 = CreateSprite( "logo", 126-300, 192-400 )
logo2 = CreateSprite( "logo", 126-300, 192-400 )

SetObjAnim( logo2, 1, false )
in_menu = true

Resume( NewThread( function()
	while in_menu do
		t = GetCurTime()/500
		col = { 0.5 + 0.5*math.cos(t), 0.5+0.5*math.sin(2*t), 0.5+0.5*math.cos(3*t), 1 }
		SetObjSpriteColor( logo2, col )
		Wait(1)
	end
end ) )

PlayBackMusic("music/come_in.mp3")
ShowMenu( 1 ) 

return function() Wait(1000) Loader.map_thread = nil end
