using Exceptionless; using Microsoft.AspNetCore.SignalR.Client; using Microsoft.Xna.Framework; using Myra.Graphics2D.Brushes; using Myra.Graphics2D.UI; using Sledgemapper.Shared.Entities; using System; using System.Diagnostics; using System.Linq; namespace Sledgemapper.UI { public partial class MainWidget { private AuthenticateResponse _authResponse; private readonly CommunicationManager CommunicationManager; public MainWidget(CommunicationManager communicationManager) { BuildUI(); CommunicationManager = communicationManager; MenuConnectLogin.Selected += OnMenuConnectLoginSelected; } public void ClearSelection() { ClearSelection(GridWalls); ClearSelection(GridOverlays); ClearSelection(Toolbar); } private void ClearSelection(Grid grid) { foreach (var widget in grid.Widgets) { widget.Border = null; } } private void ClearSelection(HorizontalStackPanel grid) { foreach (var widget in grid.Widgets) { widget.Border = null; } } private void OnMenuConnectLoginSelected(object sender, EventArgs e) { Window window = new() { Title = "Login" }; var content = new LoginRegisterWindow(); #if DEBUG content.TxtEmail.Text = "michele.scandura@outlook.com"; content.TxtPassword.Text = "slePharland!79"; #endif content.RdoLogin.IsPressed = true; content.RdoLogin.Click += (s, e) => { content.TxtFirstname.Visible = false; content.TxtLastname.Visible = false; content.TxtInitials.Visible = false; content.LblFirstname.Visible = false; content.LblLastname.Visible = false; content.LblInitials.Visible = false; content.BtnLogin.Visible = true; content.BtnRegister.Visible = false; window.Title = "Login"; }; content.RdoRegister.Click += (s, e) => { content.TxtFirstname.Visible = true; content.TxtLastname.Visible = true; content.TxtInitials.Visible = true; content.LblFirstname.Visible = true; content.LblLastname.Visible = true; content.LblInitials.Visible = true; content.BtnLogin.Visible = false; content.BtnRegister.Visible = true; window.Title = "Register"; }; content.BtnRegister.Click += OnButtonRegisterClick; content.BtnLogin.Click += OnButtonLoginClick; window.Content = content; window.ShowModal(Desktop); content.TxtEmail.SetKeyboardFocus(); } private async void OnButtonRegisterClick(object sender, EventArgs e) { var button = ((TextButton)sender); var localContent = GetParentContentInWindow(button);// localWindow.Content as PlayerWindow; if (!button.Enabled) { return; } var isValid = true; isValid &= ValidateTextbox(localContent.Content.TxtEmail); isValid &= ValidateTextbox(localContent.Content.TxtPassword); isValid &= ValidateTextbox(localContent.Content.TxtFirstname); isValid &= ValidateTextbox(localContent.Content.TxtLastname); isValid &= ValidateTextbox(localContent.Content.TxtInitials); if (!isValid) { localContent.Content.LblLoginError.Text = "Please complete all the fields"; localContent.Content.LblLoginError.Visible = true; return; } var successful = false; try { button.Text = "Wait..."; localContent.Content.LblLoginError.Text = ""; localContent.Content.LblLoginError.Visible = false; var result = await CommunicationManager.Register(new RegisterModel { Username = localContent.Content.TxtEmail.Text, Email = localContent.Content.TxtEmail.Text, Password = localContent.Content.TxtPassword.Text, FirstName = localContent.Content.TxtFirstname.Text, LastName = localContent.Content.TxtLastname.Text, Initials = localContent.Content.TxtInitials.Text }); if (result.Result) { _authResponse = await CommunicationManager.Login(new AuthenticateModel { Username = localContent.Content.TxtEmail.Text, Password = localContent.Content.TxtPassword.Text }); successful = true; } else { localContent.Content.LblLoginError.Text = result.Errors.FirstOrDefault(); localContent.Content.LblLoginError.Visible = true; } } catch (Refit.ApiException refitException) { ExceptionlessClient.Default.SubmitException(refitException); localContent.Content.LblLoginError.Text = refitException.Content; localContent.Content.LblLoginError.Visible = true; } catch (Exception ex) { ExceptionlessClient.Default.SubmitException(ex); localContent.Content.LblLoginError.Text = "Can't connect to the server"; localContent.Content.LblLoginError.Visible = true; Debug.Write(ex); } finally { button.Enabled = true; button.Text = "Register"; } if (successful) { MenuConnectNew.Enabled = true; MenuConnectJoin.Enabled = true; lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}"; localContent.Window.Close(); } } private async void OnButtonLoginClick(object sender, EventArgs e) { var button = ((TextButton)sender); Container container = button.Parent; while (!(container is Window)) { container = container.Parent; } var localWindow = (Window)container; var localContent = localWindow.Content as LoginRegisterWindow; if (!button.Enabled) { return; } var isValid = true; isValid &= ValidateTextbox(localContent.TxtEmail); isValid &= ValidateTextbox(localContent.TxtPassword); if (!isValid) { localContent.LblLoginError.Text = "Username or password is not valid"; localContent.LblLoginError.Visible = true; return; } var successful = false; try { button.Text = "Wait..."; localContent.LblLoginError.Text = ""; localContent.LblLoginError.Visible = false; _authResponse = await CommunicationManager.Login(new AuthenticateModel { Username = localContent.TxtEmail.Text, Email = localContent.TxtEmail.Text, Password = localContent.TxtPassword.Text }); successful = _authResponse != null; } catch (Refit.ApiException refitException) { ExceptionlessClient.Default.SubmitException(refitException); localContent.LblLoginError.Text = refitException.Content; localContent.LblLoginError.Visible = true; } catch (Exception ex) { ExceptionlessClient.Default.SubmitException(ex); localContent.LblLoginError.Text = "Can't connect to the server"; localContent.LblLoginError.Visible = true; Debug.Write(ex); } finally { button.Enabled = true; button.Text = "Login"; } if (successful) { MenuConnectNew.Enabled = true; MenuConnectJoin.Enabled = true; lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}"; localWindow.Close(); } } private void OnMenuConnectNewSelected(object sender, EventArgs e) { if (!((MenuItem)sender).Enabled) { return; } Window window = new() { Title = "New mapping session" }; var content = new SessionWindow(); content.BtnLogin.Text = "Join"; content.BtnLogin.Click += OnButtonNewSessionClicked; window.Content = content; window.ShowModal(Desktop); content.TxtSession.SetKeyboardFocus(); } private async void OnButtonNewSessionClicked(object sender, EventArgs e) { var localContent = GetParentContentInWindow(((TextButton)sender));// localWindow.Content as PlayerWindow; var isValid = ValidateTextbox(localContent.Content.TxtSession); if (!isValid) { return; } if (CommunicationManager.Connection.State != HubConnectionState.Connected) { _mainWidget.lblConnectionStatus.Text = "Connecting"; await CommunicationManager.Connection.StartAsync(); UpdateConnectionState(CommunicationManager.Connection); } var successful = false; try { var result = await CommunicationManager.Api.NewSession(_state.CampaignName, localContent.Content.TxtSession.Text); if (result) { _sessionData.SessionName = localContent.Content.TxtSession.Text; _sessionData.MapEntityAdded -= OnMapEntityAdded; _sessionData.MapEntityDeleted -= OnMapEntityDeleted; _sessionData.MapEntityAdded += OnMapEntityAdded; _sessionData.MapEntityDeleted += OnMapEntityDeleted; } successful = result; var result2 = await CommunicationManager.Connection?.InvokeAsync("JoinSession", localContent.Content.TxtSession.Text); _sessionData.SessionId = result2.SessionId; _sessionData.SessionName = localContent.Content.TxtSession.Text; } catch (Exception ex) { ExceptionlessClient.Default.SubmitException(ex); } if (successful) { _sessionData.SessionName = localContent.Content.TxtSession.Text; CommunicationManager.SessionData.Map = _sessionData.Map; CommunicationManager.SessionData.Overlays = _sessionData.Overlays; CommunicationManager.SessionData.Walls = _sessionData.Walls; lblSessionName.Text = _sessionData.SessionName; MenuConnectSync.Enabled = true; MenuConnectUpload.Enabled = true; localContent.Window.Close(); } } //TODO refactor private bool ValidateTextbox(TextBox textBox) { var valid = !string.IsNullOrWhiteSpace(textBox.Text); if (!valid) { textBox.Background = new SolidBrush(Color.Red); } else { textBox.Background = new SolidBrush(new Color(51, 51, 51)); ; } return valid; } //TODO Refactor private (Window Window, C Content) GetParentContentInWindow(Widget widget) where C : Widget { Container container = widget.Parent; while (!(container is Window)) { container = container.Parent; } var localWindow = (Window)container; var localContent = localWindow.Content as C; return (localWindow, localContent); } } }