compactrs 2025.12.19

High-performance native Windows file compressor using WOF (Windows Overlay Filter)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
#![allow(unsafe_op_in_unsafe_fn)]

use windows_sys::core::{PCWSTR, HRESULT, GUID};
use windows_sys::Win32::Foundation::{HINSTANCE, HWND, LPARAM, LRESULT, WPARAM, RECT, POINT};
use windows_sys::Win32::Graphics::Gdi::InvalidateRect;
use windows_sys::Win32::UI::WindowsAndMessaging::{
    CreateWindowExW, DefWindowProcW, LoadCursorW, RegisterClassW, ShowWindow,
    CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, IDC_ARROW, SW_SHOW, WM_DESTROY, WNDCLASSW,
    WS_OVERLAPPEDWINDOW, WS_VISIBLE, WM_CREATE, WM_SIZE, WM_COMMAND,
    GetWindowLongPtrW, SetWindowLongPtrW, GWLP_USERDATA, WM_DROPFILES, MessageBoxW, MB_OK,
    SendMessageW, CB_ADDSTRING, CB_SETCURSEL, CB_GETCURSEL, SetWindowTextW, WM_TIMER, SetTimer,
    MB_ICONINFORMATION, WM_NOTIFY, BM_GETCHECK, GetClientRect, GetWindowRect,
    BM_SETCHECK, ChangeWindowMessageFilterEx, MSGFLT_ALLOW, WM_COPYDATA,
    WM_CONTEXTMENU, TrackPopupMenu, CreatePopupMenu, AppendMenuW, MF_STRING, TPM_RETURNCMD, TPM_LEFTALIGN,
    DestroyMenu, GetCursorPos, WM_SETTINGCHANGE, SW_SHOWNORMAL, SetForegroundWindow,
    GetForegroundWindow, GetWindowThreadProcessId, BringWindowToTop, WM_CLOSE, WM_NCDESTROY, DestroyWindow,
    KillTimer,
};
use windows_sys::Win32::System::DataExchange::{
    COPYDATASTRUCT, OpenClipboard, GetClipboardData, CloseClipboard, IsClipboardFormatAvailable,
};
use windows_sys::Win32::System::Threading::GetCurrentThreadId;
use windows_sys::Win32::Foundation::{TRUE, FALSE};
use windows_sys::Win32::UI::Shell::{
    DragQueryFileW, DragFinish, HDROP, DragAcceptFiles,
    ShellExecuteW,
};
use windows_sys::Win32::UI::Controls::{
    PBM_SETRANGE32, PBM_SETPOS, NM_DBLCLK, NMITEMACTIVATE, NMHDR, NM_CLICK,
    InitCommonControlsEx, INITCOMMONCONTROLSEX, ICC_WIN95_CLASSES, ICC_STANDARD_CLASSES,
    LVN_ITEMCHANGED, BST_CHECKED, LVN_KEYDOWN, NMLVKEYDOWN, LVN_COLUMNCLICK, NMLISTVIEW,
};
use windows_sys::Win32::UI::Input::KeyboardAndMouse::{GetKeyState, VK_DELETE, VK_CONTROL};

#[link(name = "user32")]
unsafe extern "system" {
    fn AttachThreadInput(idAttach: u32, idAttachTo: u32, fAttach: i32) -> i32;
}
use std::cmp::Ordering as CmpOrdering;
use windows_sys::Win32::System::Com::{CoCreateInstance, CoTaskMemFree, CLSCTX_ALL};

use crate::ui::controls::{
    IDC_COMBO_ALGO, IDC_STATIC_TEXT, IDC_PROGRESS_BAR, IDC_BTN_CANCEL, IDC_BATCH_LIST,
    IDC_BTN_ADD_FOLDER, IDC_BTN_REMOVE, IDC_BTN_PROCESS_ALL, IDC_BTN_ADD_FILES,
    IDC_BTN_SETTINGS, IDC_BTN_ABOUT, IDC_BTN_CONSOLE, IDC_CHK_FORCE, IDC_COMBO_ACTION_MODE,
    IDC_LBL_ACTION_MODE, IDC_LBL_ALGO, IDC_LBL_INPUT,
};
use crate::ui::components::{
    Component, FileListView, StatusBar, StatusBarIds, ActionPanel, ActionPanelIds,
    HeaderPanel, HeaderPanelIds,
};
use crate::ui::settings::show_settings_modal;
use crate::ui::about::show_about_modal;
use crate::ui::console::{show_console_window, append_log_msg};
use crate::ui::state::{AppState, Controls, UiMessage, BatchAction, BatchStatus, AppTheme, ProcessingState};
use crate::ui::taskbar::{TaskbarProgress, TaskbarState};
use crate::ui::theme;
use std::thread;
use std::sync::atomic::Ordering;
use crate::engine::wof::{WofAlgorithm, CompressionState};
use crate::engine::worker::{
    batch_process_worker,
    calculate_path_logical_size, calculate_path_disk_size, detect_path_algorithm,
};
use crate::utils::{to_wstring, u64_to_wstring, concat_wstrings};
use crate::ui::utils::{format_size, get_window_state, load_app_icon};
use crate::config::AppConfig;

const WINDOW_CLASS_NAME: &str = "CompactRS_Class";
const WINDOW_TITLE: &str = "CompactRS";

pub unsafe fn create_main_window(instance: HINSTANCE) -> Result<HWND, String> {
    unsafe {
        // Enable dark mode for the application
        // Enable dark mode for the application
        theme::set_preferred_app_mode(true);

        // Initialize Common Controls
        let iccex = INITCOMMONCONTROLSEX {
            dwSize: std::mem::size_of::<INITCOMMONCONTROLSEX>() as u32,
            dwICC: ICC_WIN95_CLASSES | ICC_STANDARD_CLASSES,
        };
        InitCommonControlsEx(&iccex);

        // Check dark mode
        let is_dark = theme::is_system_dark_mode();
        let bg_brush = theme::get_background_brush(is_dark);

        // Load icon
        let icon = load_app_icon(instance);

        let class_name = to_wstring(WINDOW_CLASS_NAME);
        let mut title_str = WINDOW_TITLE.to_string();
        if crate::engine::elevation::is_system_or_ti() {
            title_str.push_str(" [TrustedInstaller]");
        } else if crate::is_admin() {
            title_str.push_str(" [Administrator]");
        }
        let title_name = to_wstring(&title_str);

        let wc = WNDCLASSW {
            style: CS_HREDRAW | CS_VREDRAW,
            lpfnWndProc: Some(wnd_proc),
            hInstance: instance,
            hIcon: icon,
            hCursor: LoadCursorW(std::ptr::null_mut(), IDC_ARROW),
            hbrBackground: bg_brush,
            lpszClassName: class_name.as_ptr(),
            lpszMenuName: std::ptr::null(),
            cbClsExtra: 0,
            cbWndExtra: 0,
        };

        if RegisterClassW(&wc) == 0 {
            return Err("Failed to register window class".to_string());
        }

        // Load configuration
        let config = AppConfig::load();
        let (win_x, win_y) = if config.window_x < 0 || config.window_y < 0 {
            (CW_USEDEFAULT, CW_USEDEFAULT)
        } else {
            (config.window_x, config.window_y)
        };
        let win_width = if config.window_width > 0 { config.window_width } else { 900 };
        let win_height = if config.window_height > 0 { config.window_height } else { 600 };

        let hwnd = CreateWindowExW(
            0,
            class_name.as_ptr(),
            title_name.as_ptr(),
            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            win_x,
            win_y,
            win_width,
            win_height,
            std::ptr::null_mut(),
            std::ptr::null_mut(),
            instance,
            std::ptr::null(),
        );

        if hwnd == std::ptr::null_mut() {
            return Err("Failed to create window".to_string());
        }

        // Apply initial theme
        let is_dark = theme::resolve_mode(config.theme);
        theme::set_window_frame_theme(hwnd, is_dark);

        ShowWindow(hwnd, SW_SHOW);
        
        // Hostile Takeover: Force window to foreground (Bypass ASLR/Focus restrictions)
        let foreground_hwnd = GetForegroundWindow();
        if !foreground_hwnd.is_null() {
            let foreground_thread = GetWindowThreadProcessId(foreground_hwnd, std::ptr::null_mut());
            let current_thread = GetCurrentThreadId();
            
            if foreground_thread != current_thread {
                AttachThreadInput(foreground_thread, current_thread, TRUE);
                BringWindowToTop(hwnd);
                SetForegroundWindow(hwnd);
                AttachThreadInput(foreground_thread, current_thread, FALSE);
            } else {
                SetForegroundWindow(hwnd);
            }
        } else {
            SetForegroundWindow(hwnd);
        }

        Ok(hwnd)
    }
}

unsafe extern "system" fn wnd_proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
    // STATE ACCESS HELPER and THEMING
    let get_state = || get_window_state::<AppState>(hwnd);

    // Centralized handler for theme-related messages
    // Note: We need to handle this manually since theme handles GDI types
    let is_dark = get_state()
        .map(|st| theme::resolve_mode(st.theme))
        .unwrap_or_else(|| theme::is_system_dark_mode());
        
    if let Some(result) = theme::handle_standard_colors(hwnd, msg, wparam, is_dark) {
        return result;
    }
    
    // Handle WM_DRAWITEM for owner-drawn accent button
    const WM_DRAWITEM: u32 = 0x002B;
    if msg == WM_DRAWITEM {
        use windows_sys::Win32::UI::Controls::DRAWITEMSTRUCT;
        let dis = &*(lparam as *const DRAWITEMSTRUCT);
        // Check if this is our Process button (IDC_BTN_PROCESS_ALL = 113)
        if dis.CtlID == IDC_BTN_PROCESS_ALL as u32 {
            crate::ui::controls::draw_accent_button(lparam);
            return 1; // TRUE - we handled it
        }
    }

    match msg {
        WM_CREATE => {
            let mut state = Box::new(AppState::new());
             // Taskbar creation needs fixing for windows-sys, skipping strict type check or casting
            state.taskbar = Some(TaskbarProgress::new(hwnd));
            
            // 1. StatusBar
            let mut status_bar = StatusBar::new(StatusBarIds {
                label_id: IDC_STATIC_TEXT,
                progress_id: IDC_PROGRESS_BAR,
            });
            let _ = status_bar.create(hwnd);
            
            // 2. FileListView
            let file_list = FileListView::new(hwnd, 10, 40, 860, 380, IDC_BATCH_LIST);
            
            // 3. ActionPanel
            let mut action_panel = ActionPanel::new(ActionPanelIds {
                btn_files: IDC_BTN_ADD_FILES,
                btn_folder: IDC_BTN_ADD_FOLDER,
                btn_remove: IDC_BTN_REMOVE,
                lbl_input: IDC_LBL_INPUT,
                combo_action_mode: IDC_COMBO_ACTION_MODE,
                lbl_action_mode: IDC_LBL_ACTION_MODE,
                combo_algo: IDC_COMBO_ALGO,
                lbl_algo: IDC_LBL_ALGO,
                chk_force: IDC_CHK_FORCE,
                btn_process: IDC_BTN_PROCESS_ALL,
                btn_cancel: IDC_BTN_CANCEL,
            });
            let _ = action_panel.create(hwnd);
            
            // 4. HeaderPanel
            let mut header_panel = HeaderPanel::new(HeaderPanelIds {
                btn_settings: IDC_BTN_SETTINGS,
                btn_about: IDC_BTN_ABOUT,
                btn_console: IDC_BTN_CONSOLE,
            });
            let _ = header_panel.create(hwnd);
            
            // Disable cancel button
            windows_sys::Win32::UI::Input::KeyboardAndMouse::EnableWindow(action_panel.cancel_hwnd(), 0);

            // Populate algorithm combo (As Listed uses the default per-item algorithm)
            let h_combo = action_panel.combo_hwnd();
            let algos = ["As Listed", "XPRESS4K", "XPRESS8K", "XPRESS16K", "LZX"];
            for alg in algos {
                let w = to_wstring(alg);
                SendMessageW(h_combo, CB_ADDSTRING, 0, w.as_ptr() as isize);
            }
            let algo_index = match state.config.default_algo {
                WofAlgorithm::Xpress4K => 1,
                WofAlgorithm::Xpress8K => 2,
                WofAlgorithm::Xpress16K => 3,
                WofAlgorithm::Lzx => 4,
            };
            SendMessageW(h_combo, CB_SETCURSEL, algo_index as usize, 0);
            
            // Populate action mode combo
            let h_action_mode = action_panel.action_mode_hwnd();
            let action_modes = ["As Listed", "Compress All", "Decompress All"];
            for mode in action_modes {
                let w = to_wstring(mode);
                SendMessageW(h_action_mode, CB_ADDSTRING, 0, w.as_ptr() as isize);
            }
            SendMessageW(h_action_mode, CB_SETCURSEL, 0, 0);
            
            // Set initial force checkbox state
            if state.force_compress {
                SendMessageW(action_panel.force_hwnd(), BM_SETCHECK, BST_CHECKED as usize, 0);
            }

            state.controls = Some(Controls {
                file_list,
                status_bar,
                action_panel,
                header_panel,
            });

            SetWindowLongPtrW(hwnd, GWLP_USERDATA, Box::into_raw(state) as isize);
            SetTimer(hwnd, 1, 100, None);
            DragAcceptFiles(hwnd, 1); // TRUE = 1
            
            // Allow Drag and Drop messages to bypass UIPI
            ChangeWindowMessageFilterEx(hwnd, WM_DROPFILES, MSGFLT_ALLOW, std::ptr::null_mut());
            ChangeWindowMessageFilterEx(hwnd, WM_COPYDATA, MSGFLT_ALLOW, std::ptr::null_mut());
            ChangeWindowMessageFilterEx(hwnd, 0x0049, MSGFLT_ALLOW, std::ptr::null_mut()); // WM_COPYGLOBALDATA
            
            // Apply saved theme
            if let Some(st) = get_state() {
                let is_dark = theme::resolve_mode(st.theme);
                theme::set_window_frame_theme(hwnd, is_dark);
                if let Some(ctrls) = &mut st.controls {
                    ctrls.update_theme(is_dark, hwnd);
                }
                InvalidateRect(hwnd, std::ptr::null(), 1);
            }
            
            // Process startup items
            let startup_items = crate::get_startup_items();
            if !startup_items.is_empty() {
                if let Some(st) = get_state() {
                    // Start an IPC batch for startup items!
                    // This ensures subsequent WM_COPYDATA messages append to this batch instead of clearing selection.
                    st.ipc_active = true;
                    
                    for startup_item in startup_items {
                         if !st.batch_items.iter().any(|item| item.path == startup_item.path) {
                                let item_id = st.add_batch_item(startup_item.path.clone());
                                if let Some(batch_item) = st.get_batch_item_mut(item_id) {
                                    batch_item.algorithm = startup_item.algorithm;
                                    batch_item.action = startup_item.action;
                                }
                                let logical_size = calculate_path_logical_size(&startup_item.path);
                                let disk_size = calculate_path_disk_size(&startup_item.path);
                                let detected_algo = detect_path_algorithm(&startup_item.path);
                                let logical_str = format_size(logical_size);
                                let disk_str = format_size(disk_size);
                                
                                if let Some(ctrls) = &st.controls {
                                    if let Some(batch_item) = st.batch_items.iter().find(|i| i.id == item_id) {
                                        ctrls.file_list.add_item(item_id, batch_item, logical_str, disk_str, detected_algo);
                                        // Select the new item
                                        if let Some(pos) = st.batch_items.iter().position(|i| i.id == item_id) {
                                            ctrls.file_list.set_selected(pos as i32, true);
                                            // Add to pending IPC list for robust selection at trigger time
                                            st.pending_ipc_ids.push(item_id);
                                        }
                                    }
                                }
                         }
                    }
                    
                    if let Some(ctrls) = &st.controls {
                         SendMessageW(ctrls.action_panel.combo_hwnd(), CB_SETCURSEL, 0, 0);
                    }
                    // Trigger auto-start
                    SetTimer(hwnd, 2, 500, None);
                }
            }

            0
        }
        
        // WM_APP + 3: Set Enable Force Stop
        0x8003 => {
            if let Some(st) = get_state() {
                st.enable_force_stop = wparam != 0;
            }
            0
        },

        // WM_APP + 6: Set System Guard
        0x8006 => {
            if let Some(st) = get_state() {
                st.config.enable_system_guard = wparam != 0;
            }
            0
        },
        
        // WM_APP + 4: Query Force Stop
        0x8004 => {
             let mut should_kill = false;
             if let Some(st) = get_state() {
                 if st.enable_force_stop {
                     should_kill = true;
                 } else {
                     let name_ptr = wparam as *const u16;
                     let len = (0..).take_while(|&i| unsafe { *name_ptr.offset(i) } != 0).count();
                     let slice = unsafe { std::slice::from_raw_parts(name_ptr, len) };
                     let name = String::from_utf16_lossy(slice);
                     let is_dark = theme::resolve_mode(st.theme);
                     should_kill = crate::ui::dialogs::show_force_stop_dialog(hwnd, &name, is_dark);
                 }
             }
             if should_kill { 1 } else { 0 }
        },
        
        // WM_THEME_CHANGED
        0x8001 => {
            if let Some(st) = get_state() {
                let theme_val = wparam;
                let new_theme = match theme_val {
                    0 => AppTheme::System,
                    1 => AppTheme::Dark,
                    2 => AppTheme::Light,
                    _ => st.theme,
                };
                st.theme = new_theme;
                let is_dark = theme::resolve_mode(st.theme);
                theme::set_window_frame_theme(hwnd, is_dark);
                if let Some(ctrls) = &mut st.controls {
                    ctrls.update_theme(is_dark, hwnd);
                }
                InvalidateRect(hwnd, std::ptr::null(), 1);
            }
            0
        }
        
        // WM_KEYDOWN: Handle Ctrl+V for Paste
        0x0100 => { // WM_KEYDOWN
            let vk = wparam as u16;
            if vk == 0x56 { // 'V'
                 let ctrl_state = GetKeyState(VK_CONTROL as i32) as u16;
                 if (ctrl_state & 0x8000) != 0 {
                      // Ctrl + V detected
                      // 15 = CF_HDROP
                      if IsClipboardFormatAvailable(15) != 0 {
                          if OpenClipboard(hwnd) != 0 {
                               let hdrop = GetClipboardData(15) as HDROP;
                               if !hdrop.is_null() {
                                   if let Some(st) = get_state() {
                                       process_hdrop(hwnd, hdrop, st);
                                   }
                               }
                               CloseClipboard();
                          }
                      }
                 }
            }
            0
        }

        WM_COMMAND => {
            let id = (wparam & 0xFFFF) as u16;
            match id {
                 IDC_BTN_ADD_FILES => {
                     if let Ok(files) = pick_files() {
                         if let Some(st) = get_state() {
                             for file_path in files {
                                 if !st.batch_items.iter().any(|item| item.path == file_path) {
                                    let item_id = st.add_batch_item(file_path.clone());
                                    let logical_size = calculate_path_logical_size(&file_path);
                                    let disk_size = calculate_path_disk_size(&file_path);
                                    let detected_algo = detect_path_algorithm(&file_path);
                                    let logical_str = format_size(logical_size);
                                    let disk_str = format_size(disk_size);
                                    if let Some(ctrls) = &st.controls {
                                        if let Some(batch_item) = st.batch_items.iter().find(|i| i.id == item_id) {
                                            ctrls.file_list.add_item(item_id, batch_item, logical_str, disk_str, detected_algo);
                                        }
                                    }
                                 }
                             }
                         }
                     }
                 },
                 IDC_BTN_ADD_FOLDER => {
                     if let Ok(folder) = pick_folder() {
                         if let Some(st) = get_state() {
                             if !st.batch_items.iter().any(|item| item.path == folder) {
                                let item_id = st.add_batch_item(folder.clone());
                                let logical_size = calculate_path_logical_size(&folder);
                                let disk_size = calculate_path_disk_size(&folder);
                                let detected_algo = detect_path_algorithm(&folder);
                                let logical_str = format_size(logical_size);
                                let disk_str = format_size(disk_size);
                                if let Some(ctrls) = &st.controls {
                                    if let Some(batch_item) = st.batch_items.iter().find(|i| i.id == item_id) {
                                        ctrls.file_list.add_item(item_id, batch_item, logical_str, disk_str, detected_algo);
                                    }
                                }
                             }
                         }
                     }
                 },
                 IDC_BTN_REMOVE => {
                      if let Some(st) = get_state() {
                          let mut selected_indices = if let Some(ctrls) = &st.controls {
                              ctrls.file_list.get_selected_indices()
                          } else { Vec::new() };
                          selected_indices.sort_by(|a, b| b.cmp(a));
                          
                          let ids_to_remove: Vec<u32> = selected_indices.iter()
                              .filter_map(|&idx| st.batch_items.get(idx).map(|item| item.id))
                              .collect();
                          
                          for id in ids_to_remove { st.remove_batch_item(id); }
                          
                          if let Some(ctrls) = &st.controls {
                              for idx in selected_indices { ctrls.file_list.remove_item(idx as i32); }
                          }
                      }
                 },
                 IDC_BTN_PROCESS_ALL => {
                      if let Some(st) = get_state() {
                           if st.batch_items.is_empty() {
                               let w_info = to_wstring("Info");
                               let w_msg = to_wstring("Add folders first!");
                               MessageBoxW(hwnd, w_msg.as_ptr(), w_info.as_ptr(), MB_OK | MB_ICONINFORMATION);
                           } else {
                               if let Some(ctrls) = &st.controls {
                                   let mut indices_to_process = ctrls.file_list.get_selected_indices();
                                   
                                   // Auto-Start Safety Check:
                                   // If triggered by timer (lparam == 1) and no items are selected, DO NOT process everything.
                                   // This prevents "Process All" from accidentally running on existing items.
                                   let is_auto_start = lparam == 1;
                                   if is_auto_start && indices_to_process.is_empty() {
                                       return 0;
                                   }

                                   if indices_to_process.is_empty() {
                                       indices_to_process = (0..st.batch_items.len()).collect();
                                   }
                                   let idx = SendMessageW(ctrls.action_panel.combo_hwnd(), CB_GETCURSEL, 0, 0);
                                   // Index 0 = "As Listed" (use per-item algorithm)
                                   // Index 1-4 = specific algorithms
                                   let use_as_listed = idx == 0;
                                   let global_algo = match idx {
                                       1 => WofAlgorithm::Xpress4K,
                                       3 => WofAlgorithm::Xpress16K,
                                       4 => WofAlgorithm::Lzx,
                                       _ => WofAlgorithm::Xpress8K, // Default to Xpress8K
                                   };
                                   
                                   // Update display in list (show "As Listed" or specific algo)
                                   if !use_as_listed {
                                       let algo_name = match global_algo {
                                           WofAlgorithm::Xpress4K => "XPRESS4K",
                                           WofAlgorithm::Xpress8K => "XPRESS8K",
                                           WofAlgorithm::Xpress16K => "XPRESS16K",
                                           WofAlgorithm::Lzx => "LZX",
                                       };
                                       for &row in &indices_to_process {
                                           ctrls.file_list.update_item_text(row as i32, 2, to_wstring(algo_name));
                                       }
                                   }
                                   if let Some(tb) = &st.taskbar { tb.set_state(TaskbarState::Normal); }
                                   windows_sys::Win32::UI::Input::KeyboardAndMouse::EnableWindow(ctrls.action_panel.cancel_hwnd(), 1); // TRUE

                                   // format!("Processing {} items...", count)
                                   let count_w = u64_to_wstring(indices_to_process.len() as u64);
                                   let status_msg = concat_wstrings(&[&to_wstring("Processing "), &count_w, &to_wstring(" items...")]);
                                   SetWindowTextW(ctrls.status_bar.label_hwnd(), status_msg.as_ptr());
                                   
                                   let tx = st.tx.clone();
                                   let state_global = st.global_state.clone();
                                   state_global.store(ProcessingState::Running as u8, Ordering::Relaxed);
                                   
                                   let action_mode_idx = SendMessageW(ctrls.action_panel.action_mode_hwnd(), CB_GETCURSEL, 0, 0);
                                   let items: Vec<_> = indices_to_process.into_iter().filter_map(|idx| {
                                       st.batch_items.get(idx).map(|item| {
                                           let effective_action = match action_mode_idx {
                                               1 => BatchAction::Compress, 2 => BatchAction::Decompress, _ => item.action,
                                           };
                                           // Determine effective algorithm PER ITEM
                                           let effective_algo = if use_as_listed {
                                               item.algorithm
                                           } else {
                                               global_algo
                                           };
                                           (item.path.clone(), effective_action, idx, effective_algo)
                                       })
                                   }).collect();
                                   
                                   let force = st.force_compress;
                                   let guard = st.config.enable_system_guard;
                                   let main_hwnd_usize = hwnd as usize;
                                   thread::spawn(move || {
                                       batch_process_worker(items, tx, state_global, force, main_hwnd_usize, guard);
                                   });
                               }
                           }
                      }
                 },
                 IDC_BTN_CANCEL => {
                     if let Some(st) = get_state() {
                         st.global_state.store(ProcessingState::Stopped as u8, Ordering::Relaxed);
                         for item in &st.batch_items {
                             if let Some(flag) = &item.state_flag { flag.store(ProcessingState::Stopped as u8, Ordering::Relaxed); }
                         }
                         if let Some(tb) = &st.taskbar { tb.set_state(TaskbarState::Paused); }
                         if let Some(ctrls) = &st.controls {
                             windows_sys::Win32::UI::Input::KeyboardAndMouse::EnableWindow(ctrls.action_panel.cancel_hwnd(), 0);
                             let w_stop = to_wstring("Stopping...");
                             SetWindowTextW(ctrls.status_bar.label_hwnd(), w_stop.as_ptr());
                         }
                     }
                 },
                 IDC_BTN_SETTINGS => {
                      if let Some(st) = get_state() {
                          let current_theme = st.theme;
                          let is_dark = theme::resolve_mode(st.theme);
                          let enable_ctx = st.config.enable_context_menu;
                          let enable_guard = st.config.enable_system_guard;
                          let (new_theme, new_force, new_ctx, new_guard) = show_settings_modal(hwnd, current_theme, is_dark, st.enable_force_stop, enable_ctx, enable_guard);
                          if let Some(t) = new_theme {
                              st.theme = t;
                              let new_is_dark = theme::resolve_mode(st.theme);
                              theme::set_window_frame_theme(hwnd, new_is_dark);
                              if let Some(ctrls) = &mut st.controls { ctrls.update_theme(new_is_dark, hwnd); }
                              InvalidateRect(hwnd, std::ptr::null(), 1);
                          }
                          st.enable_force_stop = new_force;
                          st.config.enable_context_menu = new_ctx;
                          st.config.enable_system_guard = new_guard;
                      }
                 },
                 IDC_BTN_ABOUT => {
                     if let Some(st) = get_state() {
                         let is_dark = theme::resolve_mode(st.theme);
                         show_about_modal(hwnd, is_dark);
                     }
                 },
                 IDC_BTN_CONSOLE => {
                     if let Some(st) = get_state() {
                          let is_dark = theme::resolve_mode(st.theme);
                          show_console_window(hwnd, &st.logs, is_dark);
                     }
                 },
                 IDC_CHK_FORCE => {
                      if let Some(st) = get_state() {
                          let hwnd_ctl = lparam as HWND;
                          let state = SendMessageW(hwnd_ctl, BM_GETCHECK, 0, 0);
                          st.force_compress = state as u32 == BST_CHECKED;
                      }
                 },
                 _ => {}
            }
            0
        }
        
        WM_TIMER => {
            if wparam == 2 {
                KillTimer(hwnd, 2);
                if let Some(st) = get_state() {
                    st.ipc_active = false;
                    
                    // Robust Selection Logic:
                    // If we have pending IPC items, explicitly select them right NOW.
                    // This ensures that even if UI state drifted during debounce, we fix it before processing.
                    if !st.pending_ipc_ids.is_empty() {
                         if let Some(ctrls) = &st.controls {
                             // clear existing selection
                             let count = ctrls.file_list.get_item_count();
                             for i in 0..count {
                                 ctrls.file_list.set_selected(i, false);
                             }
                             // select pending items
                             for &id in &st.pending_ipc_ids {
                                 if let Some(pos) = st.batch_items.iter().position(|item| item.id == id) {
                                     ctrls.file_list.set_selected(pos as i32, true);
                                 }
                             }
                         }
                         st.pending_ipc_ids.clear();
                    }
                }
                // Send lparam=1 to indicate Auto-Start source
                SendMessageW(hwnd, WM_COMMAND, IDC_BTN_PROCESS_ALL as usize, 1);
                return 0;
            }
            if let Some(st) = get_state() {
                loop {
                    match st.rx.try_recv() {
                        Ok(msg) => {
                             match msg {
                                 UiMessage::Progress(cur, total) => {
                                     if let Some(ctrls) = &st.controls {
                                         SendMessageW(ctrls.status_bar.progress_hwnd(), PBM_SETRANGE32, 0, total as isize);
                                         SendMessageW(ctrls.status_bar.progress_hwnd(), PBM_SETPOS, cur as usize, 0);
                                     }
                                     if let Some(tb) = &st.taskbar { tb.set_value(cur, total); }
                                 },
                                 UiMessage::Status(text) => {
                                     if let Some(ctrls) = &st.controls {
                                         SetWindowTextW(ctrls.status_bar.label_hwnd(), text.as_ptr());
                                     }
                                 },
                                 UiMessage::Log(text) => {
                                     st.logs.push(text.clone());
                                     append_log_msg(text);
                                 },
                                 UiMessage::Error(text) => {
                                     if let Some(tb) = &st.taskbar { tb.set_state(TaskbarState::Error); }
                                     // format!("ERROR: {}", text)
                                     let err_prefix = to_wstring("ERROR: ");
                                     let full_err = concat_wstrings(&[&err_prefix, &text]);
                                     st.logs.push(full_err.clone());
                                     append_log_msg(full_err);
                                     
                                     if let Some(ctrls) = &st.controls {
                                         // text is Vec<u16>, needs null term? to_wstring added it?
                                         // UiMessage uses Vec<u16> which should be null terminated if coming from helpers
                                         // But safe to ensure. SetWindowTextW needs ptr.
                                         // text already likely has \0 from sender.
                                         // If it doesn't, we might read OOB.
                                         // Our helpers always add \0.
                                         SetWindowTextW(ctrls.status_bar.label_hwnd(), text.as_ptr());
                                     }
                                 },
                                 UiMessage::Finished => {
                                     if let Some(tb) = &st.taskbar { tb.set_state(TaskbarState::NoProgress); }
                                     if let Some(ctrls) = &st.controls {
                                         windows_sys::Win32::UI::Input::KeyboardAndMouse::EnableWindow(ctrls.action_panel.cancel_hwnd(), 0);
                                     }
                                 },
                                 UiMessage::RowUpdate(row, progress, status, _) => {
                                     if let Some(ctrls) = &st.controls {
                                         ctrls.file_list.update_item_text(row, 6, progress);
                                         ctrls.file_list.update_item_text(row, 7, status);
                                     }
                                 },
                                 UiMessage::ItemFinished(row, status, disk_size, final_state) => {
                                     if let Some(ctrls) = &st.controls {
                                         ctrls.file_list.update_item_text(row, 7, status);
                                         if !disk_size.is_empty() && disk_size.len() > 1 { ctrls.file_list.update_item_text(row, 5, disk_size); }
                                         let state_str = match final_state {
                                             CompressionState::None => "-",
                                             CompressionState::Specific(algo) => match algo {
                                                 WofAlgorithm::Xpress4K => "XPRESS4K", WofAlgorithm::Xpress8K => "XPRESS8K",
                                                 WofAlgorithm::Xpress16K => "XPRESS16K", WofAlgorithm::Lzx => "LZX",
                                             },
                                             CompressionState::Mixed => "Mixed",
                                         };
                                         ctrls.file_list.update_item_text(row, 1, to_wstring(state_str));
                                         ctrls.file_list.update_item_text(row, 8, to_wstring("â–¶ Start"));
                                         if let Some(item) = st.batch_items.get_mut(row as usize) {
                                             item.status = BatchStatus::Pending;
                                             item.state_flag = None;
                                         }
                                     }
                                 },
                                 UiMessage::BatchItemAnalyzed(id, log, disk, state) => {
                                     let log_str = format_size(log);
                                     let disk_str = format_size(disk);
                                     if let Some(pos) = st.batch_items.iter().position(|item| item.id == id) {
                                         // Update item data for sorting
                                         if let Some(item) = st.batch_items.get_mut(pos) {
                                             item.logical_size = log;
                                             item.disk_size = disk;
                                         }
                                         
                                         if let Some(ctrls) = &st.controls {
                                             ctrls.file_list.update_item_text(pos as i32, 4, log_str);
                                             ctrls.file_list.update_item_text(pos as i32, 5, disk_str);
                                             let state_str = match state {
                                                CompressionState::None => "-",
                                                CompressionState::Specific(algo) => match algo {
                                                    WofAlgorithm::Xpress4K => "XPRESS4K", WofAlgorithm::Xpress8K => "XPRESS8K",
                                                    WofAlgorithm::Xpress16K => "XPRESS16K", WofAlgorithm::Lzx => "LZX",
                                                },
                                                CompressionState::Mixed => "Mixed",
                                             };
                                             ctrls.file_list.update_item_text(pos as i32, 1, to_wstring(state_str));
                                             ctrls.file_list.update_item_text(pos as i32, 7, to_wstring("Pending"));
                                             let count = st.batch_items.len();
                                             // format!("{} item(s) analyzed.", count)
                                             let count_w = u64_to_wstring(count as u64);
                                             let msg = concat_wstrings(&[&count_w, &to_wstring(" item(s) analyzed.")]);
                                             SetWindowTextW(ctrls.status_bar.label_hwnd(), msg.as_ptr());
                                         }
                                     }
                                 },
                                 _ => {}
                             }
                        },
                        Err(_) => break,
                    }
                }
            }
            0
        }
        
        WM_SIZE => {
            let mut client_rect: RECT = unsafe { std::mem::zeroed() };
            if GetClientRect(hwnd, &mut client_rect) != 0 {
                if let Some(st) = get_state() {
                    if let Some(ctrls) = &mut st.controls {
                         ctrls.status_bar.on_resize(&client_rect);
                         ctrls.file_list.on_resize(&client_rect);
                         ctrls.action_panel.on_resize(&client_rect);
                         ctrls.header_panel.on_resize(&client_rect);
                    }
                }
            }
            0
        }
        
        WM_CLOSE => {
            DestroyWindow(hwnd);
            0
        }

        WM_DESTROY => {
            if let Some(state) = get_window_state::<AppState>(hwnd) {
                let mut rect: RECT = unsafe { std::mem::zeroed() };
                if GetWindowRect(hwnd, &mut rect) != 0 {
                    state.config.window_x = rect.left;
                    state.config.window_y = rect.top;
                    state.config.window_width = rect.right - rect.left;
                    state.config.window_height = rect.bottom - rect.top;
                }
                if let Some(ctrls) = &state.controls {
                    let idx = SendMessageW(ctrls.action_panel.combo_hwnd(), CB_GETCURSEL, 0, 0);
                   // Index 0 = "As Listed" - save as Xpress8K default
                   state.config.default_algo = match idx {
                       1 => WofAlgorithm::Xpress4K,
                       3 => WofAlgorithm::Xpress16K, 
                       4 => WofAlgorithm::Lzx,
                       _ => WofAlgorithm::Xpress8K,
                   };
                    let force = SendMessageW(ctrls.action_panel.force_hwnd(), BM_GETCHECK, 0, 0);
                    state.config.force_compress = force as u32 == BST_CHECKED;
                }
                state.config.theme = state.theme;
                state.config.enable_force_stop = state.enable_force_stop;
                // state.config.enable_system_guard is already updated via WM_APP+6 or loop above
                state.config.save();
            }
            let ptr = GetWindowLongPtrW(hwnd, GWLP_USERDATA);
            if ptr != 0 {
                let _ = Box::from_raw(ptr as *mut AppState);
                SetWindowLongPtrW(hwnd, GWLP_USERDATA, 0);
            }
            // Force explicit process exit to prevent zombie processes.
            // Bypassing the message loop unwind ensures we don't hang on stubborn dispatch chains.
            std::process::exit(0);
        }

        WM_NCDESTROY => {
            // PostQuitMessage(0); // Moved to WM_DESTROY only
            0
        }

        WM_COPYDATA => {
             let cds = &*(lparam as *const COPYDATASTRUCT);
             if cds.dwData == 0xB00B {
                 let len = (cds.cbData / 2) as usize;
                 let slice = std::slice::from_raw_parts(cds.lpData as *const u16, len);
                 let payload = String::from_utf16_lossy(slice).trim_matches('\0').to_string();
                 let parts: Vec<&str> = payload.split('|').collect();
                 
                 if parts.len() >= 3 {
                     let path = parts[0].to_string();
                     let algo = match parts[1] {
                         "xpress4k" => WofAlgorithm::Xpress4K,
                         "xpress8k" => WofAlgorithm::Xpress8K,
                         "xpress16k" => WofAlgorithm::Xpress16K,
                         "lzx" => WofAlgorithm::Lzx,
                         _ => WofAlgorithm::Xpress8K,
                     };
                     let action = match parts[2] {
                         "decompress" => BatchAction::Decompress,
                         _ => BatchAction::Compress,
                     };
                     
                     if let Some(st) = get_state() {
                         if !st.batch_items.iter().any(|item| item.path == path) {
                             let id = st.add_batch_item(path.clone());
                             if let Some(item) = st.get_batch_item_mut(id) {
                                  item.algorithm = algo;
                                  item.action = action;
                             }
                             
                             if let Some(ctrls) = &st.controls {
                                  SendMessageW(ctrls.action_panel.combo_hwnd(), CB_SETCURSEL, 0, 0);
                                  
                                  // Auto-Start Batch Logic:
                                  // If this is the FIRST item in a potential batch (ipc_active is false), clear selection.
                                  // Otherwise (ipc_active is true), we are appending to the same batch, so KEEP selection.
                                  if !st.ipc_active {
                                      let count = ctrls.file_list.get_item_count();
                                      for i in 0..count {
                                          ctrls.file_list.set_selected(i, false);
                                      }
                                      st.ipc_active = true;
                                  }

                                  if let Some(pos) = st.batch_items.iter().position(|i| i.id == id) {
                                      if let Some(batch_item) = st.batch_items.get(pos) {
                                           ctrls.file_list.add_item(id, batch_item, to_wstring("Calculating..."), to_wstring("Calculating..."), CompressionState::None);
                                           // Select the new item visually
                                           ctrls.file_list.set_selected(pos as i32, true);
                                           // Add to pending IPC list for robust selection at trigger time
                                           st.pending_ipc_ids.push(id);
                                      }
                                  }
                             }
                             
                             let tx = st.tx.clone();
                             let p = path.clone();
                             thread::spawn(move || {
                                  let logical = calculate_path_logical_size(&p);
                                  let disk = calculate_path_disk_size(&p);
                                  let algo = detect_path_algorithm(&p);
                                  let _ = tx.send(UiMessage::BatchItemAnalyzed(id, logical, disk, algo));
                             });
                             
                             SetTimer(hwnd, 2, 500, None);
                         }
                     }
                 }
                 return 1;
             }
             0
        }
        
        WM_DROPFILES => {
            let hdrop = wparam as HDROP;
            if let Some(st) = get_state() {
                process_hdrop(hwnd, hdrop, st);
            }
            DragFinish(hdrop);
            0
        }
        
        WM_SETTINGCHANGE => {
            if let Some(st) = get_state() {
                if st.theme == AppTheme::System {
                     let is_dark = theme::resolve_mode(st.theme);
                     theme::set_window_frame_theme(hwnd, is_dark);
                     if let Some(ctrls) = &mut st.controls {
                         ctrls.update_theme(is_dark, hwnd);
                     }
                     InvalidateRect(hwnd, std::ptr::null(), 1);
                }
            }
            DefWindowProcW(hwnd, msg, wparam, lparam)
        }
        
        WM_NOTIFY => {
            let nmhdr = &*(lparam as *const NMHDR);
            if nmhdr.idFrom == IDC_BATCH_LIST as usize {
                if nmhdr.code == NM_CLICK || nmhdr.code == NM_DBLCLK {
                    let nmia = &*(lparam as *const NMITEMACTIVATE);
                    let row = nmia.iItem;
                    let col = nmia.iSubItem;
                    if row >= 0 {
                        if let Some(st) = get_state() {
                             if col == 0 && nmhdr.code == NM_DBLCLK { // Open Path (Double Click Only)
                                 if let Some(item) = st.batch_items.get(row as usize) {
                                     let path = &item.path;
                                     // format!("/select,\"{}\"", path)
                                     let select_prefix = to_wstring("/select,\"");
                                     let path_w = to_wstring(path);
                                     let suffix = to_wstring("\"");
                                     let args = concat_wstrings(&[&select_prefix, &path_w, &suffix]);
                                     
                                     ShellExecuteW(std::ptr::null_mut(), to_wstring("open").as_ptr(), to_wstring("explorer.exe").as_ptr(), args.as_ptr(), std::ptr::null(), SW_SHOWNORMAL);
                                 }
                             } else if col == 2 && nmhdr.code == NM_DBLCLK { // Cycle Algo (Double Click Only)
                                  if let Some(item) = st.batch_items.get_mut(row as usize) {
                                      item.algorithm = match item.algorithm {
                                          WofAlgorithm::Xpress4K => WofAlgorithm::Xpress8K,
                                          WofAlgorithm::Xpress8K => WofAlgorithm::Xpress16K,
                                          WofAlgorithm::Xpress16K => WofAlgorithm::Lzx,
                                          WofAlgorithm::Lzx => WofAlgorithm::Xpress4K,
                                      };
                                      let name = match item.algorithm {
                                          WofAlgorithm::Xpress4K => "XPRESS4K", WofAlgorithm::Xpress8K => "XPRESS8K",
                                          WofAlgorithm::Xpress16K => "XPRESS16K", WofAlgorithm::Lzx => "LZX",
                                      };
                                      if let Some(ctrls) = &st.controls { ctrls.file_list.update_item_text(row, 2, to_wstring(name)); }
                                  }
                             } else if col == 3 && nmhdr.code == NM_DBLCLK { // Toggle Action (Double Click Only)
                                  if let Some(item) = st.batch_items.get_mut(row as usize) {
                                      item.action = match item.action {
                                          BatchAction::Compress => BatchAction::Decompress,
                                          BatchAction::Decompress => BatchAction::Compress,
                                      };
                                      let name = match item.action {
                                          BatchAction::Compress => "Compress", BatchAction::Decompress => "Decompress",
                                      };
                                      if let Some(ctrls) = &st.controls { ctrls.file_list.update_item_text(row, 3, to_wstring(name)); }
                                  }
                             } else if col == 8 && nmhdr.code == NM_CLICK { // Start/Pause (Single Click)
                                  if let Some(item) = st.batch_items.get_mut(row as usize) {
                                       // Only start if not already running (basic check)
                                       // For now, always allow forcing a restart of that item
                                       let path = item.path.clone();
                                       let action = item.action;
                                       let algo = item.algorithm;
                                       let row_idx = row as usize;
                                       
                                       // Update UI immediately to show visual feedback
                                       if let Some(ctrls) = &st.controls {
                                           ctrls.file_list.update_item_text(row, 7, to_wstring("Starting..."));
                                       }
                                       
                                       // Prepare single item batch
                                       let items = vec![(path, action, row_idx, algo)];
                                       let tx = st.tx.clone();
                                       let state_global = st.global_state.clone();
                                       let force = st.force_compress;
                                       let guard = st.config.enable_system_guard;
                                       let main_hwnd_usize = hwnd as usize;

                                       thread::spawn(move || {
                                           batch_process_worker(items, tx, state_global, force, main_hwnd_usize, guard);
                                       });
                                  }
                             }
                        }
                    }
                }
                
                if nmhdr.code == LVN_KEYDOWN {
                    let nmkd = &*(lparam as *const NMLVKEYDOWN);
                    let vk = nmkd.wVKey as u16;
                    
                    // Handle Delete
                    if vk == VK_DELETE {
                        SendMessageW(hwnd, WM_COMMAND, (IDC_BTN_REMOVE as usize) | ((0 as usize) << 16), 0);
                    }
                    // Handle Ctrl + A
                    else if vk == 0x41 { // 'A' key
                        let ctrl_state = GetKeyState(VK_CONTROL as i32) as u16;
                        if (ctrl_state & 0x8000) != 0 {
                             if let Some(st) = get_state() {
                                 if let Some(ctrls) = &st.controls {
                                     let count = ctrls.file_list.get_item_count();
                                     for i in 0..count {
                                         ctrls.file_list.set_selected(i, true);
                                     }
                                 }
                             }
                        }
                    }
                    // Handle Ctrl + V (Paste)
                    else if vk == 0x56 { // 'V' key
                        let ctrl_state = GetKeyState(VK_CONTROL as i32) as u16;
                        if (ctrl_state & 0x8000) != 0 {
                            if IsClipboardFormatAvailable(15) != 0 {
                                if OpenClipboard(hwnd) != 0 {
                                     let hdrop = GetClipboardData(15) as HDROP;
                                     if !hdrop.is_null() {
                                         if let Some(st) = get_state() {
                                             process_hdrop(hwnd, hdrop, st);
                                         }
                                     }
                                     CloseClipboard();
                                }
                            }
                        }
                    }
                }
                
                if nmhdr.code == LVN_COLUMNCLICK {
                     let nmlv = &*(lparam as *const NMLISTVIEW);
                     let column = nmlv.iSubItem;
                     
                     if let Some(st) = get_state() {
                         // Toggle if same column, else ascending
                         if st.sort_column == column {
                             st.sort_ascending = !st.sort_ascending;
                         } else {
                             st.sort_column = column;
                             st.sort_ascending = true;
                         }
                         
                         if let Some(ctrls) = &st.controls {
                             // Pass the AppState pointer as context
                             let context = st as *const AppState as isize;
                             ctrls.file_list.sort_items(compare_items, context);
                         }
                     }
                }
                
                if nmhdr.code == LVN_ITEMCHANGED {
                     if let Some(st) = get_state() {
                         if let Some(ctrls) = &st.controls {
                             let count = ctrls.file_list.get_selection_count();
                             let text = if count > 0 { "Process Selected" } else { "Process All" };
                             SetWindowTextW(ctrls.action_panel.process_hwnd(), to_wstring(text).as_ptr());
                         }
                     }
                }
            }
            0
        }
        
        WM_CONTEXTMENU => {
            let hwnd_from = wparam as HWND;
            if let Some(st) = get_state() {
                if let Some(ctrls) = &st.controls {
                    if hwnd_from == ctrls.file_list.hwnd() {
                        let selected = ctrls.file_list.get_selected_indices();
                        if !selected.is_empty() {
                            let mut pt: POINT = unsafe { std::mem::zeroed() };
                            GetCursorPos(&mut pt);
                            let menu = CreatePopupMenu();
                            if menu != std::ptr::null_mut() {
                                // Determine status of selected items
                                let mut any_processing = false;
                                let mut any_pending = false;
                                // let mut all_done = true; // Implied if not processing and not pending
                                
                                for &idx in &selected {
                                    if let Some(item) = st.batch_items.get(idx as usize) {
                                        match item.status {
                                            BatchStatus::Processing => {
                                                any_processing = true;
                                            },
                                            BatchStatus::Pending => {
                                                any_pending = true;
                                            },
                                            _ => {}
                                        }
                                    }
                                }

                                if any_processing {
                                    // If anything is running, offer control options
                                    let _ = AppendMenuW(menu, MF_STRING, 1001, to_wstring("Pause").as_ptr());
                                    let _ = AppendMenuW(menu, MF_STRING, 1003, to_wstring("Stop").as_ptr());
                                    let _ = AppendMenuW(menu, MF_STRING, 1004, to_wstring("Remove").as_ptr());
                                    // Note: Stop acts as Cancel/Removing from the active logic usually, 
                                    // but here we allow explicit Remove too which might be cleaner.
                                } else if any_pending {
                                    // If strictly pending (and potentially some done, but no processing), show Start
                                    let _ = AppendMenuW(menu, MF_STRING, 1005, to_wstring("Start").as_ptr());
                                    let _ = AppendMenuW(menu, MF_STRING, 1004, to_wstring("Remove").as_ptr());
                                } else {
                                    // All done or mixed done/error, just Remove
                                    let _ = AppendMenuW(menu, MF_STRING, 1004, to_wstring("Remove").as_ptr());
                                }

                                // Add "Open File Location"
                                let _ = AppendMenuW(menu, MF_STRING, 1006, to_wstring("Open File Location").as_ptr());

                                let _cmd = TrackPopupMenu(menu, TPM_RETURNCMD | TPM_LEFTALIGN, pt.x, pt.y, 0, hwnd, std::ptr::null());
                                DestroyMenu(menu);
                                
                                if _cmd == 1001 { // Pause
                                     // ...
                                } else if _cmd == 1002 { // Resume
                                     // ...
                                } else if _cmd == 1003 { // Stop
                                     SendMessageW(hwnd, WM_COMMAND, IDC_BTN_CANCEL as usize, 0);
                                } else if _cmd == 1004 { // Remove
                                     SendMessageW(hwnd, WM_COMMAND, IDC_BTN_REMOVE as usize, 0);
                                } else if _cmd == 1005 { // Start Selected
                                     // Logic similar to Process All but for selected items
                                    let action_mode_idx = SendMessageW(ctrls.action_panel.action_mode_hwnd(), CB_GETCURSEL, 0, 0);
                                    let _global_algo = st.config.default_algo; // Was compression_algorithm
                                    let _use_as_listed = action_mode_idx == 0; 
                                    
                                    // ...
                                    
                                    let items: Vec<_> = selected.iter().filter_map(|&idx| {
                                        st.batch_items.get(idx as usize).map(|item| {
                                            (item.path.clone(), item.action, idx as usize, item.algorithm)
                                        })
                                    }).collect();
                                    
                                    if !items.is_empty() {
                                        let tx = st.tx.clone();
                                        let state_global = st.global_state.clone();
                                        state_global.store(ProcessingState::Running as u8, Ordering::Relaxed);
                                        let force = st.force_compress;
                                        let guard = st.config.enable_system_guard;
                                        let main_hwnd_usize = hwnd as usize;

                                        // Update UI status to Starting
                                        for &idx in &selected {
                                            if let Some(ctrls) = &st.controls {
                                                ctrls.file_list.update_item_text(idx as i32, 7, to_wstring("Starting..."));
                                            }
                                        }

                                        thread::spawn(move || {
                                            batch_process_worker(items, tx, state_global, force, main_hwnd_usize, guard);
                                        });
                                    }
                                } else if _cmd == 1006 {
                                    // Open file location logic
                                    if let Some(&first_idx) = selected.first() {
                                        if let Some(item) = st.batch_items.get(first_idx as usize) {
                                            let select_prefix = to_wstring("/select,\"");
                                            let path_w = to_wstring(&item.path);
                                            let suffix = to_wstring("\"");
                                            let args = concat_wstrings(&[&select_prefix, &path_w, &suffix]);
                                            
                                            ShellExecuteW(
                                                std::ptr::null_mut(),
                                                to_wstring("open").as_ptr(),
                                                to_wstring("explorer.exe").as_ptr(),
                                                args.as_ptr(),
                                                std::ptr::null(),
                                                SW_SHOWNORMAL
                                            );
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            0
        }
        
        _ => DefWindowProcW(hwnd, msg, wparam, lparam),
    }
}

/// Comparison callback for ListView sorting
/// 
/// # Safety
/// Called by Windows. lParam1/lParam2 are user data (BatchItem IDs).
/// lParamSort is pointer to AppState.
unsafe extern "system" fn compare_items(lparam1: isize, lparam2: isize, lparam_sort: isize) -> i32 {
    let state = &*(lparam_sort as *const AppState);
    let id1 = lparam1 as u32;
    let id2 = lparam2 as u32;
    
    let item1 = state.batch_items.iter().find(|i| i.id == id1);
    let item2 = state.batch_items.iter().find(|i| i.id == id2);
    
    match (item1, item2) {
        (Some(i1), Some(i2)) => {
            let ord = match state.sort_column {
                0 => i1.path.to_lowercase().cmp(&i2.path.to_lowercase()), // Path
                2 => format!("{:?}", i1.algorithm).cmp(&format!("{:?}", i2.algorithm)), // Algo
                3 => format!("{:?}", i1.action).cmp(&format!("{:?}", i2.action)), // Action
                4 => i1.logical_size.cmp(&i2.logical_size), // Logical Size
                5 => i1.disk_size.cmp(&i2.disk_size), // Disk Size
                7 => format!("{:?}", i1.status).cmp(&format!("{:?}", i2.status)), // Status
                _ => CmpOrdering::Equal,
            };
            
            let result = match ord {
                CmpOrdering::Less => -1,
                CmpOrdering::Equal => 0,
                CmpOrdering::Greater => 1,
            };
            
            if state.sort_ascending { result } else { -result }
        },
        _ => 0
    }
}



// IFileOpenDialog Interface Definition (Manual, as windows-sys doesn't wrap COM traits nicely)
// We use raw vtables here.
use std::ffi::c_void;

#[repr(C)]
struct IFileOpenDialogVtbl {
    pub query_interface: unsafe extern "system" fn(*mut c_void, *const GUID, *mut *mut c_void) -> HRESULT,
    pub add_ref: unsafe extern "system" fn(*mut c_void) -> u32,
    pub release: unsafe extern "system" fn(*mut c_void) -> u32,
    // IModalWindow
    pub show: unsafe extern "system" fn(*mut c_void, HWND) -> HRESULT,
    // IFileDialog
    pub set_file_types: unsafe extern "system" fn(*mut c_void, u32, *const c_void) -> HRESULT,
    pub set_file_type_index: unsafe extern "system" fn(*mut c_void, u32) -> HRESULT,
    pub get_file_type_index: unsafe extern "system" fn(*mut c_void, *mut u32) -> HRESULT,
    pub advise: unsafe extern "system" fn(*mut c_void, *mut c_void, *mut u32) -> HRESULT,
    pub unadvise: unsafe extern "system" fn(*mut c_void, u32) -> HRESULT,
    pub set_options: unsafe extern "system" fn(*mut c_void, u32) -> HRESULT,
    pub get_options: unsafe extern "system" fn(*mut c_void, *mut u32) -> HRESULT,
    pub set_default_folder: unsafe extern "system" fn(*mut c_void, *mut c_void) -> HRESULT,
    pub set_folder: unsafe extern "system" fn(*mut c_void, *mut c_void) -> HRESULT,
    pub get_folder: unsafe extern "system" fn(*mut c_void, *mut *mut c_void) -> HRESULT,
    pub get_current_selection: unsafe extern "system" fn(*mut c_void, *mut *mut c_void) -> HRESULT,
    pub set_file_name: unsafe extern "system" fn(*mut c_void, PCWSTR) -> HRESULT,
    pub get_file_name: unsafe extern "system" fn(*mut c_void, *mut PCWSTR) -> HRESULT,
    pub set_title: unsafe extern "system" fn(*mut c_void, PCWSTR) -> HRESULT,
    pub set_ok_button_label: unsafe extern "system" fn(*mut c_void, PCWSTR) -> HRESULT,
    pub set_file_name_label: unsafe extern "system" fn(*mut c_void, PCWSTR) -> HRESULT,
    pub get_result: unsafe extern "system" fn(*mut c_void, *mut *mut c_void) -> HRESULT, // Returns IShellItem
    pub add_place: unsafe extern "system" fn(*mut c_void, *mut c_void, u32) -> HRESULT,
    pub set_default_extension: unsafe extern "system" fn(*mut c_void, PCWSTR) -> HRESULT,
    pub close: unsafe extern "system" fn(*mut c_void, HRESULT) -> HRESULT,
    pub set_client_guid: unsafe extern "system" fn(*mut c_void, *const GUID) -> HRESULT,
    pub clear_client_data: unsafe extern "system" fn(*mut c_void) -> HRESULT,
    pub set_filter: unsafe extern "system" fn(*mut c_void, *mut c_void) -> HRESULT,
    // IFileOpenDialog
    pub get_results: unsafe extern "system" fn(*mut c_void, *mut *mut c_void) -> HRESULT, // Returns IShellItemArray
    pub get_selected_items: unsafe extern "system" fn(*mut c_void, *mut *mut c_void) -> HRESULT,
}

#[repr(C)]
struct IShellItemVtbl {
    pub query_interface: unsafe extern "system" fn(*mut c_void, *const GUID, *mut *mut c_void) -> HRESULT,
    pub add_ref: unsafe extern "system" fn(*mut c_void) -> u32,
    pub release: unsafe extern "system" fn(*mut c_void) -> u32,
    pub bind_to_handler: unsafe extern "system" fn(*mut c_void, *mut c_void, *const GUID, *const GUID, *mut *mut c_void) -> HRESULT,
    pub get_parent: unsafe extern "system" fn(*mut c_void, *mut *mut c_void) -> HRESULT,
    pub get_display_name: unsafe extern "system" fn(*mut c_void, u32, *mut PCWSTR) -> HRESULT,
    pub get_attributes: unsafe extern "system" fn(*mut c_void, u32, *mut u32) -> HRESULT,
    pub compare: unsafe extern "system" fn(*mut c_void, *mut c_void, u32, *mut i32) -> HRESULT,
}

#[repr(C)]
struct IShellItemArrayVtbl {
    pub query_interface: unsafe extern "system" fn(*mut c_void, *const GUID, *mut *mut c_void) -> HRESULT,
    pub add_ref: unsafe extern "system" fn(*mut c_void) -> u32,
    pub release: unsafe extern "system" fn(*mut c_void) -> u32,
    pub bind_to_handler: unsafe extern "system" fn(*mut c_void, *mut c_void, *const GUID, *const GUID, *mut *mut c_void) -> HRESULT,
    pub get_property_store: unsafe extern "system" fn(*mut c_void, u32, *const GUID, *mut *mut c_void) -> HRESULT,
    pub get_property_description_list: unsafe extern "system" fn(*mut c_void, *const GUID, *const GUID, *mut *mut c_void) -> HRESULT,
    pub get_attributes: unsafe extern "system" fn(*mut c_void, u32, u32, *mut c_void) -> HRESULT,
    pub get_count: unsafe extern "system" fn(*mut c_void, *mut u32) -> HRESULT,
    pub get_item_at: unsafe extern "system" fn(*mut c_void, u32, *mut *mut c_void) -> HRESULT,
    pub enum_items: unsafe extern "system" fn(*mut c_void, *mut *mut c_void) -> HRESULT,
}

// GUIDs
const CLSID_FILE_OPEN_DIALOG: GUID = GUID { data1: 0xDC1C5A9C, data2: 0xE88A, data3: 0x4DDE, data4: [0xA5, 0xA1, 0x60, 0xF8, 0x2A, 0x20, 0xAE, 0xF7] };
const IID_IFILE_OPEN_DIALOG: GUID = GUID { data1: 0xd57c7288, data2: 0xd4ad, data3: 0x4768, data4: [0xbe, 0x02, 0x9d, 0x96, 0x95, 0x32, 0xd9, 0x60] };

const FOS_PICKFOLDERS: u32 = 0x20;
const FOS_FORCEFILESYSTEM: u32 = 0x40;
const FOS_ALLOWMULTISELECT: u32 = 0x200;
const SIGDN_FILESYSPATH: u32 = 0x80058000;

/// Pick files (multi-select)
unsafe fn pick_files() -> Result<Vec<String>, HRESULT> {
    let mut p_dialog: *mut c_void = std::ptr::null_mut();
    let hr = CoCreateInstance(&CLSID_FILE_OPEN_DIALOG, std::ptr::null_mut(), CLSCTX_ALL, &IID_IFILE_OPEN_DIALOG, &mut p_dialog);
    if hr != 0 { return Err(hr); }

    let dialog = p_dialog as *mut *mut IFileOpenDialogVtbl;
    let vtbl = (*dialog).as_ref().unwrap();

    let mut options = 0;
    (vtbl.get_options)(p_dialog, &mut options);
    (vtbl.set_options)(p_dialog, options | FOS_FORCEFILESYSTEM | FOS_ALLOWMULTISELECT);
    
    let hr = (vtbl.show)(p_dialog, std::ptr::null_mut()); // HWND owner = null
    if hr != 0 {
        (vtbl.release)(p_dialog);
        return Err(hr);
    }

    let mut p_results: *mut c_void = std::ptr::null_mut();
    let hr = (vtbl.get_results)(p_dialog, &mut p_results);
    if hr != 0 {
        (vtbl.release)(p_dialog);
        return Err(hr);
    }

    let results = p_results as *mut *mut IShellItemArrayVtbl;
    let results_vtbl = (*results).as_ref().unwrap();

    let mut count = 0;
    (results_vtbl.get_count)(p_results, &mut count);
    
    let mut paths = Vec::new();
    for i in 0..count {
        let mut p_item: *mut c_void = std::ptr::null_mut();
        if (results_vtbl.get_item_at)(p_results, i, &mut p_item) == 0 {
            let item = p_item as *mut *mut IShellItemVtbl;
            let item_vtbl = (*item).as_ref().unwrap();
            
            let mut name_ptr: PCWSTR = std::ptr::null();
            if (item_vtbl.get_display_name)(p_item, SIGDN_FILESYSPATH, &mut name_ptr) == 0 && !name_ptr.is_null() {
                let len = (0..).take_while(|&i| *name_ptr.offset(i) != 0).count();
                let slice = std::slice::from_raw_parts(name_ptr, len);
                if let Ok(path) = String::from_utf16(slice) {
                    paths.push(path);
                }
                CoTaskMemFree(name_ptr as *mut _);
            }
            (item_vtbl.release)(p_item);
        }
    }

    (results_vtbl.release)(p_results);
    (vtbl.release)(p_dialog);

    Ok(paths)
}

/// Pick folder (single folder selection)
unsafe fn pick_folder() -> Result<String, HRESULT> {
    let mut p_dialog: *mut c_void = std::ptr::null_mut();
    let hr = CoCreateInstance(&CLSID_FILE_OPEN_DIALOG, std::ptr::null_mut(), CLSCTX_ALL, &IID_IFILE_OPEN_DIALOG, &mut p_dialog);
    if hr != 0 { return Err(hr); }

    let dialog = p_dialog as *mut *mut IFileOpenDialogVtbl;
    let vtbl = (*dialog).as_ref().unwrap();

    let mut options = 0;
    (vtbl.get_options)(p_dialog, &mut options);
    (vtbl.set_options)(p_dialog, options | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM);
    
    let hr = (vtbl.show)(p_dialog, std::ptr::null_mut()); // HWND owner = null
    if hr != 0 {
        (vtbl.release)(p_dialog);
        return Err(hr);
    }

    let mut p_item: *mut c_void = std::ptr::null_mut();
    let hr = (vtbl.get_result)(p_dialog, &mut p_item);
    if hr != 0 {
        (vtbl.release)(p_dialog);
        return Err(hr);
    }

    let item = p_item as *mut *mut IShellItemVtbl;
    let item_vtbl = (*item).as_ref().unwrap();
    
    let mut name_ptr: PCWSTR = std::ptr::null();
    let mut path = String::new();
    
    if (item_vtbl.get_display_name)(p_item, SIGDN_FILESYSPATH, &mut name_ptr) == 0 && !name_ptr.is_null() {
        let len = (0..).take_while(|&i| *name_ptr.offset(i) != 0).count();
        let slice = std::slice::from_raw_parts(name_ptr, len);
        if let Ok(s) = String::from_utf16(slice) {
            path = s;
        }
        CoTaskMemFree(name_ptr as *mut _);
    }

    (item_vtbl.release)(p_item);
    (vtbl.release)(p_dialog);

    Ok(path)
}

/// Helper function to process HDROP handle (extract paths and add to batch)
/// Used by both WM_DROPFILES and Ctrl+V (Clipboard)
unsafe fn process_hdrop(_hwnd: HWND, hdrop: HDROP, st: &mut AppState) {
    let count = DragQueryFileW(hdrop, 0xFFFFFFFF, std::ptr::null_mut(), 0);
    let mut paths = Vec::new();
    let mut buffer = [0u16; 1024];
    
    for i in 0..count {
        let len = DragQueryFileW(hdrop, i, buffer.as_mut_ptr(), 1024);
        if len > 0 {
            let s = String::from_utf16_lossy(&buffer[..len as usize]);
            paths.push(s);
        }
    }
    
    if let Some(ctrls) = &st.controls {
        let w_msg = to_wstring("Analyzing dropped files...");
        SetWindowTextW(ctrls.status_bar.label_hwnd(), w_msg.as_ptr());
    }
    let mut items_to_analyze = Vec::new();
    for path in paths {
        if !st.batch_items.iter().any(|item| item.path == path) {
            let id = st.add_batch_item(path.clone());
            if let Some(ctrls) = &st.controls {
                 if let Some(batch_item) = st.batch_items.iter().find(|i| i.id == id) {
                     ctrls.file_list.add_item(id, batch_item, to_wstring("Calculating..."), to_wstring("Calculating..."), CompressionState::None);
                 }
            }
            items_to_analyze.push((id, path));
        }
    }
    
    if items_to_analyze.is_empty() { return; }

    let tx = st.tx.clone();
    thread::spawn(move || {
        for (id, path) in items_to_analyze {
             let logical = calculate_path_logical_size(&path);
             let disk = calculate_path_disk_size(&path);
             let algo = detect_path_algorithm(&path);
             let _ = tx.send(UiMessage::BatchItemAnalyzed(id, logical, disk, algo));
        }
        let _ = tx.send(UiMessage::Status(to_wstring("Ready.")));
    });
}