-- wndchangeplayer.lua

require("scripts/styles/dialogs/requestnamestyle.lua")
require("scripts/styles/dialogs/changeplayerstyle.lua")
require("scripts/styles/mainmenustyle.lua");

-- Get the number of users (in order to create the appropiate number of checkboxes)
numberUsers = UserCount()

offset = 50
okCancelPosY = 415

maxUsers = 7 

-- Checkboxes initial positions
cbxPx = 326
cbxPy = 270
space = 38

-- Labels initial positions
lblPx = 370
lblPy = 270

-- Define a function to create a given number of checkboxes
function CreateCheckBoxes(number)

	t = {SetStyle(ChangePlayerCheckboxStyle), BeginGroup()};
	for i = 0, number - 1 do			
			table.insert(t,
			Button
			{
				-- Use each user in existence as the name for its corresponding checkbox.
				-- This will make things much easier for us, when dealing with the selection
				-- of checkboxes
				name = GetUserName(i),
				x = cbxPx,
				y = cbxPy + (space * i),
			}
			);	
	end
	
	return Group(t)
end

-- Define a function to create a given number of labels
function CreateLabels(number)

	t = {SetStyle(ChangePlayerTextStyle), };
	for i = 0, number - 1 do
			table.insert(t,
			Text
			{
				x = lblPx,
				y = lblPy + (space * i),
				label = "#"..GetUserName(i),
				name = "lbl"..GetUserName(i)
			}
			);	
	end
	
	return Group(t)
end


MakeDialog 
{
	BlgBitmap
	{
		image = "common/screens/accounts/accounts_box",
		x = kCenter, y = kCenter,
	},
	
	WndChangePlayer
	{
		x = kCenter -(10), y = kCenter,
		w=kMax, h=kMax,
		--fit = true,
		
		SetStyle(AccountsTitleStyle),
		
		-- Create the label that asks for the players selection
		Text 
		{
			x = kCenter, y = 210.0,
			w = kMax, h = kMax,
			flags = kHAlignCenter,
			label = "selectaplayer",
		},
	
		CreateCheckBoxes(numberUsers),
		CreateLabels(numberUsers),
	
		-- Create all the dialogs buttons
		--SetStyle(OrangeButtonStyle),
		SetStyle(GreenButtonStyle),
		
	
		-- OK button
		BlgButton
		{
			name = "btnOK",
			label = "ok",
			x = 337, y = 532,
			width = 300, height = 300,
			positionMatrix = {x = 10, y = 10},
			graphics =
			{
				"common/screens/accounts/accounts_orange_button",
				"",
				"",
				"",
			},
			command = function()
						local player = GetSelectedPlayer()
						if player ~= -1 then
							ChangeUser(player)
					  		CloseWindow()
					  		SetWelcomeName()
					  	end
					  end
		},
	
		-- Cancel button
		BlgButton
		{
			name = "btnCancel",
			label = "cancel",
			x = 510, y = 532,
			width = 300, height = 300,
			positionMatrix = {x = 10, y = 10},
			graphics =
			{
				"common/screens/accounts/accounts_orange_button",
				"",
				"",
				"",
			},
			command = function()
						CloseWindow()
		        	  end
		},
		
		SetStyle(NewDeleteAccountStyle),
		
		-- New player button
		BlgButton
		{
			name = "btnNewPlayer",
			x = 695.0, y = 291.0,
			label = "newplayer",
			width = 300, height = 300,
			positionMatrix = {x = 10, y = 10},
			graphics =
			{
				"common/screens/accounts/accounts_green_button",
				"",
				"",
				"",
			},
			command = function()
						if UserCount() >= maxUsers then
							DisplayDialog
							{
								"scripts/windows/dialogs/usercountexceeded.lua"
							};
						else
							DoModalWithTransition("dialogs/requestname.lua")
							CloseWindow()
						end
		          	  end
		},
	
		-- Delete player button
		BlgButton
		{
			name = "btnDeletePlayer",
			label = "deleteplayer",
			x = 695.0, y = 420.0,
			width = 300, height = 300,
			positionMatrix = {x = 10, y = 10},
			graphics =
			{
				"common/screens/accounts/accounts_green_button",
				"",
				"",
				"",
			},
			command = function()
						local player = GetSelectedPlayer()
						playerName = GetUserName(player)
						if player ~= -1 then
							t = {"scripts/windows/wnddialog.lua", body = "deleteconfirm", title = "deletetitle"};
							DisplayDialog(t);
							
							if t.result then
							
								--Remove the user from the user manager and TPrefs
								DeleteUser(player)
								
								--Disable the corresponding checkbox and label, of the currently deleted user
								EnableWindow("lbl"..playerName, false)
								EnableWindow(""..playerName, false) 
								
								UpdatePlayerGUI()
								SelectFirstValidPlayer()
								
								EnableWindow("btnCancel", false)
								
								-- If there are no more users, we hide all buttons except the one to create a new player
								if UserCount() == 0 then
									EnableWindow("btnDeletePlayer", false)
									EnableWindow("btnOK", false)
								end
								
								SetWelcomeName()
							end
						end
		        	  end
		},
	}
}