using System; using System.Collections.ObjectModel; using System.Linq; using CommunityToolkit.Mvvm.ComponentModel; using GazpromApp.Models; using GazpromApp.Utils; namespace GazpromApp.ViewModels; public partial class EquipmentViewModel : ViewModelBase { private Equipment Equipment { get; } [ObservableProperty] private string? _name; [ObservableProperty] private string? _description; public ObservableCollection Images { get; init; } = []; public ObservableCollection Properties { get; } public ObservableCollection Inspections { get; } [ObservableProperty] private string _nearestInspection; public EquipmentViewModel() { Name = string.Empty; Description = string.Empty; Properties = []; Inspections = []; NearestInspection = "Нету инспекций"; Equipment = new Equipment(); } public EquipmentViewModel(Equipment equipment) { Name = equipment.Name; Description = equipment.Description; Images = new ObservableCollection(equipment.Images.Select(i => new ImageViewModel(i))); Properties = new ObservableCollection(equipment.Properties.Select(e => new PropertyViewModel(e))); var times = equipment.Inspections.Select(e => new InspectionViewModel(e)) .Select(e => e.GetNearestDates()) .ToList(); Inspections = new ObservableCollection(equipment.Inspections .Select((e, i) => new InspectionViewModel(e) { InspectionDate = times[i][0] })); NearestInspection = times .Where(e => e[1] > DateTime.Now) .Select(e => e[1]) .OrderBy(e => e) .FirstOrDefault() .ToLongDateString(); if (Inspections.Count == 0) NearestInspection = "Нету инспекций"; Equipment = equipment; } public Equipment GetEquipment() { Equipment.Name = Name; Equipment.Description = Description; Equipment.Images = Images.Select(e => e.GetImage()).ToList(); Equipment.Inspections = Inspections.Select(e => e.GetInspection()).ToList(); return Equipment; } }