using System; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Web; using Avalonia.Controls; using Avalonia.Media.Imaging; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using GazpromApp.Enums; using GazpromApp.Messages; using GazpromApp.Models; using Image = GazpromApp.Models.Image; namespace GazpromApp.ViewModels; public partial class EditEquipmentViewModel : ViewModelBase { [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(CompleteEditCommand))] private string? _name; [ObservableProperty] private bool _isNameEditing; [RelayCommand] private void EditName() { IsNameEditing = !IsNameEditing; IsDescriptionEditing = false; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(CompleteEditCommand))] private string? _description; [ObservableProperty] private bool _isDescriptionEditing; [RelayCommand] private void EditDescription() { IsDescriptionEditing = !IsDescriptionEditing; IsNameEditing = false; } public ObservableCollection Images { get; set; } [RelayCommand] private async Task AddImage(Window w) { var files = await WeakReferenceMessenger.Default.Send(new RequestImageMessage(this)); foreach (var file in files) { try { Images.Add(new ImageViewModel(new Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(file.Path.AbsolutePath)), Parent = this}); } catch (ArgumentException) { var error = new Error { Title = "Ошибка обработки картинки", Message = $"Картинка не была добавлена: \"{HttpUtility.UrlDecode(file.Name)}\"", Severity = ErrorSeverity.Error }; _ = await WeakReferenceMessenger.Default.Send(new ErrorMessage(new ErrorViewModel(error))); } } } public ObservableCollection Inspections { get; set; } [RelayCommand] private void AddInspection() => Inspections.Add(new InspectionViewModel { Name = "Название инспекции", InspectionDate = DateTimeOffset.Now, PeriodValue = 1, PeriodType = PeriodType.Day }); private void RemoveInspection(object sender, RemoveInspectionMessage msg) { if (Inspections.Contains(msg.Inspection)) Inspections.Remove(msg.Inspection); } [RelayCommand] private void DeleteInspection(InspectionViewModel inspection) { Inspections.Remove(inspection); } public ObservableCollection Properties { get; set; } [RelayCommand] private void AddProperty() => Properties.Add(new PropertyViewModel {Parent = this}); private void RemoveProperty(object sender, RemovePropertyMessage msg) { if (Properties.Contains(msg.Property)) Properties.Remove(msg.Property); } private bool CanCompleteEdit() => !string.IsNullOrWhiteSpace(Name); [RelayCommand(CanExecute = nameof(CanCompleteEdit))] private void CompleteEdit() { _equipment.Name = Name; _equipment.Description = Description; _equipment.Images = Images; _equipment.Inspections = Inspections; _equipment.Properties = Properties; _ = _equipment.GetEquipment(); CallUpdatePoint(_equipment); } [RelayCommand] private void CancelEdit(Window w) { CallUpdatePoint(_equipment); } // Visual and other properties private readonly EquipmentViewModel _equipment; // ReSharper disable once MemberCanBeMadeStatic.Global public PeriodType[] AllPeriods => Enum.GetValues() .Where(p => p != PeriodType.Unknown) .ToArray(); private void CallUpdatePoint(ViewModelBase vm) { switch (vm) { case PointViewModel pointViewModel: WeakReferenceMessenger.Default.Send(new EditPointMessage(pointViewModel)); break; case EquipmentViewModel equipmentViewModel: CallUpdatePoint(equipmentViewModel.Parent ?? throw new InvalidOperationException("Parent equipment not found in hierarchy")); break; default: throw new ArgumentOutOfRangeException(nameof(vm), vm, null); } } public EditEquipmentViewModel() { Name = "Testing"; Description = "Testing"; Images = []; Inspections = []; Properties = []; _equipment = new EquipmentViewModel(); } public EditEquipmentViewModel(EquipmentViewModel equipment) { Name = equipment.Name; Description = equipment.Description; Images = equipment.Images; Inspections = new ObservableCollection(equipment.Inspections.Select(e => e)); Properties = new ObservableCollection(equipment.Properties.Select(e => e)); _equipment = equipment; WeakReferenceMessenger.Default.Register(this, RemoveProperty); WeakReferenceMessenger.Default.Register(this, RemoveInspection); } ~EditEquipmentViewModel() { WeakReferenceMessenger.Default.UnregisterAll(this); } }