using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Linq.Expressions; using System.Runtime.CompilerServices; using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Imaging; using AutoProperties; using Comal.Classes; using InABox.Core; using InABox.DynamicGrid; using InABox.Integration.Logikal; using InABox.WPF; using PRSDesktop.Integrations.Logikal; namespace PRSDesktop.Integrations.Common; public class IntegrationBOMWindowViewModel : DependencyObject { private ILogikalPartsResponse _bom; public static DependencyProperty BOMProperty = DependencyProperty.Register( nameof(BOM), typeof(ILogikalPartsResponse), typeof(IntegrationBOMWindowViewModel), new FrameworkPropertyMetadata(BOMChanged)); private static void BOMChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not IntegrationBOMWindowViewModel model) return; var bom = e.NewValue as ILogikalPartsResponse; ExtractMappings( bom?.Finishes, x => x.Code, x => x.Description, x => x.Code, s => model.Styles = s); ExtractMappings( bom?.Profiles, x => x.Code, x => x.Description, x => x.Code, p => model.Profiles = p); ExtractMappings( bom?.Components, x => x.Code, x => x.Description, x => x.Code, c => model.Components = c); ExtractMappings( bom?.Glass, x => x.Code, x => x.Description, x => x.Code, g => model.Glass = g); ExtractMappings( bom?.Labour, x => x.Code, x => x.Description, x => x.Code, l => model.Labour = l); } public ILogikalPartsResponse? BOM { get => GetValue(BOMProperty) as ILogikalPartsResponse; set => SetValue(BOMProperty, value); } private static readonly DependencyProperty StylesProperty = DependencyProperty.Register( nameof(Styles), typeof(List), typeof(IntegrationBOMWindowViewModel) ); public List? Styles { get => GetValue(StylesProperty) as List; set => SetValue(StylesProperty, value); } private static readonly DependencyProperty ProfilesProperty = DependencyProperty.Register( nameof(Profiles), typeof(List), typeof(IntegrationBOMWindowViewModel) ); public List? Profiles { get => GetValue(ProfilesProperty) as List; set => SetValue(ProfilesProperty, value); } private static readonly DependencyProperty ComponentsProperty = DependencyProperty.Register( nameof(Components), typeof(List), typeof(IntegrationBOMWindowViewModel) ); public List? Components { get => GetValue(ComponentsProperty) as List; set => SetValue(ComponentsProperty, value); } private static readonly DependencyProperty GlassProperty = DependencyProperty.Register( nameof(Glass), typeof(List), typeof(IntegrationBOMWindowViewModel) ); public List? Glass { get => GetValue(GlassProperty) as List; set => SetValue(GlassProperty, value); } private static readonly DependencyProperty LabourProperty = DependencyProperty.Register( nameof(Labour), typeof(List), typeof(IntegrationBOMWindowViewModel) ); public List? Labour { get => GetValue(LabourProperty) as List; set => SetValue(LabourProperty, value); } private static readonly DependencyProperty SectionsProperty = DependencyProperty.Register( nameof(Sections), typeof(Dictionary), typeof(IntegrationBOMWindowViewModel) ); private Dictionary _sections = new() { { nameof(Styles), PRSDesktop.Resources.palette.AsBitmapImage(64, 64) }, { nameof(Profiles), PRSDesktop.Resources.profile.AsBitmapImage(64, 64) }, { nameof(Components), PRSDesktop.Resources.fixings.AsBitmapImage(64, 64) }, { nameof(Glass), PRSDesktop.Resources.glass.AsBitmapImage(64, 64) }, { nameof(Labour), PRSDesktop.Resources.quality.AsBitmapImage(64, 64) }, }; public Dictionary Sections { get => (Dictionary)GetValue(SectionsProperty); set => SetValue(SectionsProperty, value); } private static readonly DependencyProperty SelectedSectionProperty = DependencyProperty.Register( nameof(SelectedSection), typeof(KeyValuePair), typeof(IntegrationBOMWindowViewModel) ); public KeyValuePair SelectedSection { get => (KeyValuePair)GetValue(SelectedSectionProperty); set => SetValue(SelectedSectionProperty, value); } public IntegrationBOMWindowViewModel() { Sections = _sections; SelectedSection = Sections.First(); } private static void ExtractMappings( IEnumerable? items, Func logikalcode, Func logikaldescription, Expression> entitycode, Action> update) where TCode : BaseIntegrationCode, new() where TEntity : Entity, IRemotable,IPersistent, new() where TLink :EntityLink where TMapping : BaseIntegrationSource, IRemotable, IPersistent, new() { Task.Run>(() => { var f = entitycode.Compile(); var results = new List(); if (items == null) return results; var sourceitems = new Dictionary( items.Select(x => new KeyValuePair(logikalcode(x) ?? "", logikaldescription(x) ?? "") ).Distinct() ); MultiQuery query = new(); query.Add( new Filter(entitycode).InList(sourceitems.Keys.ToArray()), Columns.Required().Add(x => x.ID).Add(entitycode) ); query.Add( new Filter(x => x.Code).InList(sourceitems.Keys.ToArray()), Columns.Required().Add(x => x.ID).Add(x => x.Code) ); query.Query(); var mappings = query.Get().ToDictionary(x=>x.Code ?? "",x=>x.ID); var entities = query.Get().ToDictionary(entitycode, x=>x.ID); foreach (var sourceitem in sourceitems) { var result = new TCode() { Code = sourceitem.Key, Description = sourceitem.Value }; if (mappings.ContainsKey(sourceitem.Key)) { result.Entity.ID = mappings[sourceitem.Key]; CoreUtils.SetPropertyValue(result.Entity,CoreUtils.GetFullPropertyName(entitycode,"."),sourceitem.Key); } else if (entities.ContainsKey(sourceitem.Key)) { result.Entity.ID = entities[sourceitem.Key]; CoreUtils.SetPropertyValue(result.Entity,CoreUtils.GetFullPropertyName(entitycode,"."),sourceitem.Key); } results.Add(result); } return results.ToList(); } ).ContinueWith( (task) => update?.Invoke(task.Result), TaskScheduler.FromCurrentSynchronizationContext() ); } }