using System; using System.Collections.Generic; using CommunityToolkit.Mvvm.ComponentModel; namespace ThreadPilot.Models { /// /// Configuration model for process monitoring and power plan associations /// public partial class ProcessMonitorConfiguration : ObservableObject { [ObservableProperty] private string defaultPowerPlanGuid = string.Empty; [ObservableProperty] private string defaultPowerPlanName = string.Empty; [ObservableProperty] private bool isEventBasedMonitoringEnabled = true; [ObservableProperty] private bool isFallbackPollingEnabled = true; [ObservableProperty] private int pollingIntervalSeconds = 5; [ObservableProperty] private bool preventDuplicatePowerPlanChanges = true; [ObservableProperty] private int powerPlanChangeDelayMs = 1000; [ObservableProperty] private bool enableLogging = true; [ObservableProperty] private List associations = new(); [ObservableProperty] private DateTime lastSavedDate = DateTime.Now; [ObservableProperty] private string configurationVersion = "1.0"; public ProcessMonitorConfiguration() { Associations = new List(); } /// /// Gets all enabled associations sorted by priority (descending) /// public IEnumerable GetEnabledAssociations() { return Associations .Where(a => a.IsEnabled) .OrderByDescending(a => a.Priority) .ThenBy(a => a.ExecutableName); } /// /// Finds the best matching association for a process /// public ProcessPowerPlanAssociation? FindMatchingAssociation(ProcessModel process) { return GetEnabledAssociations() .FirstOrDefault(a => a.MatchesProcess(process)); } /// /// Finds association by executable name /// public ProcessPowerPlanAssociation? FindAssociationByExecutable(string executableName) { return Associations .FirstOrDefault(a => a.MatchesExecutable(executableName)); } /// /// Adds or updates an association /// public void AddOrUpdateAssociation(ProcessPowerPlanAssociation association) { var existing = Associations.FirstOrDefault(a => a.Id == association.Id); if (existing != null) { var index = Associations.IndexOf(existing); Associations[index] = association; } else { Associations.Add(association); } LastSavedDate = DateTime.Now; } /// /// Removes an association /// public bool RemoveAssociation(string associationId) { var association = Associations.FirstOrDefault(a => a.Id == associationId); if (association != null) { Associations.Remove(association); LastSavedDate = DateTime.Now; return true; } return false; } /// /// Validates the configuration /// public List Validate() { var errors = new List(); if (PollingIntervalSeconds < 1) errors.Add("Polling interval must be at least 1 second"); if (PowerPlanChangeDelayMs < 0) errors.Add("Power plan change delay cannot be negative"); // Check for duplicate associations var duplicates = Associations .GroupBy(a => new { a.ExecutableName, a.MatchByPath }) .Where(g => g.Count() > 1) .Select(g => g.Key.ExecutableName); foreach (var duplicate in duplicates) { errors.Add($"Duplicate association found for executable: {duplicate}"); } return errors; } } }