user_profiles = {}
user_profiles.temp_profile = _T("")

user_profiles.new_player_string_id = _T("$353")

function user_profiles.run_profiles_dialog(_owner)
	local app = sf.core.g_Application			
	local window = s4.COnKeyDownWindow()
	window.Load(
		app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _T("s4_user_profiles"), true), 
		app.GetSettings().GetChild(_T("GUITemplates"), false))
	window.UpdateLayout()
	MoveWindowToCenter(_owner, window)

	user_profiles.temp_profile = _T("")
	local current_profile = s4.g_ProfileManager.Instance().GetCurrentProfile()
	if current_profile ~= nil then
		user_profiles.temp_profile = current_profile.GetName()
	end

	local list_widget = __cast(window.GetWidget(_T("profiles_list")).get(), "sf.gui.CListWidget")
	if list_widget then
	
		user_profiles.update_list_with_profiles(list_widget)
	end

	local window_ptr = sf.gui.CBaseWindowPtrT(window)
	_owner.DoModal(window_ptr)
	window_ptr = nil
	collectgarbage()

end

function user_profiles.run_create_new_profile_dialog(_owner)
	local app = sf.core.g_Application			
	local window = s4.COnKeyDownWindow()
	window.Load(
		app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _T("s4_new_user"), true), 
		app.GetSettings().GetChild(_T("GUITemplates"), false))
	window.UpdateLayout()
	MoveWindowToCenter(_owner, window)

	local edit_widget = __cast(window.GetWidget(_T("edit")).get(), "sf.gui.CEditWidget")
	edit_widget.SetText(sf.misc.g_StringTable.Instance().FormatByStringsID(user_profiles.new_player_string_id))
	window.SetChildFocused(edit_widget)
	edit_widget.SelectText(0, edit_widget.GetText().size())
	
	local window_ptr = sf.gui.CBaseWindowPtrT(window)
	local modal_res = _owner.DoModal(window_ptr)
	window_ptr = nil
	collectgarbage()

	return 	modal_res
end

function user_profiles.change_player_dialog_script(_owner, _action, _child)
	local app = sf.core.g_Application
	local list_widget = __cast(_owner.GetWidget(_T("profiles_list")).get(), "sf.gui.CListWidget")
	if _child.GetId()==_T("profiles_list") and _action==_T("line_selected") then
		
		current_line = list_widget.GetSelectedLine()
		if current_line ~= -1 then
			s4.g_ProfileManager.Instance().SetCurrentProfile(list_widget.GetLine(current_line))
			main_menu.init_welcome(_owner.GetParentWindow())
		else
		end
		
	elseif _child.GetId()==_T("ok") and _action==_T("pressed") or _action == _T("key_down_enter") then
		s4.g_ProfileManager.Instance().Save()
		s4.g_GameOptions.Instance().ApplyOptionsChanges()
		_owner.EndModal(1)
	elseif _child.GetId()==_T("new") and _action==_T("pressed")  then
		local modal_res =user_profiles.run_create_new_profile_dialog(_owner)

		if modal_res == 1 then
			main_menu.init_welcome(_owner.GetParentWindow())
			s4.g_ProfileManager.Instance().Save()
			s4.g_GameOptions.Instance().ApplyOptionsChanges()
			_owner.EndModal(1)
		elseif modal_res == 0 then
		end
	
	elseif _child.GetId()==_T("cancel") and _action==_T("pressed") or _action == _T("key_down_escape") then
		if user_profiles.temp_profile ~= _T("") then
			local old_profile = s4.g_ProfileManager.Instance().GetProfile(user_profiles.temp_profile)
			if old_profile ~= nil then
				s4.g_ProfileManager.Instance().SetCurrentProfile(user_profiles.temp_profile)
				main_menu.init_welcome(_owner.GetParentWindow())
			end
		end
		_owner.EndModal(0)
	elseif _child.GetId()==_T("delete") and _action==_T("pressed") then
			num_lines = list_widget.EnumLines()
			current_line = list_widget.GetSelectedLine()
			if num_lines > 0 and current_line ~= -1 then
				if user_profiles.run_delete_user_name_dialog(_owner, list_widget.GetLine(current_line)) == 1 then
					local profile_name = s4.g_ProfileManager.Instance().DeleteProfile(list_widget.GetLine(current_line))
					local num_of_profiles = s4.g_ProfileManager.Instance().EnumProfiles()
					if num_of_profiles > 0 then
						s4.g_ProfileManager.Instance().SetCurrentProfile(0)
					end
					user_profiles.update_list_with_profiles(list_widget)
					main_menu.init_welcome(_owner.GetParentWindow())				
					s4.g_ProfileManager.Instance().Save()
				end
			end
	elseif _child.GetId()==_T("rename") and _action==_T("pressed") then
		num_lines = list_widget.EnumLines()
		current_line = list_widget.GetSelectedLine()
		if num_lines > 0 and current_line ~= -1 then
			local window = s4.COnKeyDownWindow()
			window.Load(
				app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _T("s4_rename_user"), true), 
				app.GetSettings().GetChild(_T("GUITemplates"), false))
			window.UpdateLayout()
			MoveWindowToCenter(_owner, window)
			
			local selected_profile_name = list_widget.GetLine(current_line)
			--user_profiles.renamed_profile = s4.g_ProfileManager.Instance().GetCurrentProfile() -- GetProfile(selected_profile_name)
			
			local edit_widget = __cast(window.GetWidget(_T("edit")).get(), "sf.gui.CEditWidget")
			edit_widget.SetText(selected_profile_name)
			window_ptr = sf.gui.CBaseWindowPtrT(window)
			local modal_res = _owner.DoModal(window_ptr)
			window_ptr = nil
			collectgarbage()

			if modal_res == 1 then
				local list_widget = __cast(_owner.GetWidget(_T("profiles_list")).get(), "sf.gui.CListWidget")
				if list_widget then
					user_profiles.update_list_with_profiles(list_widget)
				end
				main_menu.init_welcome(_owner.GetParentWindow())
				s4.g_ProfileManager.Instance().Save()
			elseif modal_res == 0 then
			end
		else
		end
	end
end

function user_profiles.update_list_with_profiles(_list_widget)
	
	_list_widget.ClearLines()
	
	local num_profiles = s4.g_ProfileManager.Instance().EnumProfiles()
	if num_profiles > 0 then
		local current_profile_name = _T("")
		local current_profile = s4.g_ProfileManager.Instance().GetCurrentProfile()
		if current_profile ~= nil then
			current_profile_name = current_profile.GetName()
		end
	
		local i = 0
		local current_profile_id = -1
		while i ~= num_profiles do 
			local new_line_id = s4.g_ProfileManager.Instance().GetProfile(i).GetName()
			if new_line_id == current_profile_name then
				current_profile_id = i
			end
			_list_widget.AddLine(new_line_id)
			i = i + 1
		end
		_list_widget.SelectLine(current_profile_id, false)
	else
	end
	
end

function user_profiles.new_player_dialog_script(_owner, _action, _child)
	if _child.GetId()==_T("ok") and _action==_T("pressed") or _action == _T("key_down_enter") then
		local edit_widget = __cast(_owner.GetWidget(_T("edit")).get(), "sf.gui.CEditWidget")
		local edit_text = edit_widget.GetText()
		if edit_text == _T("") then
			user_profiles.run_empty_user_name_dialog(_owner)
		else
			local find_profile_res = s4.g_ProfileManager.Instance().GetProfile(edit_text)
			if find_profile_res then
					user_profiles.run_dublicate_profile_dialog(_owner)
			else
				
				s4.g_ProfileManager.Instance().CreateProfile(edit_text, true)

				local widget_ = _owner.GetParentWindow().GetWidget(_T("profiles_list")).get()
				if widget_ then
					local list_widget = __cast(widget_,		"sf.gui.CListWidget")
					if list_widget then
						user_profiles.update_list_with_profiles(list_widget)
					end
				end
				_owner.EndModal(1)
			end
		end
	elseif _child.GetId()==_T("cancel") and _action==_T("pressed") or _action == _T("key_down_escape") then
		_owner.EndModal(0)
	end
end

function user_profiles.ok_action(_owner, _action, _child)
	if _child.GetId()==_T("ok") and _action==_T("pressed") or _action == _T("key_down_enter") then
		_owner.EndModal(1)
	elseif _child.GetId()==_T("cancel") and _action==_T("pressed") or _action == _T("key_down_escape") then
		_owner.EndModal(0)
	end
end

function user_profiles.yes_no_action(_owner, _action, _child)
	if _child.GetId()==_T("yes") and _action==_T("pressed") then
		_owner.EndModal(1)
	elseif _child.GetId()==_T("no") and _action==_T("pressed") then
		_owner.EndModal(0)
	end
end



function user_profiles.run_delete_user_name_dialog(_owner, _user_name)
	local app = sf.core.g_Application
	local window = s4.COnKeyDownWindow()
	window.Load(
	app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _T("s4_user_delete_profile"), true), 
		app.GetSettings().GetChild(_T("GUITemplates"), false))
														    
	local label_widget = __cast(window.GetWidget(_T("delete_message_label")).get(), "sf.gui.CLabelWidget")
	
	local template_text = label_widget.GetText()
	
	local new_text = string.format (template_text.c_str(), _user_name.c_str())
	
	label_widget.SetText(_T(new_text))
	
	
	window.UpdateLayout()                                             
	MoveWindowToCenter(_owner, window)
	local window_ptr = sf.gui.CBaseWindowPtrT(window)
	local modal_res = _owner.DoModal(window_ptr)
	window_ptr = nil
	collectgarbage()
	return modal_res
end


function user_profiles.run_empty_user_name_dialog(_owner)
	local app = sf.core.g_Application
	local window = s4.COnKeyDownWindow()
	window.Load(
	app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _T("s4_user_empty_user_name"), true), 
		app.GetSettings().GetChild(_T("GUITemplates"), false))
	window.UpdateLayout()                                             
	MoveWindowToCenter(_owner, window)
	local window_ptr = sf.gui.CBaseWindowPtrT(window)
	_owner.DoModal(window_ptr)
	window_ptr = nil
	collectgarbage()

end

function user_profiles.run_dublicate_profile_dialog(_owner)
	local app = sf.core.g_Application
	local window = s4.COnKeyDownWindow()
	window.Load(
		app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _T("s4_user_dublicate_profile"), true), 
		app.GetSettings().GetChild(_T("GUITemplates"), false))
	window.UpdateLayout()                                             
	MoveWindowToCenter(_owner, window)
	local window_ptr = sf.gui.CBaseWindowPtrT(window)
	_owner.DoModal(window_ptr)
	window_ptr = nil
	collectgarbage()
end

function user_profiles.rename_player_dialog_script(_owner, _action, _child)
	if _action==_T("key_down_escape") then
		_owner.EndModal(0)
	elseif (_child.GetId()==_T("ok") and _action==_T("pressed")) or  _action==_T("key_down_enter") then
		local edit_widget = __cast(_owner.GetWidget(_T("edit")).get(), "sf.gui.CEditWidget")
		local edit_text = edit_widget.GetText()
		if edit_text ~= _T("") then
			local current_profile = s4.g_ProfileManager.Instance().GetCurrentProfile()
			local find_profile_res = s4.g_ProfileManager.Instance().GetProfile(edit_text)
			if  current_profile ~= nil and find_profile_res ~= nil  and current_profile.GetName() ~= edit_text then
					user_profiles.run_dublicate_profile_dialog(_owner)
			else
					local renamed_curent_profile = s4.g_ProfileManager.Instance().GetCurrentProfile() 
					renamed_curent_profile.SetName(edit_text)
					_owner.EndModal(1)
			end
		else
			user_profiles.run_empty_user_name_dialog(_owner)
		end
	elseif _child.GetId()==_T("cancel") and _action==_T("pressed") then
		_owner.EndModal(0)
	end
end
