using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Clients; using InABox.Configuration; using InABox.Core; using InABox.WPF; using NPOI.HSSF.Util; using Syncfusion.Windows.Tools.Controls; namespace PRSDesktop { public class EquipmentSelectorData { // if first == FullGuid, remainder are selected // Otherwise, ignore the rest public Guid[] Groups { get; set; } // if first == FullGuid, select all public Guid[] Equipment { get; set; } public EquipmentSelectorData( Guid[] groups, Guid[] equipment) { Groups = groups; Equipment = equipment; } public EquipmentSelectorData() : this(new[] { Guid.Empty }, new[] { CoreUtils.FullGuid }) { } } public class EquipmentSelectorSettings { public double SplitPosition { get; set; } public EquipmentSelectorSettings() { SplitPosition = 200.0F; } } public class EquipmentSelectorSettingsChangedArgs : EventArgs { public EquipmentSelectorSettings Settings { get; private set; } public EquipmentSelectorSettingsChangedArgs(EquipmentSelectorSettings settings) { Settings = settings; } } public delegate void EquipmentSelectorSettingsChanged(object sender, EquipmentSelectorSettingsChangedArgs args); public class EquipmentSelectorSelectionChangedArgs : EventArgs { public EquipmentSelectorData Selection { get; private set; } public EquipmentSelectorSelectionChangedArgs(EquipmentSelectorData selection) { Selection = selection; } } public delegate void EquipmentSelectorSelectionChanged(object sender, EquipmentSelectorSelectionChangedArgs args); public partial class EquipmentSelector : UserControl { private enum Suppress { This } private class ListEntry { public Guid Key { get; set; } public String Value { get; set; } public BitmapImage Image { get; set; } public ListEntry(Guid key, String value, BitmapImage image = null) { Key = key; Value = value; Image = image; } } public EquipmentSelectorSettings Settings { get; set; } public event EquipmentSelectorSettingsChanged SettingsChanged; public event EquipmentSelectorSelectionChanged SelectionChanged; private CoreTable _equipment; private CoreTable _activities; private ListEntry[] _groups; private Tuple[] _equipmentgroups; public T[] GetEquipmentData(Func transform) { List result = new List(); var selgrps = GetSelectedGroups(); var seleq = GetSelectedEquipment(); var eqids = _equipmentgroups .Where(x => selgrps.Contains(x.Item1) && seleq.Contains(CoreUtils.FullGuid) || seleq.Contains(x.Item2)) .Select(x => x.Item2) .Distinct() .ToArray(); var rows = _equipment.Rows.Where(r => eqids.Contains(r.Get(c => c.ID))); foreach (var row in rows) { var activities = _activities.Rows .Where(r => r.Get(c => c.Equipment.ID) == row.Get(c => c.ID)) .Select(r=>r.ToObject()) .ToArray(); result.Add( transform.Invoke( row, _groups.FirstOrDefault(x => x.Key == row.Get(c => c.GroupLink.ID))?.Image, activities ) ); } return result.ToArray(); } public EquipmentSelector() { Settings = new EquipmentSelectorSettings(); using (new EventSuppressor(Suppress.This)) InitializeComponent(); } public void Setup() { using (new EventSuppressor(Suppress.This)) { var results = Client.QueryMultiple( new KeyedQueryDef(LookupFactory.DefineFilter()), new KeyedQueryDef(), new KeyedQueryDef(LookupFactory.DefineFilter()) ); _equipment = results.Get(); _activities = results.Get(); var groups = results.Get(); _groups = groups.Rows.Select(r => new ListEntry( r.Get(c=>c.ID), r.Get(c=>c.Name) )).ToArray(); GroupList.ItemsSource = _groups; var ids = groups.Rows.Select(r => r.Get(c=>c.Thumbnail.ID)).Distinct().Where(x => x != Guid.Empty).ToArray(); var Images = ids.Any() ? new Client().Load(new Filter(x=>x.ID).InList(ids)) : new Document[] { }; foreach (var row in groups.Rows) { var image = Images.FirstOrDefault(x => x.ID.Equals(row.Get(c=>c.Thumbnail.ID))); var grp = _groups.FirstOrDefault(x => x.Key == row.Get(c => c.Thumbnail.ID)); if (grp != null) grp.Image = ImageUtils.LoadImage(image.Data) ?? PRSDesktop.Resources.truck.AsBitmapImage(); } ObservableCollection groupdata = new() { new ListEntry(Guid.Empty, "All Equipment", PRSDesktop.Resources.truck.AsBitmapImage()) }; foreach (var group in _groups) groupdata.Add(group); groupdata.Add(new ListEntry(CoreUtils.FullGuid, "Multiple Groups", PRSDesktop.Resources.truck.AsBitmapImage())); Groups.ItemsSource = groupdata; _equipmentgroups = _equipment.Rows.Select( row => new Tuple( row.Get(c => c.GroupLink.ID), row.Get(c => c.ID), row.Get(c => c.Description), _groups.FirstOrDefault(x=>x.Key == row.Get(c => c.GroupLink.ID))?.Image ) ).Union( _equipment.Rows.Select( row => new Tuple( Guid.Empty, row.Get(c => c.ID), row.Get(c => c.Description), PRSDesktop.Resources.truck.AsBitmapImage() ) ) ).ToArray(); Groups.SelectedValue = Guid.Empty; } } private void UpdateSettings() { bool changed = false; var groups = GetSelectedGroups(); if (groups.Contains(CoreUtils.FullGuid) && (Math.Abs(Settings.SplitPosition - GroupListRow.Height.Value) >= 1.0F)) { changed = true; Settings.SplitPosition = GroupListRow.Height.Value; } if (changed && (!EventSuppressor.IsSet(Suppress.This))) SettingsChanged?.Invoke(this, new EquipmentSelectorSettingsChangedArgs(Settings)); } private Guid[] GetSelectedGroups() { Guid groupid = Groups.SelectedValue != null ? (Guid)Groups.SelectedValue : Guid.Empty; var teams = (groupid == CoreUtils.FullGuid) ? GroupList.SelectedItems .OfType() .Select(x => x.Key) .Prepend(groupid) .ToArray() : new Guid[] { groupid }; return teams; } private Guid[] GetSelectedEquipment() { var emps = (SelectedEquipment.SelectedItems.Count == SelectedEquipment.Items.Count) ? new Guid[] { CoreUtils.FullGuid } : SelectedEquipment.SelectedItems .OfType() .Select(x => x.Key) .ToArray(); return emps; } public EquipmentSelectorData Selection { get => GetSelectionData(); set => SelectEquipment(value); } private EquipmentSelectorData GetSelectionData() { return new EquipmentSelectorData( GetSelectedGroups(), GetSelectedEquipment() ); } private void SelectEquipment(EquipmentSelectorData selection) { using (new EventSuppressor(Suppress.This)) { Groups.SelectedValue = selection.Groups.Contains(CoreUtils.FullGuid) ? CoreUtils.FullGuid : selection.Groups.FirstOrDefault(); LoadGroups(selection.Groups); var equipment = SelectedEquipment.ItemsSource as ObservableCollection; var pairs = equipment.Where(x => selection.Equipment.Contains(CoreUtils.FullGuid) || selection.Equipment.Contains(x.Key)); SelectedEquipment.SelectedItems.Clear(); foreach (var pair in pairs) SelectedEquipment.SelectedItems.Add(pair); } } private void LoadGroups(Guid[] groupids) { using (new EventSuppressor(Suppress.This)) { Guid groupid = groupids.FirstOrDefault(); if (Guid.Equals(groupid, CoreUtils.FullGuid)) { GroupListRow.Height = new GridLength(Settings.SplitPosition, GridUnitType.Pixel); GroupListSplitterRow.Height = new GridLength(4, GridUnitType.Pixel); var selected = new ObservableCollection(_groups.Where(x => groupids.Contains(x.Key))); GroupList.SelectedItems = selected; LoadEquipment(groupids.Skip(1).ToArray()); } else { GroupListRow.Height = new GridLength(0, GridUnitType.Pixel); GroupListSplitterRow.Height = new GridLength(0, GridUnitType.Pixel); LoadEquipment(new Guid[] { groupid }); } } } private void LoadEquipment(Guid[] teamids) { using (new EventSuppressor(Suppress.This)) { var eqs =_equipmentgroups .OrderBy(x => x.Item3) .Where(x => teamids.Contains(x.Item1)) .Select(x => new ListEntry(x.Item2, x.Item3)) .Distinct() .ToArray(); SelectedEquipment.ItemsSource = new ObservableCollection(eqs); SelectedEquipment.SelectedItems = new ObservableCollection(eqs); } } public void SelectEmployee(Guid employeeid) { using (new EventSuppressor(Suppress.This)) { LoadGroups(new Guid[] { Guid.Empty }); if (SelectedEquipment.ItemsSource is ObservableCollection employees) { var emp = employees.FirstOrDefault(x => x.Key == employeeid); if (emp != null) SelectedEquipment.SelectedItems = new ObservableCollection() { emp }; } } } private void TeamsSelectionChanged(object sender, SelectionChangedEventArgs e) { if (EventSuppressor.IsSet(Suppress.This)) return; using (new EventSuppressor(Suppress.This)) LoadGroups(GetSelectedGroups()); UpdateSettings(); SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData())); } private void SelectedGroups_ItemChecked(object? sender, ItemCheckedEventArgs e) { if (EventSuppressor.IsSet(Suppress.This)) return; using (new EventSuppressor(Suppress.This)) LoadEquipment(GetSelectedGroups()); UpdateSettings(); SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData())); } private void SelectedEquipment_OnItemChecked(object? sender, ItemCheckedEventArgs e) { if (EventSuppressor.IsSet(Suppress.This)) return; UpdateSettings(); SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData())); } private void SelectedGroups_SizeChanged(object sender, SizeChangedEventArgs e) { if (EventSuppressor.IsSet(Suppress.This)) return; UpdateSettings(); } } }