using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Globalization; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; namespace bib_talk { public partial class MainWindow : Window { private DispatcherTimer _timer; public string loggedinUser; private bool isFirstLoad = true; public MainWindow() { InitializeComponent(); InitializeTimer(); LoadData(); } public MainWindow(string user) { loggedinUser = user; InitializeComponent(); InitializeTimer(); LoadData(); LoadMessages(); } private void InitializeTimer() { _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) }; _timer.Tick += (sender, e) => { LoadMessages(); LoadOnlineUsers(); }; _timer.Start(); } public void LoadData() { loggedinuser.Text = "Logged in as: " + loggedinUser; } private void CloseButton_Click(object sender, RoutedEventArgs e) { LogoutUser(); this.Close(); } private void DraggableArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.ButtonState == MouseButtonState.Pressed) { DragMove(); } } private async void LoadMessages() { using (var client = new HttpClient()) { var response = await client.GetAsync("http://daddypig.dns.navy:5114/api/messages/get"); if (response.IsSuccessStatusCode) { var responseContent = await response.Content.ReadAsStringAsync(); var messages = JsonConvert.DeserializeObject>(responseContent); chatlistbox.ItemsSource = messages; if (chatlistbox.Items.Count > 0) { var lastItem = chatlistbox.Items[chatlistbox.Items.Count - 1]; chatlistbox.ScrollIntoView(lastItem); } } } } private async void LogoutUser() { using (var client = new HttpClient()) { var logoutData = new { Username = loggedinUser, Password = "", Email = "", Birthday = "", IsOnline = "" }; var json = JsonConvert.SerializeObject(logoutData); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await client.PostAsync("http://daddypig.dns.navy:5114/api/users/logout", content); var responseContent = await response.Content.ReadAsStringAsync(); this.Close(); } } private async void LoadOnlineUsers() { using (var client = new HttpClient()) { var response = await client.GetAsync("http://daddypig.dns.navy:5114/api/users/online-users"); if (response.IsSuccessStatusCode) { var responseContent = await response.Content.ReadAsStringAsync(); var onlineUsers = JsonConvert.DeserializeObject>(responseContent); onlineUsersListBox.ItemsSource = onlineUsers; } } } private async void messageBOX_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { if (!string.IsNullOrEmpty(messageBOX.Text)) { var message = new MessageDto { Username = loggedinUser, Message = messageBOX.Text, Timestamp = DateTime.Now, }; using (var client = new HttpClient()) { var json = JsonConvert.SerializeObject(message); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await client.PostAsync("http://daddypig.dns.navy:5114/api/messages/send", content); if (response.IsSuccessStatusCode) { messageBOX.Clear(); LoadMessages(); // Reload messages after sending } } } } } private async void send_Click(object sender, RoutedEventArgs e) { if (!string.IsNullOrEmpty(messageBOX.Text)) { var message = new MessageDto { Username = loggedinUser, Message = messageBOX.Text, Timestamp = DateTime.Now, }; using (var client = new HttpClient()) { var json = JsonConvert.SerializeObject(message); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await client.PostAsync("http://daddypig.dns.navy:5114/api/messages/send", content); if (response.IsSuccessStatusCode) { messageBOX.Clear(); LoadMessages(); // Reload messages after sending } } } } public class Message { public int Id { get; set; } public string Username { get; set; } public string Text { get; set; } public DateTime Timestamp { get; set; } } public class MessageDto { public string Username { get; set; } public string Message { get; set; } public DateTime Timestamp { get; set; } } public class User { public int Id { get; set; } public string Username { get; set; } public string Password { get; set; } public string Email { get; set; } public string Birthday { get; set; } public string IsOnline { get; set; } } private void onlineUsersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { } } }