using System;
using System.Linq;
using System.Windows.Forms;
using TradeIdeas.TIProData.Interfaces;
namespace TradeIdeas.TIProGUI.UserNotifications
{
public partial class UserNotificationsControl : UserControl
{
private Form _parentForm;
private IConnectionMaster _connectionMaster;
private string _userName;
public UserNotificationsControl(Form parentForm, IConnectionMaster connectionMaster)
{
_parentForm = parentForm;
_connectionMaster = connectionMaster;
InitializeComponent();
// Load the notifications.
//LoadSampleNotifications(); // TEMPORARY
// Display notification count.
DisplayNotificationCount(0, 0, 0);
}
///
/// Display the notification count (unread/total).
///
public void DisplayNotificationCount(int readCount, int unreadCount, int acknowledgementRequiredCount)
{
//int unreadNotifications = UserNotifications.UserNotificationsQueue.Count(x => !x.Read);
//int totalNotifications = UserNotifications.UserNotificationsQueue.Count();
//lblNotificationCount.Text = $"Notifications: {unreadCount.ToString("N0")}/{(unreadCount + readCount).ToString("N0")}";
// Update icon.
if (acknowledgementRequiredCount > 0)
{
timer1.Start();
}
else
{
timer1.Stop();
if (unreadCount > 0)
{
picNewMessage.Visible = true;
picNoMessage.Visible = false;
}
else
{
picNewMessage.Visible = false;
picNoMessage.Visible = true;
}
}
// Update the User Notifications form if shown.
if (!UpdateUserNotificationsForm())
{
// User Notifications form is not shown.
// Check to see if there are any notifications requiring acknowledgement and show the form if there are.
if (acknowledgementRequiredCount > 0)
ShowUserNotificationsForm();
}
}
///
/// User Name.
///
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
///
/// Show User Notifications form.
///
private void ShowUserNotificationsForm()
{
using (UserNotificationsForm userNotificationsForm = new UserNotificationsForm(_parentForm, _connectionMaster))
{
userNotificationsForm.ShowDialog();
}
}
///
/// Returns true if the user notifications form is displayed and a reference to it in the output variable userNotificationsForm,
/// false otherwise and null in the output variable.
///
///
public static bool tryGetUserNotificationsForm(out UserNotificationsForm userNotificationsForm)
{
userNotificationsForm = Application.OpenForms.OfType().FirstOrDefault(x => !x.IsDisposed);
return userNotificationsForm != null;
}
///
/// Update the user notifications form if displayed.
///
/// True if User Notifications form is shown and refreshed. False otherwise.
public bool UpdateUserNotificationsForm()
{
// Find the form and refresh the notifications.
if (tryGetUserNotificationsForm(out UserNotificationsForm userNotificationsForm))
{
userNotificationsForm.RefreshNotifications();
return true;
}
else
return false;
}
//private void LoadSampleNotifications()
//{
// UserNotifications.UserNotificationsQueue.Clear();
// UserNotifications.AddUserNotification("1", DateTime.Now, "Normal Message 1", UserNotificationStatus.unread, UserNotificationType.notification, "", "http://localhost", false);
// UserNotifications.AddUserNotification("2", DateTime.Now, "Urgent Message 2", UserNotificationStatus.unread, UserNotificationType.notification, "", "https://trade-ideas.com", true);
// UserNotifications.AddUserNotification("3", DateTime.Now, "Urgent Message 3", UserNotificationStatus.unread, UserNotificationType.notification, "Urgent Message body 3
Trade Ideas", "", true);
//}
private void UserNotificationsControl_Click(object sender, EventArgs e)
{
ShowUserNotificationsForm();
}
private void UserNotificationsControl_Resize(object sender, EventArgs e)
{
// Center icons.
//int newLeft = (this.Width - picNoMessage.Width) / 2;
int newTop = (this.Height - picNoMessage.Height) / 2;
//picNoMessage.Left = newLeft;
picNoMessage.Top = newTop + 2;
//picNewMessage.Left = newLeft;
picNewMessage.Top = newTop + 2;
}
private void picNewMessage_Click(object sender, EventArgs e)
{
ShowUserNotificationsForm();
}
private void picNoMessage_Click(object sender, EventArgs e)
{
ShowUserNotificationsForm();
}
private void timer1_Tick(object sender, EventArgs e)
{
// Swap visibility of icons.
picNoMessage.Visible = !picNoMessage.Visible;
picNewMessage.Visible = !picNoMessage.Visible;
}
///
/// Update connectionMaster instance used when connectionMaster is updated via Global Settings.
///
///
public void UpdateConnectionMaster(IConnectionMaster connectionMaster)
{
_connectionMaster = connectionMaster;
}
}
}