require( "scripts/styles/accountsstyle.lua" );
require( "scripts/styles/uistyle.lua" );

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

if numberUsers > 5 then
	numberUsers = 5
end

-- Checkboxes initial positions
cbxPx = 465;
cbxPy = 243;
space = 48;

-- Labels initial positions
lblPx = cbxPx + 45;
lblPy = cbxPy + 10;

local function createButtons()
	t = {};
	
	if numberUsers ~= 0 then
		t = {
			SetStyle(Button2Style),
			BlgButton{
				name = "deleteBtn",
				transformMatrix = {x = 707, y = 413},
				label = "wnd_accounts_delete",
--~ 				font = {
--~ 					standardFont,
--~ 					16,
--~ 					WhiteColor,
--~ 				},
				upState = {
					bitmap = "common/buttons/btn_default",
				},
				overState = {
					bitmap = "common/buttons/btn_rollover",
				},
				downState = {
					bitmap = "common/buttons/btn_pushed",
					sound = kDefaultButtonSound,
				},
				command = function()
					local player = GetSelectedPlayer();
					playerName = GetUserName(player);

					if player ~= -1 then
						t = {"scripts/states/stateyesnodialog.lua", body = "deleteconfirm", title = "deletetitle", yes = function () gDialogTable.result = true; PopState(); end};
						DisplayStateDialog(t);

						if t.result then

							--Remove the user from the user manager and TPrefs
							DeleteUser(player);

							SwapToState("scripts/states/stateaccounts.lua");

							--SelectFirstValidPlayer();
							SetWelcomeName();
						end
					end
				end
			},
		
			SetStyle(Button3Style),
			BlgButton{
				name = "chooseBtn",
				transformMatrix = {x =680, y = 476},
				label = "wnd_accounts_close",
--~ 				font = {
--~ 					standardFont,
--~ 					16,
--~ 					WhiteColor,
--~ 				},
				upState = {
					bitmap = "common/buttons/btn_default",
				},
				overState = {
					bitmap = "common/buttons/btn_rollover",
				},
				downState = {
					bitmap = "common/buttons/btn_pushed",
					sound = kDefaultButtonSound,
				},
				command = function()
					local player = GetSelectedPlayer();
					if player ~= -1 then
						SetCurrentUser(player);
						SetWelcomeName();
						LoadUserData();
						PopState();
						return;
					end

					PopState();
				end
			},
		};
	end
	
	return Group(t);
end

-- Define a function to create a given number of checkboxes
function CreateCheckBoxes(number)
	local t = {};
	for i = 0, number - 1 do
		table.insert(t,
			BlgContainer{
				BlgCheckBox
				{
					group = "grp1",
					-- 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),
					DebugOut(tostring(i).." "..GetUserName(i));
					transformMatrix = { x = cbxPx, y = cbxPy + (space * i) },

					selectedIcon = {
						image = "common/popups/selectplayer/icon_selected",
						transformMatrix = {x = 0, y = 0},
						center = {x = 0, y = 0},
					},

					font = ChangePlayerUserFont,

					upState = {
						bitmap = "common/popups/selectplayer/icon",
					},
					overState = {
						bitmap = "common/popups/selectplayer/icon_over",
					},
					downState = {
						bitmap = "common/popups/selectplayer/icon_down",
					},
				},
--~ 				BlgBitmap{
--~ 					image = "common/backgrounds/line",
--~ 					name = "line",
--~ 					center = {x = 0, y = 0},
--~ 					transformMatrix = { x = cbxPx-160, y = cbxPy + (space * i) },
--~ 				},
			}		
		);
	end

	return Group(t);
end

-- Define a function to create a given number of labels
function CreateLabels(number)
	local t = {};
	for i = 0, number - 1 do
		local userName = GetUserName(i);
		table.insert(t,
			BlgText
			{
				name = "lbl"..userName,
				transformMatrix = { x = lblPx, y = lblPy + (space * i) },
				w = 250, h = ChangePlayerUserFont[2] + 10,
				label = "#"..userName,

				font = ChangePlayerUserFont,
			}
		);
	end

	return Group(t);
end

-- Function that will turn on the check box of the current user,
-- each time the change player window is shown
function ActivateCheckBoxCurrentPlayer()
	for i = 0, numberUsers - 1 do
		local buttonName = GetUserName(i);

		if GetCurrentUserName() == buttonName then
			BlgSetButtonToggleState(buttonName, true);
			break;
		end
	end
end

-- Function that will activate the checkbox of the first player in the list
function SelectFirstValidPlayer()
	-- Turn on the checkbox of the first valid player
	if numberUsers ~= 0 then
		BlgSetButtonToggleState(GetUserName(0), true)
	end
end

-- Function that will return the index of the selected checkbox
function GetSelectedPlayer()
	for i = 0, numberUsers do
		local buttonName = GetUserName(i);

		if BlgGetButtonToggleState(buttonName) then
			return i;
		end
	end

	return -1;
end

MakeState{

	BlgBitmap{
		image = "common/backgrounds/alpha_black",
		name = "mainmenuBG",
		center = {x = 0, y = 0},
	},
	
	BlgMovieClip{
		--transformMatrix = {x = 150, y = 142},

		SetStyle(WindowBGStyle),
			
		BlgMovieClip{
			repeatCount = 0,
			SetStyle(PalmTrunkStyle),
			BlgContainer{
				transformMatrix = {x = 700, y = 200},
				BlgBitmap {
					image="common/backgrounds/trunk",
					center = {x = 27, y = 122},
					transformMatrix = {x = 0, y = 0},
				},
				BlgBitmap {
					image = "common/backgrounds/hojas",
					center = {x = 90, y = 202},
					transformMatrix = {x = 0, y = 0},
				},
				
			},	
		},
			
				-- Graphic content
		SetStyle(WindowBGStyle),
		
		BlgBitmap {
			image="common/backgrounds/box_mainmenu",
			center = {x = 0, y = 0},
			transformMatrix = {x = 307, y = 174},
		},
		
		BlgBitmap {
			image="common/backgrounds/text_container",
			center = {x = 0, y = 0},
			transformMatrix = {x = 336, y = 176},
		},

			
		BlgContainer {
			name = "back",
			
			
			-- Create the label that asks for the players selection
			BlgText
			{
				transformMatrix = {x = 500, y = 195},
				w = 300, h = ChangePlayerLabelFont[2] + 6,
				flags = kHAlignLeft | kVAlignTop,
				label = "wnd_accounts_pickaccount",
				font = ChangePlayerLabelFont,
				--dropShadow = true,
			},

			CreateCheckBoxes(numberUsers),
			CreateLabels(numberUsers),
		},
		SetStyle(WindowLogoStyle),
		BlgBitmap{
			image = "common/backgrounds/logo_01_big",
			name = "mainmenuBG",
			center = {x = 0, y = 0},
			transformMatrix = {x = 236, y = 185},
		},
	
--~ 		SetStyle(WindowLogoStyle),
--~ 		BlgMovieClip{
--~ 			SetStyle(LogoCircleStyle),
--~ 			repeatCount = -1,
--~ 			BlgBitmap{
--~ 				image = "common/backgrounds/logo_bg",
--~ 				name = "logo",
--~ 				center = {x = 0, y = 0},
--~ 				transformMatrix = {x = 19, y = 112},
--~ 			},
--~ 			SetStyle(nil),
--~ 			BlgBitmap{
--~ 				image = "common/backgrounds/logo_menu",
--~ 				name = "logo2",
--~ 				center = {x = 80, y = 32},
--~ 				playAnim = "DoAnim",
--~ 			},
--~ 		},
		
		SetStyle(Button1Style),
		BlgButton{
			name = "newBtn",
			transformMatrix = {x = 735, y =340},
			label = "wnd_accounts_newaccount",
--~ 			font = {
--~ 				standardFont,
--~ 				16,
--~ 				WhiteColor,
--~ 			},
			upState = {
				bitmap = "common/buttons/btn_default",
			},
			overState = {
				bitmap = "common/buttons/btn_rollover",
			},
			downState = {
				bitmap = "common/buttons/btn_pushed",
				sound = kDefaultButtonSound,
			},
			command = function()
				if GetNumUsers() >= 5 then
					PushState({"scripts/states/okdialog.lua", title = "wnd_accounts_maxexceeded"});
				else
					PopState();
					PushState("scripts/states/staterequestname.lua");
				end
			end
		},
		
		createButtons();
	},
};

ActivateCheckBoxCurrentPlayer();
