using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Comal.Classes;
using InABox.Clients;
using InABox.Core;
using InABox.DynamicGrid;
using InABox.WPF;
using Image = System.Windows.Controls.Image;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace PRSDesktop
{
public delegate void NotificationsChangedEvent(object sender);
///
/// Interaction logic for NotificationsDock.xaml
///
public partial class NotificationsDock : UserControl
{
public NotificationsDock()
{
InitializeComponent();
NotificationsList.ItemsSource = Notifications;
}
private ObservableCollection Notifications = new();
public bool IsActive => Notifications.Any();
public event NotificationsChangedEvent Changed;
public void AddNotification(Notification notification)
{
Dispatcher.Invoke(() =>
{
if (!Notifications.Any(x => x.ID == notification.ID))
{
Notifications.Add(notification);
}
MessageHeader.Content = string.Format("{0} Notification{1}", Notifications.Count, Notifications.Count == 1 ? "" : "s");
Changed?.Invoke(this);
});
}
public void Refresh()
{
new Client().Query(
new Filter(x => x.Employee.ID).IsEqualTo(App.EmployeeID).And(x => x.Closed)
.IsEqualTo(DateTime.MinValue),
new Columns(
x => x.ID,
x => x.Title,
//x => x.Description,
x => x.Created,
x => x.Sender.ID,
x => x.Sender.Name,
x => x.Job.ID,
x => x.Job.Deleted,
x => x.Job.JobNumber,
//x => x.Kanban.ID,
//x => x.Setout.ID,
//x => x.Requisition.ID,
//x => x.Delivery.ID,
x => x.Employee.ID,
x => x.EntityType,
x => x.EntityID,
x => x.Closed
),
null, //new SortOrder(x => x.Created, InABox.Core.SortDirection.Descending),
(n, ex) =>
{
Dispatcher.Invoke(() =>
{
if (n != null)
{
MessageHeader.Content = string.Format("{0} Notification{1}", n.Rows.Count, n.Rows.Count == 1 ? "" : "s");
Notifications.Clear();
foreach(var notification in n.Rows.Select(r => r.ToObject()))
{
Notifications.Add(notification);
}
Changed?.Invoke(this);
}
else
{
Changed?.Invoke(this);
}
});
}
);
}
private void Notification_Toolbar_Loaded(object sender, RoutedEventArgs e)
{
var toolBar = sender as ToolBar;
var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement;
if (overflowGrid != null)
overflowGrid.Visibility = Visibility.Collapsed;
var mainPanelBorder = toolBar.Template.FindName("MainPanelBorder", toolBar) as FrameworkElement;
if (mainPanelBorder != null)
mainPanelBorder.Margin = new Thickness();
}
private void LoadButton(ToolBar toolbar, Notification notification, int index, bool visible, Bitmap image, string tooltip)
{
var btn = toolbar.Items[index] as Button;
btn.Tag = notification;
btn.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
var img = btn.Content as Image;
img.Source = image.AsBitmapImage();
btn.ToolTip = tooltip;
}
private void Expander_Expanded(object sender, RoutedEventArgs e)
{
var expander = sender as Expander;
var notification = expander.Tag as Notification;
var stack = (expander.Content as Border).Child as StackPanel;
var isJob = notification.Job.IsValid();
var isForm = NotificationUtils.IsDigitalForm(notification.EntityType);
var entitytype = NotificationUtils.GetEntityType(notification.EntityType);
var toolbar = stack.Children[0] as ToolBar;
LoadButton(toolbar, notification, 0, true, PRSDesktop.Resources.delete, "Dismiss");
LoadButton(toolbar, notification, 1, true, PRSDesktop.Resources.reply, "Reply");
LoadButton(toolbar, notification, 2, true, PRSDesktop.Resources.forward, "Forward");
LoadButton(toolbar, notification, 3, isForm, PRSDesktop.Resources.checklist, "View Form");
LoadButton(toolbar, notification, 4, entitytype != null, PRSDesktop.Resources.pencil,
string.Format("View {0}", entitytype?.GetCaption()));
LoadButton(toolbar, notification, 5, true, PRSDesktop.Resources.project, string.Format("{0} {1}", isJob ? "View" : "Attach To", "Job"));
LoadButton(toolbar, notification, 6, entitytype != typeof(Kanban), PRSDesktop.Resources.kanban, "Create Task");
LoadButton(toolbar, notification, 7, entitytype != typeof(Delivery), PRSDesktop.Resources.truck, "Create Delivery");
LoadButton(toolbar, notification, 8, entitytype != typeof(Requisition), PRSDesktop.Resources.requisition, "Create Requisition");
LoadButton(toolbar, notification, 9, entitytype != typeof(Setout), PRSDesktop.Resources.factory, "Create Setout");
var editor = stack.Children[1] as ExtendedRichTextEditor;
new Client().Query(
new Filter(x => x.ID).IsEqualTo(notification.ID),
new Columns(x => x.Description),
null,
(table, error) =>
{
var drow = table != null ? table.Rows.FirstOrDefault() : null;
var desc = drow?.Get(x => x.Description)?.Replace("background:NoColor;", "") ?? "";
var ms = new MemoryStream(Encoding.ASCII.GetBytes(desc));
Dispatcher.Invoke(() =>
{
notification.Description = desc;
editor.Text = desc;
});
}
);
editor.Height = 200;
ScrollViewer scroller = stack.Children.OfType().FirstOrDefault();
if (scroller == null)
PreviewPhotos(notification, stack);
}
private void PreviewPhotos(Notification notification, StackPanel stack)
{
Task.Run(() =>
{
CoreTable table = new Client().Query
(
new Filter(x => x.EntityLink.ID).IsEqualTo(notification.ID),
new Columns(x => x.DocumentLink.ID, x => x.ID)
);
if (table.Rows.Any())
{
Dispatcher.Invoke(() =>
{
List images = new List();
foreach (CoreRow row in table.Rows)
{
CoreTable table1 = new Client().Query
(
new Filter(x => x.ID).IsEqualTo(Guid.Parse(row.Values[0].ToString())),
new Columns(x => x.FileName, x => x.Data, x => x.ID)
);
Image img = DataToImage((table1.Rows.FirstOrDefault().Values[1]) as byte[], table1.Rows.FirstOrDefault().Values[0].ToString());
img.Tag = row.Values[1].ToString();
images.Add(img);
}
ScrollViewer scroller = new ScrollViewer() { VerticalScrollBarVisibility = ScrollBarVisibility.Disabled, HorizontalScrollBarVisibility = ScrollBarVisibility.Visible };
StackPanel photoPanel = new StackPanel();
photoPanel.Height = 50;
photoPanel.Orientation = Orientation.Horizontal;
foreach (Image img in images)
{
photoPanel.Children.Add(img);
}
scroller.Content = photoPanel;
stack.Children.Add(scroller);
});
}
});
}
private void Notification_ReplyButton_Click(object sender, RoutedEventArgs e)
{
var notification = (sender as Button).Tag as Notification;
if (NotificationUtils.ReplyOrForward(new[] { notification.ID }, "RE: ", true, false))
Refresh();
}
private void Notification_ForwardButton_Click(object sender, RoutedEventArgs e)
{
var notification = (sender as Button).Tag as Notification;
if (NotificationUtils.ReplyOrForward(new[] { notification.ID }, "FW: ", false, false))
Refresh();
}
private void Notification_FormButton_Click(object sender, RoutedEventArgs e)
{
var notification = (sender as Button).Tag as Notification;
NotificationUtils.ViewForm(new[] { notification.ID });
}
private void Notification_EntityButton_Click(object sender, RoutedEventArgs e)
{
var notification = (sender as Button).Tag as Notification;
if (NotificationUtils.ViewEntity(new[] { notification.ID }))
Refresh();
}
private void Notification_JobButton_Click(object sender, RoutedEventArgs e)
{
var notification = (sender as Button).Tag as Notification;
if (notification.Job.IsValid())
{
NotificationUtils.ViewJob(new[] { notification.Job.ID });
}
else
{
if (NotificationUtils.AttachToJob(new[] { notification.ID }))
Refresh();
}
}
private void Notification_TaskButton_Click(object sender, RoutedEventArgs e)
{
var notification = (sender as Button).Tag as Notification;
if (NotificationUtils.CreateTask(new[] { notification.ID }, App.EmployeeID))
Refresh();
}
private void Notification_DeliveryButton_Click(object sender, RoutedEventArgs e)
{
var notification = (sender as Button).Tag as Notification;
if (NotificationUtils.CreateDelivery(new[] { notification.ID }))
Refresh();
}
private void Notification_RequisitionButton_Click(object sender, RoutedEventArgs e)
{
var notification = (sender as Button).Tag as Notification;
if (NotificationUtils.CreateRequi(new[] { notification.ID }, App.EmployeeID))
Refresh();
}
private void Notification_SetoutButton_Click(object sender, RoutedEventArgs e)
{
var notification = (sender as Button).Tag as Notification;
if (NotificationUtils.CreateSetout(new[] { notification.ID }))
Refresh();
}
private void Notification_DismissButton_Click(object sender, RoutedEventArgs e)
{
var notification = (sender as Button).Tag as Notification;
if (NotificationUtils.Archive(new[] { notification.ID }))
{
var list = NotificationsList.ItemsSource as ObservableCollection;
if (list.Contains(notification))
list.Remove(notification);
MessageHeader.Content = string.Format("{0} Notification{1}", list.Count, list.Count == 1 ? "" : "s");
Changed?.Invoke(this);
}
}
private Image DataToImage(byte[] data, string filename)
{
Image img = new Image();
img.Height = 50;
img.Width = 50;
img.Margin = new Thickness(5);
img.MouseUp += Img_MouseUp;
if (filename.EndsWith("png") || filename.EndsWith("jpg") || filename.EndsWith("jpeg") || filename.EndsWith("bmp"))
{
img.Source = ImageUtils.LoadImage(data);
}
else if (filename.EndsWith("pdf"))
{
img.Source = PRSDesktop.Resources.doc_pdf.AsBitmapImage();
}
return img;
}
private void Img_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Image img = sender as Image;
string str = img.Tag as string;
Guid id = Guid.Parse(str);
CoreTable table = new Client().Query(new Filter(x => x.ID).IsEqualTo(id),
new Columns(x => x.ID, x => x.DocumentLink.ID, x => x.DocumentLink.FileName));
if (table.Rows.Any())
{
IEntityDocument doc = table.Rows.First().ToObject();
IEntityDocument[] docs = new IEntityDocument[] { doc };
DocumentEditor editor = new DocumentEditor(docs);
editor.Show();
}
}
}
}