require "scripts/style"

function ShowSubmitOptions( show )
	SetWindowVisible( "delete", show )
	SetWindowVisible( "chooseok", show )
	SetWindowVisible( "editname", show )
end

function ShowCreateOptions( show )
	SetWindowVisible( "newplayer", show )
end

function UpdatePlayerChoices( curUser, numUsers, userTable )

	if (numUsers >= kMaxUsers) then
		ShowCreateOptions(false)
	else
		ShowCreateOptions(true)
	end

	if (numUsers==0) then
		ShowSubmitOptions( false )
	else
		ShowSubmitOptions( true )
	end

	for i = 0,numUsers-1 do
		SetLabel( "button"..i, userTable[ i+1 ] );
		SetWindowVisible( "button"..i, true ) ;
	end

	for i = numUsers, kMaxUsers do
		SetWindowVisible( "button"..i, false ) ;
	end

	if numUsers>0 then
		SetButtonToggleState( "button"..curUser, true );
	end

--	DebugOut("UpdatePlayerChoices");
end

NameFont = {
  standardFont,
  28,
  BlackColor
};

ButtonFontt = {
  standardFont,
  20,
  NormalBlackColor
};


HeaderFont = {
  standardFont,
  48,
  BlackColor
};

ButtonX = 378;
ButtonY = 192;
ButtonYSpace = 38;

kMaxUsers = 8

NameButtonGraphics = {
	"buttons/choosenameover.png",
	"buttons/choosenamedown.png",
	"buttons/choosenameover.png",
	"buttons/choosenamedown.png",
};

NameStyle= {
	type = kRadio,
	flags = kHAlignCenter + kVAlignCenter,
	font = NameFont,
	graphics = NameButtonGraphics,
	sound = kDefaultButtonSound
};

--gSelected = -1 ;

function AddButtons( N )

	t = { SetStyle(NameStyle), BeginGroup() };

	for i = 0,N-1 do
		table.insert(t,
			Button{
				name="button"..i,
--				command=function() gSelected = i; end,
				label="#name",
				x=ButtonX,
				y=ButtonY+(ButtonYSpace*i),
			}
		);
	end

	return Group(t);
end

local bx = 641
local by = 220
local bdy = 72

ButtonStyleProfile = {
	parent = ButtonStyle,
	graphics={"buttons/ir_btn_univ_sm01","buttons/ir_btn_univ_sm03","buttons/ir_btn_univ_sm02"},
}

MakeDialog
{
	-- Name the modal dialog
	name="chooseplayer",
	Bitmap
	{
		image="backgrounds/dark",
		x=0,
		y=0,
		w= 1024,
		h= 768,
		alpha=true,

		ChoosePlayerWindow
		{
			Bitmap
			{
				image="backgrounds/ir_ui_profile",
				x=512 - (465/2),
				y=384 - (537/2),
				w= 465,
				h= 537,
				alpha=true,
			};
			
			x = 0, y = 0, w=1024, h=768,
			AddButtons( kMaxUsers );

			SetStyle(ButtonStyleProfile);
			Button
			{
				font = ButtonFontt,
				--graphics={"buttons/ir_btn_univ_sm01","buttons/ir_btn_univ_sm03","buttons/ir_btn_univ_sm02"},
				x=525,
				y=577,
				name="editname",
				MenuButtonLabel3("editname",25,13),
				command =
					function()
						gEnterNameValue = GetSelectedPlayerName();
						name = DoModal("scripts/entername_edit.lua");
						if (not(name == nil)) then
							SetCurrentUser(GetCurrentSelectedUser());
							ChangeCurrentUserName(name);
							SetPlayerChoices();
							SetWelcomeName(name);
						end
					end
			};

			Button
			{
				font = ButtonFontt,
				--graphics={"buttons/ir_btn_univ_sm01","buttons/ir_btn_univ_sm03","buttons/ir_btn_univ_sm02"},
				x=370,
				y=577,
				name="chooseok",
				MenuButtonLabel3("ok",55,13),
				default=true,
				cancel=true,	-- ALSO allow them to exit the dialog by hitting escape
				command =
					function()
						if(not(GetCurrentSelectedUser() == GetCurrentUser())) then
							ChangeUser();
							SetCurrentUser(GetCurrentSelectedUser());
							ResetUpgradeStats();
							loadSaveData(true);
							LoadShownHints();
							loadPlayedTutorials();
							UpdateUpgradeTextures();
							--GetDefaultLayers("assets/DefaultLayers.xml");
							loadGameState();
							--ResetPlayedTutorials();
							UpdateUI();
							SetWelcomeName(GetCurrentUserName());
						end
						PopModal("chooseplayer");
					end

			};

			Button
			{
				font = ButtonFontt,
				--graphics={"buttons/ir_btn_univ_sm01","buttons/ir_btn_univ_sm03","buttons/ir_btn_univ_sm02"},
				x=370,
				y=520,
				name="newplayer",
				MenuButtonLabel3("newplayer",18,13),
				command =
					function()
						result = DisplayDialog {
							"scripts/yesno.lua",
							body="newplayerconfirm",
							title="newplayertitle"
						};
						if(result=='yes') then
							setIsANewChar(1);
							PopModal("chooseplayer");
							DoModal("scripts/intro.lua");							
						end
						--gEnterNameValue = "";
						--name = DoModal("scripts/entername.lua");
						--if (not(name == nil)) then
						--	CreateNewUser(name);
						--	resetRoomData();
						--	initRoomData(0);
						--	resetMoney();
						--	resetHintNumber();
						--	ResetGameState();
						--	ResetUpgradeStats();
						--	SetGameState("innview");
						--	setIsANewChar(1);
						--	ResetPlayedTutorials();
						--	saveRoom();
						--	saveData();
						--	SetPlayerChoices()
						--end
					end
			};

			Button
			{
				font = ButtonFontt,
				--graphics={"buttons/ir_btn_univ_sm01","buttons/ir_btn_univ_sm03","buttons/ir_btn_univ_sm02"},
				x=525,
				y=520,
				name="delete",
				MenuButtonLabel3("deleteplayer",11,13),
				command =
					function()
						--if(CanDeleteUser(GetCurrentSelectedUser())) then
							result = DisplayDialog {
							"scripts/yesno.lua",
							body ="deleteconfirm",
							title = "deletetitle"
							};
							if (result=='yes') then
								DeleteUser(GetCurrentSelectedUser())
								resetRoomData();
								SetPlayerChoices();
								ChangeUser();
								SetCurrentUser(GetCurrentSelectedUser());
								ResetUpgradeStats();
								loadSaveData(true);
								LoadShownHints();
								loadPlayedTutorials();
								UpdateUpgradeTextures();
								--GetDefaultLayers("assets/DefaultLayers.xml");
								loadGameState();
								--ResetPlayedTutorials();
								UpdateUI();
								SetWelcomeName(GetCurrentUserName());
							end
						--else
						--	DisplayDialog {
						--	"scripts/ok.lua",
						--	body ="cannotdeleteuser",
						--	title = ""
						--	};
						--end
					end

			};
		}
	}
}


--UpdatePlayerChoices( 0,0, {} );

--SetPlayerChoices();

