using System.Windows; using System.Windows.Controls; using System.Windows.Media; using Comal.Classes; using InABox.Core; using InABox.DynamicGrid; using Syncfusion.Windows.Shared; namespace PRS.Shared; public class MinimumStockEditorControl : DynamicEnclosedEditorControl { private readonly Grid _grid = new() { VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment = HorizontalAlignment.Stretch }; private readonly CheckBox _customStock = new() { Content = "Custom", IsThreeState = false, VerticalAlignment = VerticalAlignment.Center }; private readonly IntegerTextBox _stockLevel = new(){ Margin = new Thickness(5, 0, 0, 0), TextAlignment = TextAlignment.Center, VerticalAlignment = VerticalAlignment.Stretch, }; private readonly CheckBox _customUsage = new() { Content = "# Days", Margin = new Thickness(5, 0, 0, 0), IsThreeState = false, VerticalAlignment = VerticalAlignment.Center }; private readonly IntegerTextBox _usageDays = new() { Margin = new Thickness(5, 0, 0, 0), TextAlignment = TextAlignment.Center, VerticalAlignment = VerticalAlignment.Stretch, }; private readonly Label _usageFactorLabel = new() { Content = "Usage Factor", VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(5, 0, 0, 0), }; private readonly DoubleTextBox _usageFactor = new() { Margin = new Thickness(5, 0, 0, 0), TextAlignment = TextAlignment.Center, VerticalAlignment = VerticalAlignment.Stretch, }; private readonly Label _stockFactorLabel = new() { Content = "Stock Factor", VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(5, 0, 0, 0) }; private readonly DoubleTextBox _stockFactor = new() { Margin = new Thickness(5, 0, 0, 0), TextAlignment = TextAlignment.Center, VerticalAlignment = VerticalAlignment.Stretch, }; public override void Configure() { } protected override FrameworkElement CreateEditor() { void AddEditor(FrameworkElement element, ref int col) { element.SetValue(Grid.ColumnProperty,col); _grid.Children.Add(element); col++; } _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) }); _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) }); _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) }); _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) }); _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) }); int col = 0; AddEditor(_customStock,ref col); _customStock.Checked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomStockLevel), true); _customStock.Unchecked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomStockLevel), true); AddEditor(_stockLevel,ref col); _stockLevel.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.StockLevel), false); AddEditor(_customUsage,ref col); _customUsage.Checked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomUsage), true); _customUsage.Unchecked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomUsage), true); AddEditor(_usageDays,ref col); _usageDays.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.UsageDays), false); AddEditor(_usageFactorLabel,ref col); AddEditor(_usageFactor,ref col); _usageFactor.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.UsageFactor), false); AddEditor(_stockFactorLabel,ref col); AddEditor(_stockFactor,ref col); _stockFactor.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.StockFactor), false); return _grid; } private void UpdateValue(string fieldname, bool checkenabled) { CheckChanged(fieldname); if (checkenabled) CheckEnabled(IsEnabled); } private void CheckEnabled(bool enabled) { _customUsage.IsEnabled = enabled; _usageDays.IsEnabled = enabled && _customUsage.IsChecked == true; _usageDays.Value = _customUsage.IsChecked != true ? 0 : (_usageDays.Value ?? 0) > 0 ? _usageDays.Value : 30; _usageFactor.IsEnabled = enabled && _customUsage.IsChecked == true; _usageFactor.Value = _customUsage.IsChecked != true ? 0 : (_usageFactor.Value ?? 0.0).IsEffectivelyGreaterThan(0.0) ? _usageFactor.Value : 1.0; _stockFactor.IsEnabled = enabled && _customUsage.IsChecked == true; _stockFactor.Value = _customUsage.IsChecked != true ? 0 : (_stockFactor.Value ?? 0.0).IsEffectivelyGreaterThan(0.0) ? _stockFactor.Value : 1.0; _customStock.IsEnabled = enabled; _stockLevel.IsEnabled = enabled && _customStock.IsChecked == true; _stockLevel.Value = _customStock.IsChecked != true ? 0 : (_stockLevel.Value ?? 0); } public override int DesiredWidth() => int.MaxValue; public override int DesiredHeight() => 30; public override void SetFocus() => _customUsage.Focus(); public override void SetColor(Color color) { _usageDays.Background = new SolidColorBrush(color); _usageFactor.Background = new SolidColorBrush(color); _stockFactor.Background = new SolidColorBrush(color); _stockLevel.Background = new SolidColorBrush(color); } protected override IEnumerable> GetChildValues() { yield return new(nameof(ProductInstanceMinimumStock.CustomUsage), _customUsage.IsChecked ?? false); yield return new(nameof(ProductInstanceMinimumStock.UsageDays), (int)(_usageDays.Value ?? 0L)); yield return new(nameof(ProductInstanceMinimumStock.UsageFactor), _usageFactor.Value ?? 0F); yield return new(nameof(ProductInstanceMinimumStock.StockFactor), _stockFactor.Value ?? 0F); yield return new(nameof(ProductInstanceMinimumStock.CustomStockLevel), _customStock.IsChecked ?? false); yield return new(nameof(ProductInstanceMinimumStock.StockLevel), (int)(_stockLevel.Value ?? 0L)); } protected override object? GetChildValue(string property) { if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomUsage))) return _customUsage.IsChecked ?? false; if (string.Equals(property,nameof(ProductInstanceMinimumStock.UsageDays))) return (int)(_usageDays.Value ?? 0L); if (string.Equals(property,nameof(ProductInstanceMinimumStock.UsageFactor))) return _usageFactor.Value ?? 0F; if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockFactor))) return _stockFactor.Value ?? 0F; if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomStockLevel))) return _customStock.IsChecked ?? false; if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockLevel))) return (int)(_stockLevel.Value ?? 0L); return null; } protected override void SetChildValue(string property, object? value) { if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomUsage))) _customUsage.IsChecked = (bool)(value ?? false); else if(string.Equals(property,nameof(ProductInstanceMinimumStock.UsageDays))) _usageDays.Value = (int)(value ?? 0); else if (string.Equals(property,nameof(ProductInstanceMinimumStock.UsageFactor))) _usageFactor.Value = (double)(value ?? 0.0); else if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockFactor))) _stockFactor.Value = (double)(value ?? 0.0); else if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomStockLevel))) _customStock.IsChecked = (bool)(value ?? false); else if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockLevel))) _stockLevel.Value = (int)(value ?? 0); } public override void SetEnabled(bool enabled) { base.SetEnabled(enabled); CheckEnabled(enabled); } }