using Comal.Classes;
using InABox.Clients;
using InABox.Core;
using InABox.DynamicGrid;
using InABox.WPF;
using Syncfusion.Data.Extensions;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Syncfusion.Pdf.Parsing;
using Encoder = System.Drawing.Imaging.Encoder;
using Image = System.Windows.Controls.Image;
using System.Reflection;
namespace PRSDesktop
{
///
/// Interaction logic for DataEntryPanel.xaml
///
///
/// This is a host because it has a singular code popup editor
///
public partial class DataEntryPanel : UserControl, IBasePanel, IDynamicEditorHost
{
IPopupEditorControl? Popup;
public DataEntryPanel()
{
InitializeComponent();
}
public void Setup()
{
var types = CoreUtils.Entities
.Where(x => x.IsAssignableTo(typeof(Entity))
&& x.HasInterface(typeof(IRemotable))
&& x.HasInterface(typeof(IPersistent))
&& x.GetCustomAttribute() is null
&& Security.CanEdit(x)
&& x.HasInterface())
.Select(x => new Tuple(x.Name, x)).OrderBy(x => x.Item1).ToList();
types.Insert(0, new("", null));
TypeBox.ItemsSource = types;
TypeBox.DisplayMemberPath = "Item1";
TypeBox.SelectedValuePath = "Item2";
ScanPanel.Setup();
}
public void Refresh()
{
ScanPanel.Refresh();
}
public bool IsReady { get; set; }
public string SectionName => "Data Entry";
public DataModel DataModel(Selection selection)
{
return new EmptyDataModel();
}
public event DataModelUpdateEvent? OnUpdateDataModel;
public void CreateToolbarButtons(IPanelHost host)
{
if (Security.IsAllowed())
{
host.CreateSetupAction(new PanelAction
{
Caption = "Scan Tags",
OnExecute = (action) =>
{
var list = new MasterList(typeof(ScanTag));
list.ShowDialog();
}
});
}
}
public void Heartbeat(TimeSpan time)
{
}
public Dictionary Selected()
{
return new Dictionary();
}
public void Shutdown()
{
ScanPanel.Shutdown();
}
#region Host
private HashSet _loadedTypes = new();
public DynamicGridColumns Columns { get; set; } = new();
IEnumerable IDynamicEditorHost.Columns => Columns;
public void LoadColumns(string column, Dictionary columns)
{
var selectedType = TypeBox.SelectedValue as Type;
if (selectedType is null) return;
columns.Clear();
foreach (var c in LookupFactory.DefineColumns(selectedType).ColumnNames().Where(x => x != "ID"))
{
columns.Add(c, c);
}
if (Popup?.EditorDefinition is CodePopupEditorControl codePopup && !columns.ContainsKey(codePopup.CodeColumn))
{
columns.Add(codePopup.CodeColumn, codePopup.CodeColumn);
}
}
public IFilter? DefineFilter(Type type) => LookupFactory.DefineFilter(type);
public void LoadLookups(ILookupEditorControl sender)
{
var editor = sender.EditorDefinition as ILookupEditor;
var colname = sender.ColumnName;
var values = editor.Values(colname, Editor.Items);
sender.LoadLookups(values);
}
public Document? FindDocument(string filename) => null;
public Document? GetDocument(Guid id) => null;
public void SaveDocument(Document document) { }
object?[] IDynamicEditorHost.GetItems() => Editor.Items;
public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor();
#endregion
private void ClearEditor(Type TEntity)
{
UpdateEditor(TEntity, null);
if(Popup is not null)
{
Popup.Value = Guid.Empty;
}
}
private bool EditorChanged = false;
private IDynamicDataGrid UpdateEditor(Type TEntity, BaseObject[]? items)
{
DetailGrid.Children.Remove(Editor);
Editor = new EmbeddedDynamicEditorForm();
Editor.SetLayoutType();
Editor.SetValue(Grid.RowProperty, 1);
Editor.SetValue(Grid.ColumnProperty, 0);
Editor.SetValue(Grid.ColumnSpanProperty, 4);
EditorChanged = false;
Editor.OnAfterEditorValueChanged += (sender, column) =>
{
EditorChanged = true;
return null;
};
Editor.OnOK += () =>
{
var cancel = new System.ComponentModel.CancelEventArgs();
Editor.SaveItem(cancel);
if(!cancel.Cancel)
ClearEditor(TEntity);
};
Editor.OnCancel += () =>
{
ClearEditor(TEntity);
};
DetailGrid.Children.Add(Editor);
var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), TEntity);
grid.InitialiseEditorForm(Editor, items ?? new object[] { Activator.CreateInstance(TEntity)! });
return (grid as IDynamicDataGrid)!;
}
private void TypeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedType = TypeBox.SelectedValue as Type;
if (selectedType is not null)
{
var dataGrid = UpdateEditor(selectedType, null);
var editorColumns = dataGrid.LoadEditorColumns();
if (Popup is UIElement element)
DetailGrid.Children.Remove(element);
var code = DatabaseSchema.Properties(selectedType)
.Where(x => x.Parent is null && (x.Editor is CodeEditor || x.Editor is UniqueCodeEditor)
&& x.Editor.Editable != Editable.Hidden)
.FirstOrDefault();
BaseEditor editor;
if (code is not null)
{
editor = new CodePopupEditor(selectedType) { CodeColumn = code.Name };
Popup = new CodePopupEditorControl { Margin = new Thickness(5), CodeColumn = code.Name };
}
else
{
editor = new PopupEditor(selectedType);
Popup = new PopupEditorControl { Margin = new Thickness(5) };
}
if (Popup is IPopupEditorControl popupEditor)
{
Popup.ColumnName = "ID";
popupEditor.Host = this;
Columns = new DynamicGridColumns();
Columns.ExtractColumns(selectedType);
}
var el = (Popup as UIElement)!;
el.SetValue(Grid.RowProperty, 0);
el.SetValue(Grid.ColumnProperty, 3);
Popup.EditorDefinition = editor;
Popup.OnEditorValueChanged += (s, v) =>
{
var entityID = (Guid)v["ID"];
BaseObject[]? objs = null;
if (entityID != Guid.Empty)
{
var obj = Client.Create(selectedType)
.Query(
Filter.Create(selectedType, x => x.ID).IsEqualTo(entityID),
editorColumns)
.ToObjects(selectedType)
.FirstOrDefault();
if (obj is not null)
{
objs = new BaseObject[] { obj };
}
}
UpdateEditor(selectedType, objs);
};
Popup.Configure();
Popup.Loaded = true;
DetailGrid.Children.Add(el);
}
else
{
DetailGrid.Children.Remove(Editor);
if (Popup is UIElement el)
DetailGrid.Children.Remove(el);
}
}
private void ScanPanel_OnSelectAppliesTo(string appliesTo)
{
if(CoreUtils.TryGetEntity(appliesTo, out var entity))
{
TypeBox.SelectedValue = entity;
}
}
}
}