55using System . Linq ;
66using System . Windows ;
77using System . Windows . Controls ;
8+ using Newtonsoft . Json . Linq ;
89
910namespace WinOptimizer . AI
1011{
@@ -13,6 +14,7 @@ public partial class HistoryWindow : Window
1314 public HistoryWindow ( )
1415 {
1516 InitializeComponent ( ) ;
17+ ApplyLanguage ( ) ;
1618 LoadHistory ( ) ;
1719
1820 // Subscribe to new entries to auto-refresh
@@ -58,13 +60,16 @@ private void ApplyFilters()
5860 // Filter by action type
5961 if ( TypeFilterComboBox . SelectedItem is ComboBoxItem item )
6062 {
61- var selected = item . Content . ToString ( ) ;
63+ var selected = ( item . Tag ? . ToString ( ) ?? "all" ) . ToLowerInvariant ( ) ;
6264 filtered = selected switch
6365 {
64- "🤖 AutoKill" => filtered . Where ( e => e . Action == ActionType . AutoKill ) ,
65- "🔴 Manual Kill" => filtered . Where ( e => e . Action == ActionType . ManualKill ) ,
66- "🛡️ Whitelist" => filtered . Where ( e => e . Action == ActionType . WhitelistAdd ) ,
67- "😴 Snooze" => filtered . Where ( e => e . Action == ActionType . Snooze ) ,
66+ "autokill" => filtered . Where ( e => e . Action == ActionType . AutoKill ) ,
67+ "manualkill" => filtered . Where ( e => e . Action == ActionType . ManualKill ) ,
68+ "whitelist" => filtered . Where ( e => e . Action == ActionType . WhitelistAdd ) ,
69+ "service" => filtered . Where ( e => e . TargetType == "Service" ) ,
70+ "task" => filtered . Where ( e => e . TargetType == "Task" ) ,
71+ "autorun" => filtered . Where ( e => e . TargetType == "Autorun" ) ,
72+ "snooze" => filtered . Where ( e => e . Action == ActionType . Snooze ) ,
6873 _ => filtered
6974 } ;
7075 }
@@ -163,6 +168,128 @@ private void BtnKillAgain_Click(object sender, RoutedEventArgs e)
163168 }
164169 }
165170
171+ private void BtnRestoreFromHistory_Click ( object sender , RoutedEventArgs e )
172+ {
173+ if ( sender is not Button btn || btn . DataContext is not ActionHistoryEntry entry )
174+ {
175+ return ;
176+ }
177+
178+ if ( string . IsNullOrWhiteSpace ( entry . PreviousStateJson ) )
179+ {
180+ MessageBox . Show ( "No previous state stored for this entry." , "Restore" , MessageBoxButton . OK , MessageBoxImage . Information ) ;
181+ return ;
182+ }
183+
184+ try
185+ {
186+ switch ( entry . TargetType )
187+ {
188+ case "Service" :
189+ RestoreServiceFromHistory ( entry ) ;
190+ break ;
191+ case "Task" :
192+ RestoreTaskFromHistory ( entry ) ;
193+ break ;
194+ case "Autorun" :
195+ RestoreAutorunFromHistory ( entry ) ;
196+ break ;
197+ default :
198+ MessageBox . Show ( "Restore is available only for Service/Task/Autorun entries." , "Restore" , MessageBoxButton . OK , MessageBoxImage . Information ) ;
199+ return ;
200+ }
201+
202+ ActionHistory . Record ( ActionType . StateRestored , entry . TargetType , entry . TargetName , entry . TargetPath ,
203+ reason : "Restored from history" , source : "History" ,
204+ previousStateJson : entry . AppliedStateJson , appliedStateJson : entry . PreviousStateJson ) ;
205+
206+ LoadHistory ( ) ;
207+ MessageBox . Show ( $ "State restored for '{ entry . TargetName } '.", "Restore" , MessageBoxButton . OK , MessageBoxImage . Information ) ;
208+ }
209+ catch ( Exception ex )
210+ {
211+ MessageBox . Show ( $ "Restore failed: { ex . Message } ", "Restore Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
212+ }
213+ }
214+
215+ private static void RestoreServiceFromHistory ( ActionHistoryEntry entry )
216+ {
217+ var state = JObject . Parse ( entry . PreviousStateJson ) ;
218+ var startup = state [ "Startup" ] ? . ToString ( ) ?? "Manual" ;
219+ var status = state [ "State" ] ? . ToString ( ) ?? "Stopped" ;
220+
221+ var backupEntry = new ServiceBackupEntry
222+ {
223+ ServiceName = entry . TargetName ,
224+ StartupType = startup . ToLowerInvariant ( ) switch
225+ {
226+ "automatic" => 2 ,
227+ "delayed" => 2 ,
228+ "disabled" => 4 ,
229+ _ => 3
230+ } ,
231+ IsRunning = status . Equals ( "Running" , StringComparison . OrdinalIgnoreCase )
232+ } ;
233+
234+ OptimizationBackupManager . RestoreService ( backupEntry ) ;
235+ }
236+
237+ private static void RestoreTaskFromHistory ( ActionHistoryEntry entry )
238+ {
239+ var state = JObject . Parse ( entry . PreviousStateJson ) ;
240+ var isEnabled = state [ "Enabled" ] ? . Value < bool > ( ) ?? true ;
241+ var taskPath = string . IsNullOrWhiteSpace ( entry . TargetPath ) ? entry . TargetName : entry . TargetPath ;
242+
243+ var backupEntry = new TaskBackupEntry
244+ {
245+ TaskPath = taskPath ,
246+ IsEnabled = isEnabled
247+ } ;
248+
249+ OptimizationBackupManager . RestoreTask ( backupEntry ) ;
250+ }
251+
252+ private static void RestoreAutorunFromHistory ( ActionHistoryEntry entry )
253+ {
254+ var state = JObject . Parse ( entry . PreviousStateJson ) ;
255+ var present = state [ "Present" ] ? . Value < bool > ( ) ?? false ;
256+ if ( ! present )
257+ {
258+ throw new InvalidOperationException ( "Previous state indicates this autorun entry did not exist." ) ;
259+ }
260+
261+ var backupEntry = new AutorunBackupEntry
262+ {
263+ Location = state [ "Location" ] ? . ToString ( ) ,
264+ EntryName = state [ "Name" ] ? . ToString ( ) ?? entry . TargetName ,
265+ Command = state [ "Command" ] ? . ToString ( )
266+ } ;
267+
268+ OptimizationBackupManager . RestoreAutorun ( backupEntry ) ;
269+ }
270+
271+ private void ApplyLanguage ( )
272+ {
273+ Title = LanguageManager . GetString ( "HistoryTitle" ) ;
274+ HistoryTitleText . Text = LanguageManager . GetString ( "HistoryHeader" ) ;
275+ FilterLabelText . Text = LanguageManager . GetString ( "FilterLabel" ) ;
276+ BtnExport . Content = LanguageManager . GetString ( "BtnExportHistoryJson" ) ;
277+ BtnClearAll . Content = LanguageManager . GetString ( "BtnClearHistory" ) ;
278+ BtnClose . Content = LanguageManager . GetString ( "PromptEditorBtnClose" ) ;
279+
280+ if ( TypeFilterComboBox . Items . Count >= 8 )
281+ {
282+ ( ( ComboBoxItem ) TypeFilterComboBox . Items [ 0 ] ) . Content = LanguageManager . GetString ( "FilterAllActions" ) ;
283+ ( ( ComboBoxItem ) TypeFilterComboBox . Items [ 1 ] ) . Content = LanguageManager . GetString ( "FilterAutoKill" ) ;
284+ ( ( ComboBoxItem ) TypeFilterComboBox . Items [ 2 ] ) . Content = LanguageManager . GetString ( "FilterManualKill" ) ;
285+ ( ( ComboBoxItem ) TypeFilterComboBox . Items [ 3 ] ) . Content = LanguageManager . GetString ( "FilterWhitelist" ) ;
286+ ( ( ComboBoxItem ) TypeFilterComboBox . Items [ 4 ] ) . Content = LanguageManager . GetString ( "FilterServiceChanges" ) ;
287+ ( ( ComboBoxItem ) TypeFilterComboBox . Items [ 5 ] ) . Content = LanguageManager . GetString ( "FilterTaskChanges" ) ;
288+ ( ( ComboBoxItem ) TypeFilterComboBox . Items [ 6 ] ) . Content = LanguageManager . GetString ( "FilterAutorunChanges" ) ;
289+ ( ( ComboBoxItem ) TypeFilterComboBox . Items [ 7 ] ) . Content = LanguageManager . GetString ( "FilterSnooze" ) ;
290+ }
291+ }
292+
166293 private void BtnExport_Click ( object sender , RoutedEventArgs e )
167294 {
168295 try
0 commit comments