using InABox.Clients; using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using ComboItemType = System.Tuple>; namespace InABox.DynamicGrid { public class DFLookupControl : DynamicFormFieldControl { private DFLayoutLookupValue _value = null!; private ComboBox Combo = null!; // late-initialising protected override FrameworkElement Create() { _value = new DFLayoutLookupValue(); Combo = new ComboBox(); if (CoreUtils.TryGetEntity(Field.Properties.LookupType, out var type)) { var client = ClientFactory.CreateClient(type); //Task.Run(() => //{ var columns = LookupFactory.DefineColumns(type); foreach(var property in Field.Properties.AdditionalPropertiesList) { columns.Add(property); } IFilter? filter; if (!Field.Properties.Filter.IsNullOrWhiteSpace()) { filter = Serialization.Deserialize(typeof(Filter<>).MakeGenericType(type), Field.Properties.Filter) as IFilter; filter?.ConvertCustomValues((_, value) => { var property = System.Text.Encoding.UTF8.GetString(value.Data); return FormDesignGrid.DataModel?.GetEntityValue(property); }); } else { filter = LookupFactory.DefineFilter(type); } var table = client.Query( filter, columns, LookupFactory.DefineSort(type) ); var lookups = new Dictionary() { { Guid.Empty, new("(None)", table.NewRow().ToDictionary(new[] { "ID" })) } }; foreach (var row in table.Rows) { var id = row.Get("ID"); var values = row.ToDictionary(new[] { "ID" }); var display = LookupFactory.FormatLookup(type, values, new string[] { }); lookups[id] = new(display, values); } //Dispatcher.Invoke(() => //{ Combo.DisplayMemberPath = "Value.Item1"; Combo.SelectedValuePath = "Key"; Combo.ItemsSource = lookups; Combo.VerticalContentAlignment = VerticalAlignment.Center; Combo.SelectionChanged += (sender, e) => ChangeField(); //}); //}); } return Combo; } public override void Deserialize(string serialized) { _value.Load(serialized); } public override string Serialize() { return _value.ToString(); } public override object? GetEntityValue() { return (Guid?)Combo.SelectedValue ?? Guid.Empty; } public override void SetEntityValue(object? value) { Combo.SelectedValue = value; } public override object? GetData(string dataField) { return ((KeyValuePair?)Combo.SelectedItem)?.Value.Item2.GetValueOrDefault(dataField); } public override Dictionary? GetAdditionalValues() { if (Field.Properties.AdditionalPropertiesList.Count == 0) return null; var values = new Dictionary(); foreach(var field in Field.Properties.AdditionalPropertiesList) { values[field] = GetData(field); } return values; } public override Guid GetValue() => ((KeyValuePair?)Combo.SelectedItem)?.Key ?? Guid.Empty; public override void SetValue(Guid value) { var lookups = Combo.ItemsSource as Dictionary; if (lookups is null) return; var lookup = lookups!.FirstOrDefault(x => x.Key == value); Combo.SelectedItem = lookup; } protected override bool IsEmpty() => Combo.SelectedValue == null || (Guid)Combo.SelectedValue == Guid.Empty; } }