using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Core; using InABox.DynamicGrid; using InABox.Wpf; using InABox.WPF; namespace PRSDesktop.Grids; public class ProductDimensionUnitGrid : DynamicDataGrid { private static readonly BitmapImage CONVERT = PRSDesktop.Resources.specifications.AsBitmapImage(); protected override void Init() { base.Init(); HiddenColumns.Add(x => x.Code); HiddenColumns.Add(x => x.HasQuantity); HiddenColumns.Add(x => x.HasLength); HiddenColumns.Add(x => x.HasWeight); HiddenColumns.Add(x => x.HasWidth); HiddenColumns.Add(x => x.HasHeight); HiddenColumns.Add(x =>x.Conversion); ActionColumns.Add(new DynamicImageColumn(ConvertImage) { Position = DynamicActionColumnPosition.End}); AddButton("Update Expressions", null, (button, rows) => { UpdateExpressions(rows.ToArray()); return false; }); } private BitmapImage? ConvertImage(CoreRow? row) { return row == null ? CONVERT : String.IsNullOrWhiteSpace(row.Get(x => x.Conversion)) ? null : CONVERT; } protected override void DoValidate(ProductDimensionUnit[] items, List errors) { base.DoValidate(items, errors); foreach (var item in items) item.Validate(errors); } private void UpdateExpressions(ProductDimensionUnit[] items) { Dictionary? results = null; Exception? exception = null; Progress.ShowModal("Updating Dimensions", progress => { try { results = DimensionUnitUtils.UpdateExpressions(items, progress); } catch(Exception e) { exception = e; } }); if(results is not null) { MessageWindow.ShowMessage($"Update successful:\n{string.Join("\n", results.Select(x => $"- {x.Key.Name}: {x.Value} items updated"))}", "Success"); } else if(exception is not null) { MessageWindow.ShowError("Error while updating dimensions", exception); } } private bool ShouldUpdateExpressions = false; protected override void DoBeforeSave(IDynamicEditorForm editor, ProductDimensionUnit[] items) { base.DoBeforeSave(editor, items); ShouldUpdateExpressions = false; if(items.Any(x => x.HasOriginalValue(x => x.Format) || x.HasOriginalValue(x => x.Formula))) { if(MessageWindow.ShowYesNo("The format and/or formula has been changed; do you wish to update the UnitSize/Value of every item that uses dimension to match the new expression? (This may take a while)", "Update expressions?")) { ShouldUpdateExpressions = true; } } } protected override void DoAfterSave(IDynamicEditorForm editor, ProductDimensionUnit[] items) { base.DoAfterSave(editor, items); if (ShouldUpdateExpressions) { UpdateExpressions(items); } } protected override bool DoMerge(CoreRow[] rows) { var columns = new Column[] { new(x => x.HasLength), new(x => x.HasQuantity), new(x => x.HasHeight), new(x => x.HasWeight), new(x => x.HasWidth), }; var target = rows[^1].ToObject(); if(columns.Any(c => rows.Select(r => r[c.Property]).Distinct().Skip(1).Any())) { MessageWindow.ShowMessage("These dimension units are incompatible, and cannot be merged.\n\n(Dimension units can only be merged if they have the same [HasQuantity], [HasLength], [HasWidth], [HasHeight] and [HasWeight] values).", "Incompatible units", image: MessageWindow.WarningImage); return false; } if (base.DoMerge(rows)) { if(MessageWindow.ShowYesNo( $"Do you wish to update the UnitSize/Value for every item that uses {target.Code}? (This may take a while)", "Update Expressions?")) { UpdateExpressions([target]); } return true; } return false; } protected override void CustomiseEditor(ProductDimensionUnit[] items, DynamicGridColumn column, BaseEditor editor) { base.CustomiseEditor(items, column, editor); if (column.ColumnName == nameof(ProductDimensionUnit.Conversion) && editor is ScriptEditor scriptEditor) { scriptEditor.Type = ScriptEditorType.TemplateEditor; scriptEditor.OnEditorClicked += () => { var script = items.FirstOrDefault()?.Conversion.NotWhiteSpaceOr() ?? DimensionUnit.DefaultConvertDimensionsScript(); var editor = new ScriptEditorWindow(script, SyntaxLanguage.CSharp); if (editor.ShowDialog() == true) { foreach (var item in items) SetEditorValue(item, column.ColumnName, editor.Script); } }; } } }