using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Comal.Classes;
using InABox.Clients;
using InABox.Core;
using InABox.DynamicGrid;
using InABox.WPF;
using InABox.Wpf;
using System.ComponentModel;
using sun.net.www;
using Syncfusion.Windows.Controls.RichTextBoxAdv;
using static ICSharpCode.AvalonEdit.Document.TextDocumentWeakEventManager;
using SelectionChangedEventArgs = System.Windows.Controls.SelectionChangedEventArgs;
namespace PRSDesktop;
///
/// Interaction logic for NotificationPanel.xaml
///
public partial class NotificationPanel : UserControl, IPanel
{
private readonly Button Archive;
private readonly Button AttachToJob;
private readonly Button CreateDelivery;
private readonly Button CreateRequi;
private readonly Button CreateSetout;
private readonly Button CreateTask;
private readonly List> folders = new()
{
new("Inbox", PRSDesktop.Resources.download.AsBitmapImage()),
new("Sent", PRSDesktop.Resources.upload.AsBitmapImage()),
new("Archive", PRSDesktop.Resources.box.AsBitmapImage())
};
private readonly Button Forward;
private readonly Employee me = new Client().Load(new Filter(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid))
.FirstOrDefault();
private readonly DynamicDataGrid Notifications;
private readonly Button Reopen;
//Button WriteNew = null;
private readonly Button Reply;
private readonly Button ViewEntity;
private readonly Button ViewForm;
private readonly Button ViewJob;
public NotificationPanel()
{
InitializeComponent();
Notifications = new DynamicDataGrid();
Notifications.HiddenColumns.Add(x => x.Sender.ID);
Notifications.HiddenColumns.Add(x => x.Employee.ID);
Notifications.HiddenColumns.Add(x => x.Job.ID);
Notifications.HiddenColumns.Add(x => x.EntityType);
Notifications.HiddenColumns.Add(x => x.EntityID);
//Notifications.HiddenColumns.Add(x => x.Kanban.ID);
//Notifications.HiddenColumns.Add(x => x.Setout.ID);
//Notifications.HiddenColumns.Add(x => x.Requisition.ID);
//Notifications.HiddenColumns.Add(x => x.Delivery.ID);
//Notifications.HiddenColumns.Add(x => x.LeaveRequestLink.ID);
Notifications.HiddenColumns.Add(x => x.Closed);
Notifications.Reconfigure(options => options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.FilterRows, DynamicGridOption.SelectColumns, DynamicGridOption.MultiSelect));
Notifications.SetValue(Grid.RowProperty, 0);
Notifications.SetValue(Grid.ColumnProperty, 1);
Notifications.Margin = new Thickness(0);
Notifications.OnReload += Notifications_OnReload;
Notifications.OnSelectItem += Notifications_OnSelectItem;
Notifications.OnValidate += Notifications_OnValidate;
Notifications.OnCreateItem += Notifications_OnCreateItem;
//WriteNew = CreateButton("Write New", null, WriteNewClick);
//WriteNew.Visibility = Visibility.Visible;
//WriteNew.Margin = new Thickness(WriteNew.Margin.Left, WriteNew.Margin.Top, 10.0F, WriteNew.Margin.Bottom);
Reply = CreateButton("Reply", null, ReplyClick);
Forward = CreateButton("Forward", null, ForwardClick);
ViewForm = CreateButton("View Form", null, ViewFormClick);
ViewEntity = CreateButton("View Item", null, ViewEntityClick);
AttachToJob = CreateButton("Attach To Job", null, AttachToJobClick);
ViewJob = CreateButton("View Job", null, ViewJobClick);
CreateTask = CreateButton("Create Task", null, CreateTaskClick);
CreateSetout = CreateButton("Create Setout", null, CreateSetoutClick);
CreateRequi = CreateButton("Create Requi", null, CreateRequiClick);
CreateDelivery = CreateButton("Create Delivery", null, CreateDeliveryClick);
Archive = CreateButton("Archive", null, ArchiveClick);
Archive.Margin = new Thickness(10.0F, Archive.Margin.Top, Archive.Margin.Right, Archive.Margin.Bottom);
Reopen = CreateButton("Reopen", null, ReopenClick);
Reopen.Margin = new Thickness(10.0F, Reopen.Margin.Top, Reopen.Margin.Right, Reopen.Margin.Bottom);
Layout.Children.Add(Notifications);
Folders.ItemsSource = folders;
}
private void Notifications_OnCreateItem(object sender, object item)
{
if (item is not Notification notification)
return;
notification.Sender.ID = me.ID;
}
private void Notifications_OnValidate(object sender, Notification[] items, List errors)
{
foreach(var item in items)
{
if(!item.Sender.IsValid())
{
errors.Add("[Sender] may not be blank!");
}
if(!item.Employee.IsValid())
{
errors.Add("[Employee] may not be blank!");
}
}
}
public bool IsReady { get; set; }
public event DataModelUpdateEvent? OnUpdateDataModel;
private Button CreateButton(string caption, BitmapImage? umage, DynamicGridButtonClickEvent action)
{
var result = Notifications.AddButton(caption, umage, action);
result.Width = 100.0F;
result.Visibility = Visibility.Collapsed;
return result;
}
private void Notifications_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
{
var singlerow = e.Rows?.Length == 1;
//WriteNew.Visibility = (Folders.SelectedIndex == 0) ? Visibility.Visible : Visibility.Collapsed;
var row = e.Rows?.FirstOrDefault();
if (row != null)
{
var hasjob = row.IsEntityLinkValid(x => x.Job);
var entitytype = row.Get(x => x.EntityType);
NotificationUtils.MakeVisible(Reply, 0, 5, singlerow, Folders.SelectedIndex == 0 || Folders.SelectedIndex == 2);
NotificationUtils.MakeVisible(Forward, 5, 10, singlerow);
NotificationUtils.MakeVisible(ViewForm, 0, 5, singlerow, NotificationUtils.IsDigitalForm(entitytype));
NotificationUtils.MakeVisible(ViewEntity, 0, 10, singlerow, !string.IsNullOrWhiteSpace(entitytype));
ViewEntity.Content = string.Format("View {0}", NotificationUtils.GetEntityType(entitytype)?.GetCaption());
NotificationUtils.MakeVisible(AttachToJob, 0, 10, singlerow, !hasjob);
NotificationUtils.MakeVisible(ViewJob, 0, 10, singlerow, hasjob, !string.Equals(entitytype, typeof(Job).EntityName()));
var create = NotificationUtils.MakeVisible(CreateTask, 0, 5, singlerow, !string.Equals(entitytype, typeof(Kanban).EntityName()));
create = NotificationUtils.MakeVisible(CreateSetout, 0, 5, singlerow, hasjob, !string.Equals(entitytype, typeof(Setout).EntityName())) ||
create;
create =
NotificationUtils.MakeVisible(CreateRequi, 0, 5, singlerow, hasjob, !string.Equals(entitytype, typeof(Requisition).EntityName())) ||
create;
create = NotificationUtils.MakeVisible(CreateDelivery, 0, 5, singlerow, !string.Equals(entitytype, typeof(Delivery).EntityName())) ||
create;
NotificationUtils.MakeVisible(Archive, create ? 10 : 0, 5, Folders.SelectedIndex == 0);
NotificationUtils.MakeVisible(Reopen, create ? 10 : 0, 5, Folders.SelectedIndex == 2);
new Client().Query(
new Filter(x => x.ID).IsEqualTo(row.Get(x => x.ID)),
new Columns(x => x.Description),
null,
(table, error) =>
{
var drow = table?.Rows.FirstOrDefault();
var desc = drow?.Get(x => x.Description)?.Replace("background:NoColor;", "") ?? "";
var ms = new MemoryStream(Encoding.ASCII.GetBytes(desc));
Dispatcher.Invoke(() =>
{
Editor.Load(ms, FormatType.Html);
//cursor.Dispose();
});
}
);
}
else
{
NotificationUtils.MakeVisible(Reply, 0, 0, false);
NotificationUtils.MakeVisible(Forward, 0, 0, false);
NotificationUtils.MakeVisible(ViewForm, 0, 0, false);
NotificationUtils.MakeVisible(ViewEntity, 0, 0, false);
NotificationUtils.MakeVisible(AttachToJob, 0, 0, false);
NotificationUtils.MakeVisible(ViewJob, 0, 0, false);
NotificationUtils.MakeVisible(CreateTask, 0, 0, false);
NotificationUtils.MakeVisible(CreateSetout, 0, 0, false);
NotificationUtils.MakeVisible(CreateRequi, 0, 0, false);
NotificationUtils.MakeVisible(CreateDelivery, 0, 0, false);
NotificationUtils.MakeVisible(Archive, 0, 0, false);
NotificationUtils.MakeVisible(Reopen, 0, 0, false);
var ms = new MemoryStream(Encoding.ASCII.GetBytes(""));
Editor.Load(ms, FormatType.Html);
}
}
public void AddNotification(Notification notification)
{
Dispatcher.Invoke(() =>
{
if (IsInbox)
{
//Notifications.AddRow(notification);
Notifications.Refresh(false, true);
}
});
}
private bool IsInbox => Folders.SelectedIndex == 0;
private bool IsOutbox => Folders.SelectedIndex == 1;
private bool IsArchive => Folders.SelectedIndex == 2;
private void Notifications_OnReload(object sender, Filters criteria, Columns columns,
ref SortOrder? sortby)
{
var filter = new Filter(x => x.ID).IsEqualTo(Guid.Empty);
if (me != null)
{
if (IsInbox)
filter = new Filter(x => x.Employee.ID).IsEqualTo(me.ID).And(x => x.Closed).IsEqualTo(DateTime.MinValue);
else if (IsOutbox)
filter = new Filter(x => x.Sender.ID).IsEqualTo(me.ID);
else if (IsArchive)
filter = new Filter(x => x.Employee.ID).IsEqualTo(me.ID).And(x => x.Closed).IsNotEqualTo(DateTime.MinValue);
}
criteria.Add(filter);
sortby = new SortOrder(x => x.Created, SortDirection.Descending);
}
private void Folders_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (IsReady)
using (new WaitCursor())
{
Notifications.ColumnsTag = folders[Folders.SelectedIndex].Item1;
Notifications.Refresh(true, true);
}
}
#region IPanel stuff
public void CreateToolbarButtons(IPanelHost host)
{
// host.CreatePanelAction(new PanelAction() { Caption = "Send Notification", Image = PRSDesktop.Resources.email, OnExecute = SendNotificationClick });
}
public void Setup()
{
Folders.SelectedIndex = 0;
Notifications.ColumnsTag = folders[Folders.SelectedIndex].Item1;
Notifications.Refresh(true, false);
}
public void Shutdown(CancelEventArgs? cancel)
{
}
public void Refresh()
{
Notifications.Refresh(false, true);
}
public string SectionName => "Notifications";
public DataModel DataModel(Selection selection)
{
var ids = Notifications.ExtractValues(c => c.ID, selection).ToArray();
return new BaseDataModel(new Filter(x => x.ID).InList(ids));
}
public Dictionary DataEnvironment()
{
return new Dictionary
{
{ typeof(Notification), Notifications.Data }
};
}
public Dictionary Selected()
{
return new Dictionary();
}
#endregion
#region Actions
private Guid[] SelectedIDs => Notifications.ExtractValues(x => x.ID, Selection.Selected).ToArray();
private bool ViewFormClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.ViewForm(SelectedIDs);
}
private bool ViewEntityClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.ViewEntity(SelectedIDs);
}
private bool WriteNewClick(Button sender, CoreRow[] rows)
{
var form = new NotificationForm();
return form.ShowDialog() == true;
}
private bool ReplyClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.ReplyOrForward(SelectedIDs, "RE:", true, Folders.SelectedIndex == 1);
}
private bool ForwardClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.ReplyOrForward(SelectedIDs, "FW:", false, Folders.SelectedIndex == 1);
}
private bool CreateTaskClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.CreateTask(SelectedIDs, me.ID);
}
private bool AttachToJobClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.AttachToJob(SelectedIDs);
}
private bool ViewJobClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.ViewJob(SelectedIDs);
}
private bool CreateSetoutClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.CreateSetout(SelectedIDs);
}
private bool CreateRequiClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.CreateRequi(SelectedIDs, me.ID);
}
private bool CreateDeliveryClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.CreateDelivery(SelectedIDs);
}
private bool ArchiveClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.Archive(SelectedIDs);
}
private bool ReopenClick(Button sender, CoreRow[] rows)
{
return NotificationUtils.Reopen(SelectedIDs);
}
public void Heartbeat(TimeSpan time)
{
}
#endregion
}