namespace ThreadPilot.ViewModels { using System; using System.Collections.ObjectModel; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Input; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.Extensions.Logging; using Microsoft.Win32; using ThreadPilot.Models; using ThreadPilot.Services; using ThreadPilot.ViewModels; public partial class PowerPlanViewModel : BaseViewModel { internal const string MorePlansUrl = "https://discord.gg/EmPfb57Kfr"; private readonly IPowerPlanService powerPlanService; private System.Timers.Timer? refreshTimer; private bool isAutoRefreshPaused = true; private int isRefreshInProgress; [ObservableProperty] private ObservableCollection powerPlans = new(); [ObservableProperty] private ObservableCollection customPowerPlans = new(); [ObservableProperty] private PowerPlanModel? selectedPowerPlan; [ObservableProperty] private PowerPlanModel? selectedCustomPlan; [ObservableProperty] private PowerPlanModel? activePowerPlan; public PowerPlanViewModel( ILogger logger, IPowerPlanService powerPlanService, IEnhancedLoggingService? enhancedLoggingService = null, IActivityAuditService? activityAuditService = null) : base(logger, enhancedLoggingService, activityAuditService) { this.powerPlanService = powerPlanService; this.SetupRefreshTimer(); } private void SetupRefreshTimer() { this.refreshTimer = new System.Timers.Timer(10000); // PERFORMANCE OPTIMIZATION: Increased to 10 second refresh - power plans change infrequently this.refreshTimer.Elapsed += async (s, e) => { if (this.isAutoRefreshPaused) { return; } if (Interlocked.Exchange(ref this.isRefreshInProgress, 1) == 1) { return; } try { var dispatcher = System.Windows.Application.Current?.Dispatcher; if (dispatcher == null) { return; } await dispatcher.InvokeAsync(async () => { if (!this.isAutoRefreshPaused) { await this.RefreshPowerPlansCoreAsync(reportStatus: false); } }); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Power plan refresh timer error: {ex.Message}"); } finally { Interlocked.Exchange(ref this.isRefreshInProgress, 0); } }; } protected override void OnDispose() { this.isAutoRefreshPaused = true; if (this.refreshTimer != null) { this.refreshTimer.Stop(); this.refreshTimer.Dispose(); this.refreshTimer = null; } base.OnDispose(); } public void PauseAutoRefresh() { this.isAutoRefreshPaused = true; this.refreshTimer?.Stop(); } public void ResumeAutoRefresh(bool refreshImmediately = true) { var wasPaused = this.isAutoRefreshPaused; this.isAutoRefreshPaused = false; this.refreshTimer?.Start(); if (!refreshImmediately || !wasPaused) { return; } _ = System.Windows.Application.Current.Dispatcher.InvokeAsync(async () => { try { await this.RefreshPowerPlansCoreAsync(reportStatus: false); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Power plan immediate refresh error: {ex.Message}"); } }); } [RelayCommand] private void OpenMorePlans() => this.OpenExternalLink(MorePlansUrl); [RelayCommand] public async Task LoadPowerPlans() { try { this.SetStatus("Loading power plans..."); await this.RefreshPowerPlansCoreAsync(reportStatus: false); this.SetStatus("Power plans loaded.", false); } catch (Exception ex) { await this.SetOperationFailedAsync($"Error loading power plans: {ex.Message}", "PowerPlanLoadFailed"); } } [RelayCommand] private async Task RefreshPowerPlans() { if (this.IsBusy) { return; } try { await this.RefreshPowerPlansCoreAsync(reportStatus: true); } catch (Exception ex) { await this.SetOperationFailedAsync($"Error refreshing power plans: {ex.Message}", "PowerPlanRefreshFailed"); } } [RelayCommand] private async Task SetActivePlan() { if (this.SelectedPowerPlan == null) { return; } try { var targetPlan = this.SelectedPowerPlan; this.SetStatus($"Setting active power plan to {targetPlan.Name}..."); var success = await this.powerPlanService.SetActivePowerPlan(targetPlan); if (success) { this.ActivePowerPlan = targetPlan; await this.RefreshPowerPlansCoreAsync(reportStatus: false); this.SetStatus($"Power plan applied: {targetPlan.Name}.", false); await this.LogUserActionAsync("PowerPlanApplied", $"Applied power plan {targetPlan.Name}", $"Guid: {targetPlan.Guid}"); } else { await this.SetOperationFailedAsync($"Failed to set power plan {targetPlan.Name}", "PowerPlanApplyFailed"); } } catch (Exception ex) { await this.SetOperationFailedAsync($"Error setting power plan: {ex.Message}", "PowerPlanApplyFailed"); } } [RelayCommand] private async Task ImportCustomPlan() { if (this.SelectedCustomPlan == null) { return; } try { var customPlan = this.SelectedCustomPlan; this.SetStatus($"Importing custom power plan {customPlan.Name}..."); var success = await this.powerPlanService.ImportCustomPowerPlan(customPlan.FilePath); if (success) { await this.RefreshPowerPlansCoreAsync(reportStatus: false); this.SetStatus($"Power plan imported: {customPlan.Name}.", false); await this.LogUserActionAsync("PowerPlanImported", $"Imported power plan {customPlan.Name}", customPlan.FilePath); } else { await this.SetOperationFailedAsync($"Failed to import power plan {customPlan.Name}", "PowerPlanImportFailed"); } } catch (Exception ex) { await this.SetOperationFailedAsync($"Error importing power plan: {ex.Message}", "PowerPlanImportFailed"); } } [RelayCommand] private async Task AddCustomPlanFile() { try { var dialog = new OpenFileDialog { Title = "Select custom power plan", Filter = "Power Plan Files (*.pow)|*.pow|All Files (*.*)|*.*", FilterIndex = 1, CheckFileExists = true, CheckPathExists = true, Multiselect = false, }; if (dialog.ShowDialog() != true) { return; } this.SetStatus("Adding custom power plan file..."); var success = await this.powerPlanService.AddCustomPowerPlanFileAsync(dialog.FileName); if (success) { await this.RefreshPowerPlansCoreAsync(reportStatus: false); this.SetStatus("Custom power plan added to library.", false); await this.LogUserActionAsync("PowerPlanAdded", "Added custom power plan file", dialog.FileName); } else { await this.SetOperationFailedAsync("Failed to add custom power plan file.", "PowerPlanAddFailed"); } } catch (Exception ex) { await this.SetOperationFailedAsync($"Error adding custom power plan file: {ex.Message}", "PowerPlanAddFailed"); } } [RelayCommand] private async Task DeletePowerPlan(PowerPlanModel? powerPlan) { var targetPlan = powerPlan ?? this.SelectedPowerPlan; if (targetPlan == null) { return; } var activePlan = this.ActivePowerPlan ?? await this.powerPlanService.GetActivePowerPlan(); if (targetPlan.IsActive || string.Equals(targetPlan.Guid, activePlan?.Guid, StringComparison.OrdinalIgnoreCase)) { await this.SetOperationFailedAsync("Switch to another power plan before deleting the active plan.", "PowerPlanDeleteBlocked"); return; } try { this.SetStatus($"Deleting power plan {targetPlan.Name}..."); var success = await this.powerPlanService.DeletePowerPlanAsync(targetPlan.Guid); if (!success) { await this.SetOperationFailedAsync( $"Could not delete power plan {targetPlan.Name}. Windows may not allow this plan to be removed.", "PowerPlanDeleteFailed"); return; } await this.RefreshPowerPlansCoreAsync(reportStatus: false); this.SetStatus($"Power plan deleted: {targetPlan.Name}.", false); await this.LogUserActionAsync("PowerPlanDeleted", $"Deleted power plan {targetPlan.Name}", $"Guid: {targetPlan.Guid}"); } catch (Exception ex) { await this.SetOperationFailedAsync($"Error deleting power plan: {ex.Message}", "PowerPlanDeleteFailed"); } } private async Task RefreshPowerPlansCoreAsync(bool reportStatus) { var currentPlans = await this.powerPlanService.GetPowerPlansAsync(); var customPlans = await this.powerPlanService.GetCustomPowerPlansAsync(); this.PowerPlans = new ObservableCollection(currentPlans); this.CustomPowerPlans = new ObservableCollection(customPlans); this.ActivePowerPlan = this.PowerPlans.FirstOrDefault(plan => plan.IsActive); if (this.SelectedPowerPlan != null) { this.SelectedPowerPlan = this.PowerPlans.FirstOrDefault(p => p.Guid == this.SelectedPowerPlan.Guid); } if (reportStatus) { this.SetStatus("Power plans refreshed.", false); await this.LogUserActionAsync("PowerPlansRefreshed", "Refreshed power plan list"); } } private async Task SetOperationFailedAsync(string message, string action) { this.SetStatus(message, false); this.SetError(message); await this.LogUserActionAsync(action, message); } } }