qr-scanner 0.2.1

A cross-platform QR code scanner with screen capture and clipboard integration
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
mod qr_scanner;
#[cfg(target_os = "windows")]
mod desktop_cropper;
#[cfg(target_os = "windows")]
mod overlay;

use arboard::{Clipboard, ImageData};
use eframe::egui;
use image::ImageBuffer;

pub fn convert_bgra_to_rgba(bgra: &[u8]) -> Vec<u8> {
    let mut bytes = Vec::with_capacity(bgra.len());
    let mut i = 0;
    while i + 3 < bgra.len() {
        let b = bgra[i];
        let g = bgra[i + 1];
        let r = bgra[i + 2];
        let a = bgra[i + 3];
        bytes.push(r);
        bytes.push(g);
        bytes.push(b);
        bytes.push(a);
        i += 4;
    }
    bytes
}

pub fn rasterize_svg(svg_text: &str) -> Option<image::DynamicImage> {
    eprintln!("Rasterizing SVG...");
    let opt = usvg::Options::default();
    let tree = match usvg::Tree::from_str(svg_text, &opt) {
        Ok(tree) => tree,
        Err(e) => {
            eprintln!("Failed to parse SVG: {:?}", e);
            return None;
        }
    };

    let size = tree.size();
    let width = size.width().ceil() as u32;
    let height = size.height().ceil() as u32;
    eprintln!("SVG size: {}x{}", width, height);

    if width == 0 || height == 0 {
        return None;
    }

    let mut pixmap = tiny_skia::Pixmap::new(width, height)?;

    let white = tiny_skia::Color::from_rgba(1.0, 1.0, 1.0, 1.0).unwrap();
    pixmap.fill(white);

    resvg::render(&tree, tiny_skia::Transform::identity(), &mut pixmap.as_mut());

    let pixels = pixmap.data().to_vec();
    if let Some(buf) = image::ImageBuffer::from_raw(width, height, pixels) {
        let debug_path = std::env::temp_dir().join("qr_rasterized.png");
        let _ = buf.save(&debug_path);
        eprintln!("Saved rasterized SVG to: {:?}", debug_path);

        return Some(image::DynamicImage::ImageRgba8(buf));
    }

    None
}

pub fn decode_html_entities(s: &str) -> String {
    s.replace("&amp;", "&")
        .replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
        .replace("&#39;", "'")
        .replace("&apos;", "'")
}

pub fn extract_img_url_from_html(html: &str) -> Option<String> {
    if let Some(start) = html.find("src=\"") {
        let start = start + 5;
        if let Some(end) = html[start..].find('"') {
            let raw = html[start..start + end].to_string();
            return Some(decode_html_entities(&raw));
        }
    }
    if let Some(start) = html.find("src='") {
        let start = start + 5;
        if let Some(end) = html[start..].find('\'') {
            let raw = html[start..start + end].to_string();
            return Some(decode_html_entities(&raw));
        }
    }
    None
}

pub fn download_image_from_url(url: &str) -> Option<image::DynamicImage> {
    eprintln!("Downloading image from URL: {}", url);

    let temp_path = std::env::temp_dir().join("qr_downloaded");

    #[cfg(target_os = "windows")]
    {
        let ps_script = format!(
            "Invoke-WebRequest -Uri '{}' -OutFile '{}' -UseBasicParsing",
            url,
            temp_path.display()
        );

        let output = std::process::Command::new("powershell")
            .args(&["-NoProfile", "-NonInteractive", "-Command", &ps_script])
            .output()
            .ok()?;
        if !output.status.success() || !temp_path.exists() {
            eprintln!("PowerShell download failed");
            return None;
        }
    }

    #[cfg(not(target_os = "windows"))]
    {
        let output = std::process::Command::new("curl")
            .args(&["-sL", "-H", "User-Agent: Mozilla/5.0", "-o"])
            .arg(&temp_path)
            .arg(url)
            .status()
            .ok()?;
        if !output.success() || !temp_path.exists() {
            eprintln!("curl download failed");
            return None;
        }
    }

    eprintln!("Downloaded to {:?}", temp_path);

    // Read downloaded bytes to detect actual content type
    let data = match std::fs::read(&temp_path) {
        Ok(d) => d,
        Err(e) => {
            eprintln!("Failed to read downloaded file: {:?}", e);
            let _ = std::fs::remove_file(&temp_path);
            return None;
        }
    };

    let _ = std::fs::remove_file(&temp_path);

    // Check if it's actually SVG (regardless of URL extension)
    if let Ok(text) = String::from_utf8(data.clone()) {
        let trimmed = text.trim();
        if trimmed.starts_with("<?xml") || trimmed.starts_with("<svg") || trimmed.contains("<svg") {
            eprintln!("Downloaded content is SVG, rasterizing...");
            return rasterize_svg(&text);
        }
    }

    // Try loading as raster image
    match image::load_from_memory(&data) {
        Ok(img) => {
            eprintln!("Successfully loaded downloaded image!");
            Some(img)
        }
        Err(e) => {
            eprintln!("Failed to load downloaded image: {:?}", e);
            None
        }
    }
}

struct QrScannerApp {
    result_text: String,
    history: Vec<String>,
    auto_copy: bool,
    debug_info: String,
}

impl QrScannerApp {
    fn new() -> Self {
        Self {
            result_text: String::new(),
            history: Vec::new(),
            auto_copy: true,
            debug_info: "Ready".to_string(),
        }
    }

    fn copy_to_clipboard(text: &str) {
        if let Ok(mut clipboard) = Clipboard::new() {
            let _ = clipboard.set_text(text.to_string());
        }
    }

    fn capture_screen() -> Option<image::DynamicImage> {
        let temp_path = "/tmp/qr_scan_capture.png";
        
        // macOS: screencapture (interactive area selection)
        #[cfg(target_os = "macos")]
        {
            use std::process::Command;

            // Try interactive capture to file
            eprintln!("Trying screencapture -i to file...");
            let _ = std::fs::remove_file(temp_path);
            
            let status = Command::new("screencapture")
                .arg("-i")
                .arg(temp_path)
                .status();
            let success = status.map(|s| s.success()).unwrap_or(false);
            let file_exists = std::path::Path::new(temp_path).exists();
            eprintln!("screencapture -i: success={}, file_exists={}", success, file_exists);
            
            if success && file_exists {
                match image::open(temp_path) {
                    Ok(img) => {
                        let _ = std::fs::remove_file(temp_path);
                        eprintln!("Captured image: {}x{}", img.width(), img.height());
                        return Some(img);
                    }
                    Err(e) => {
                        eprintln!("Failed to open image: {:?}", e);
                        let _ = std::fs::remove_file(temp_path);
                    }
                }
            }

            // Fallback: full screen capture
            eprintln!("Trying full screen capture...");
            let _ = std::fs::remove_file(temp_path);
            if Command::new("screencapture")
                .arg(temp_path)
                .status()
                .map(|s| s.success())
                .unwrap_or(false)
                && std::path::Path::new(temp_path).exists()
            {
                match image::open(temp_path) {
                    Ok(img) => {
                        let _ = std::fs::remove_file(temp_path);
                        return Some(img);
                    }
                    Err(e) => {
                        eprintln!("Failed to open full screen: {:?}", e);
                        let _ = std::fs::remove_file(temp_path);
                    }
                }
            }
            
            eprintln!("All capture attempts failed");
        }

        // Linux: GNOME (area selection)
        #[cfg(target_os = "linux")]
        if std::process::Command::new("gnome-screenshot")
            .arg("-a")
            .arg("-f")
            .arg(temp_path)
            .output()
            .map(|o| o.status.success() && std::path::Path::new(temp_path).exists())
            .unwrap_or(false)
        {
            let img = image::open(temp_path).ok();
            let _ = std::fs::remove_file(temp_path);
            return img;
        }

        // Linux: KDE Plasma (area selection)
        #[cfg(target_os = "linux")]
        if std::process::Command::new("spectacle")
            .arg("-b")
            .arg("-o")
            .arg(temp_path)
            .output()
            .map(|o| o.status.success() && std::path::Path::new(temp_path).exists())
            .unwrap_or(false)
        {
            let img = image::open(temp_path).ok();
            let _ = std::fs::remove_file(temp_path);
            return img;
        }

        // Linux: Xfce
        #[cfg(target_os = "linux")]
        if std::process::Command::new("xfce4-screenshooter")
            .arg("-r")
            .arg("-s")
            .arg(temp_path)
            .output()
            .map(|o| o.status.success() && std::path::Path::new(temp_path).exists())
            .unwrap_or(false)
        {
            let img = image::open(temp_path).ok();
            let _ = std::fs::remove_file(temp_path);
            return img;
        }

        // Linux: Sway/Wayland with grim + slop
        #[cfg(target_os = "linux")]
        if let Ok(slop_out) = std::process::Command::new("slop").output() {
            if slop_out.status.success() {
                let geom = String::from_utf8_lossy(&slop_out.stdout);
                let parts: Vec<&str> = geom.split_whitespace().collect();
                if parts.len() >= 4 {
                    let x = parts[0];
                    let y = parts[1];
                    let w = parts[2];
                    let h = parts[3];

                    if std::process::Command::new("grim")
                        .arg("-g")
                        .arg(format!("{} {}x{}", format!("{},{}", x, y), w, h))
                        .arg(temp_path)
                        .output()
                        .map(|o| o.status.success() && std::path::Path::new(temp_path).exists())
                        .unwrap_or(false)
                    {
                        let img = image::open(temp_path).ok();
                        let _ = std::fs::remove_file(temp_path);
                        return img;
                    }
                }
            }
        }

        // Linux fallback: full screen gnome-screenshot
        #[cfg(target_os = "linux")]
        if std::process::Command::new("gnome-screenshot")
            .arg("-f")
            .arg(temp_path)
            .output()
            .map(|o| o.status.success() && std::path::Path::new(temp_path).exists())
            .unwrap_or(false)
        {
            let img = image::open(temp_path).ok();
            let _ = std::fs::remove_file(temp_path);
            return img;
        }

        // Linux fallback: scrot
        #[cfg(target_os = "linux")]
        if std::process::Command::new("scrot")
            .arg(temp_path)
            .output()
            .map(|o| o.status.success() && std::path::Path::new(temp_path).exists())
            .unwrap_or(false)
        {
            let img = image::open(temp_path).ok();
            let _ = std::fs::remove_file(temp_path);
            return img;
        }

        // Windows: use desktop cropper for region selection
        #[cfg(target_os = "windows")]
        {
            return desktop_cropper::start_snapshot_cropper();
        }

        // All screenshot tools failed
        None
    }

    fn start_scan(&mut self) {
        self.debug_info = "Select area to capture...".to_string();

        match Self::capture_screen() {
            Some(img) => {
                eprintln!("capture_screen returned image: {}x{}", img.width(), img.height());
                match qr_scanner::scan_image(&img) {
                    Ok(result) => {
                        self.result_text = result.text.clone();
                        self.history.push(result.text);
                        if self.auto_copy {
                            Self::copy_to_clipboard(&self.result_text);
                        }
                        self.debug_info = "QR scanned from screenshot!".to_string();
                    }
                    Err(e) => {
                        self.debug_info = format!(
                            "No QR found in screenshot: {}. Try 'Open File'.",
                            e
                        );
                    }
                }
            }
            None => {
                eprintln!("capture_screen returned None");
                self.debug_info =
                    "Screenshot not supported. Use 'Paste Image' or 'Open File'."
                        .to_string();
            }
        }
    }

    fn paste_from_clipboard(&mut self) {
        self.debug_info = "Checking clipboard...".to_string();

        #[cfg(target_os = "linux")]
        {
            // Try xclip for X11
            if let Ok(output) = std::process::Command::new("sh")
                .arg("-c")
                .arg("xclip -selection clipboard -t image/png -o 2>/dev/null | base64")
                .output()
            {
                if !output.stdout.is_empty() {
                    if let Ok(png_data) = base64::Engine::decode(
                        &base64::engine::general_purpose::STANDARD,
                        &output.stdout,
                    ) {
                        if let Ok(img) = image::load_from_memory(&png_data) {
                            self.debug_info = "Loaded image via xclip!".to_string();
                            match qr_scanner::scan_image(&img) {
                                Ok(result) => {
                                    self.result_text = result.text.clone();
                                    self.history.push(result.text);
                                    if self.auto_copy {
                                        Self::copy_to_clipboard(&self.result_text);
                                    }
                                    self.debug_info =
                                        "QR scanned from clipboard!".to_string();
                                    return;
                                }
                                Err(e) => {
                                    self.result_text = format!("No QR code: {}", e);
                                    self.debug_info = format!("No QR found: {}", e);
                                    return;
                                }
                            }
                        }
                    }
                }
            }

            // Try wl-paste for Wayland
            if let Ok(output) = std::process::Command::new("sh")
                .arg("-c")
                .arg("wl-paste --type image/png 2>/dev/null | base64")
                .output()
            {
                if !output.stdout.is_empty() {
                    if let Ok(png_data) = base64::Engine::decode(
                        &base64::engine::general_purpose::STANDARD,
                        &output.stdout,
                    ) {
                        if let Ok(img) = image::load_from_memory(&png_data) {
                            self.debug_info = "Loaded image via wl-paste!".to_string();
                            match qr_scanner::scan_image(&img) {
                                Ok(result) => {
                                    self.result_text = result.text.clone();
                                    self.history.push(result.text);
                                    if self.auto_copy {
                                        Self::copy_to_clipboard(&self.result_text);
                                    }
                                    self.debug_info =
                                        "QR scanned from clipboard!".to_string();
                                    return;
                                }
                                Err(e) => {
                                    self.result_text = format!("No QR code: {}", e);
                                    self.debug_info = format!("No QR found: {}", e);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }

        // Cross-platform: arboard clipboard (bitmap image)
        if let Ok(mut clipboard) = Clipboard::new() {
            match clipboard.get_image() {
                Ok(img_data) => {
                    eprintln!("arboard get_image succeeded: {}x{} ({} bytes)", img_data.width, img_data.height, img_data.bytes.len());
                    self.scan_image_data(img_data);
                    return;
                }
                Err(e) => {
                    eprintln!("arboard get_image error: {:?}", e);
                }
            }

            // arboard bitmap failed — try reading clipboard as text (SVG/HTML)
            match clipboard.get_text() {
                Ok(text) => {
                    let trimmed = text.trim();
                    eprintln!("arboard get_text succeeded ({} bytes), first 500 chars: {:?}", text.len(), trimmed.chars().take(500).collect::<String>());

                    // Check if it's HTML with an img src
                    let lower = trimmed.to_lowercase();
                    if lower.contains("<html") || lower.contains("<img") {
                        eprintln!("Detected HTML in clipboard, extracting image URL...");
                        if let Some(img_url) = extract_img_url_from_html(&text) {
                            eprintln!("Found image URL: {}", img_url);
                            if let Some(img) = download_image_from_url(&img_url) {
                                eprintln!("Downloaded and loaded image!");
                                match qr_scanner::scan_image(&img) {
                                    Ok(result) => {
                                        self.result_text = result.text.clone();
                                        self.history.push(result.text);
                                        if self.auto_copy {
                                            Self::copy_to_clipboard(&self.result_text);
                                        }
                                        self.debug_info =
                                            "QR scanned from HTML clipboard!".to_string();
                                        return;
                                    }
                                    Err(e) => {
                                        self.result_text = format!("No QR code: {}", e);
                                        self.debug_info = format!("No QR found: {}", e);
                                        return;
                                    }
                                }
                            } else {
                                self.debug_info = format!(
                                    "Found image URL but download failed: {}",
                                    img_url
                                );
                            }
                        } else {
                            self.debug_info = "HTML in clipboard but no img src found.".to_string();
                        }
                    }

                    // Check if it's SVG content
                    let is_svg = lower.contains("<svg")
                        || lower.contains("</svg>")
                        || lower.contains("<?xml")
                            && (lower.contains("xmlns") && lower.contains("svg"))
                        || (lower.contains("xmlns=") && lower.contains("viewbox="));

                    eprintln!("Is SVG detected: {}", is_svg);

                    if is_svg {
                        eprintln!("Detected SVG in clipboard ({} bytes), rasterizing...", text.len());
                        if let Some(img) = rasterize_svg(&text) {
                            eprintln!("SVG rasterized successfully!");
                            match qr_scanner::scan_image(&img) {
                                Ok(result) => {
                                    self.result_text = result.text.clone();
                                    self.history.push(result.text);
                                    if self.auto_copy {
                                        Self::copy_to_clipboard(&self.result_text);
                                    }
                                    self.debug_info =
                                        "QR scanned from SVG clipboard!".to_string();
                                    return;
                                }
                                Err(e) => {
                                    self.result_text = format!("No QR code in SVG: {}", e);
                                    self.debug_info = format!("No QR found in SVG: {}", e);
                                    return;
                                }
                            }
                        } else {
                            self.debug_info = "SVG detected in clipboard but rasterization failed.".to_string();
                            return;
                        }
                    }

                    eprintln!("Clipboard text is not SVG or HTML with image.");
                }
                Err(e) => {
                    eprintln!("arboard get_text error: {:?}", e);
                }
            }
        }

        // Windows: try PNG format from clipboard
        #[cfg(target_os = "windows")]
        {
            if let Some(img) = Self::read_png_from_clipboard_windows() {
                match qr_scanner::scan_image(&img) {
                    Ok(result) => {
                        self.result_text = result.text.clone();
                        self.history.push(result.text);
                        if self.auto_copy {
                            Self::copy_to_clipboard(&self.result_text);
                        }
                        self.debug_info = "QR scanned from clipboard!".to_string();
                        return;
                    }
                    Err(e) => {
                        self.result_text = format!("No QR code: {}", e);
                        self.debug_info = format!("No QR found: {}", e);
                        return;
                    }
                }
            }
        }

        // macOS: try reading HTML/SVG from clipboard via osascript/AppKit
        #[cfg(target_os = "macos")]
        {
            if let Some(img) = Self::read_html_or_svg_from_clipboard_macos() {
                match qr_scanner::scan_image(&img) {
                    Ok(result) => {
                        self.result_text = result.text.clone();
                        self.history.push(result.text);
                        if self.auto_copy {
                            Self::copy_to_clipboard(&self.result_text);
                        }
                        self.debug_info = "QR scanned from clipboard!".to_string();
                        return;
                    }
                    Err(e) => {
                        self.result_text = format!("No QR code: {}", e);
                        self.debug_info = format!("No QR found: {}", e);
                        return;
                    }
                }
            }
        }

        #[cfg(target_os = "macos")]
        {
            self.debug_info =
                "No image in clipboard. Copy an image (Cmd+C in browser) then paste."
                    .to_string();
        }
        #[cfg(target_os = "linux")]
        {
            if std::env::var("WAYLAND_DISPLAY").is_ok() {
                self.debug_info = "Wayland: Right-click → 'Copy Image', then Ctrl+V \
                     or use 'Open File'."
                    .to_string();
            } else if std::env::var("DISPLAY").is_ok() {
                self.debug_info =
                    "X11: Make sure xclip is installed: sudo apt install xclip"
                        .to_string();
            } else {
                self.debug_info = "No image in clipboard. Use 'Open File'.".to_string();
            }
        }
        #[cfg(not(any(target_os = "macos", target_os = "linux")))]
        {
            self.debug_info = "No image in clipboard. Use 'Open File'.".to_string();
        }
    }

    fn scan_image_data(&mut self, img_data: ImageData) {
        let width = img_data.width;
        let height = img_data.height;

        // Debug: print first few pixels
        let sample = img_data.bytes.iter().take(16).collect::<Vec<_>>();
        eprintln!("Clipboard raw: {}x{}, first bytes: {:02X?}", width, height, sample);

        let bytes = convert_bgra_to_rgba(&img_data.bytes);

        self.debug_info = format!("Got image: {}x{}", width, height);

        if let Some(img) = ImageBuffer::<image::Rgba<u8>, _>::from_raw(
            width as u32,
            height as u32,
            bytes,
        ) {
            // Debug: save image to temp file to verify
            let debug_path = std::env::temp_dir().join("qr_debug_clipboard.png");
            if img.save(&debug_path).is_ok() {
                eprintln!("Saved debug image to: {:?}", debug_path);
            }

            let dyn_img = image::DynamicImage::ImageRgba8(img);
            match qr_scanner::scan_image(&dyn_img) {
                Ok(result) => {
                    self.result_text = result.text.clone();
                    self.history.push(result.text);
                    if self.auto_copy {
                        Self::copy_to_clipboard(&self.result_text);
                    }
                    self.debug_info = "QR code scanned from clipboard!".to_string();
                }
                Err(e) => {
                    self.result_text = format!("No QR code in image: {}", e);
                    self.debug_info = format!("No QR found: {}", e);
                }
            }
        } else {
            self.result_text = "Failed to process image data".to_string();
            self.debug_info = "Image format not supported".to_string();
        }
    }

    #[cfg(target_os = "windows")]
    fn read_png_from_clipboard_windows() -> Option<image::DynamicImage> {
        eprintln!("Trying clipboard-win to read image...");
        use clipboard_win::raw::{get_vec, open, close, EnumFormats};

        let mut formats = Vec::new();
        if open().is_ok() {
            EnumFormats::new().for_each(|fmt| {
                formats.push(fmt);
            });
            let _ = close();
            eprintln!("Found {} clipboard formats", formats.len());

            for fmt in formats {
                if open().is_ok() {
                    let mut data = Vec::new();
                    let result = get_vec(fmt, &mut data);
                    let _ = close();
                    if result.is_ok() && !data.is_empty() {
                        eprintln!("Format {}: got {} bytes", fmt, data.len());

                        // Check for PNG
                        if data.len() > 8 && data[0..8] == [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] {
                            if let Ok(img) = image::load_from_memory(&data) {
                                return Some(img);
                            }
                        }
                        // Check for JPEG
                        if data.len() > 2 && data[0..2] == [0xFF, 0xD8] {
                            if let Ok(img) = image::load_from_memory(&data) {
                                return Some(img);
                            }
                        }
                        // Check for BMP
                        if data.len() > 2 && data[0..2] == [0x42, 0x4D] {
                            if let Ok(img) = image::load_from_memory(&data) {
                                return Some(img);
                            }
                        }
                        // Check for SVG text
                        if let Ok(text) = String::from_utf8(data.clone()) {
                            let t = text.trim();
                            let first_200 = text.chars().take(200).collect::<String>();
                            eprintln!(" Text content (first 200): {:?}", first_200);

                            // Check if it's HTML with img src
                            if t.contains("<html") || t.contains("<img") {
                                eprintln!(" Detected HTML, trying to extract image URL...");
                            if let Some(img_url) = extract_img_url_from_html(&text) {
                                eprintln!(" Found image URL: {}", img_url);
                                if let Some(img) = download_image_from_url(&img_url) {
                                        eprintln!(" Downloaded and loaded image!");
                                        return Some(img);
                                    }
                                }
                            }

                            // Also check for plain SVG (only if HTML didn't work)
                            let is_svg = t.contains("<svg") ||
                                t.contains("</svg>") ||
                                t.contains("xmlns=") ||
                                t.contains("viewBox=");

                            eprintln!(" Is SVG detected: {}", is_svg);

                            if is_svg {
                                eprintln!(" Detected SVG text ({} bytes), rasterizing...", data.len());
                                if let Some(img) = rasterize_svg(&text) {
                                    return Some(img);
                                } else {
                                    eprintln!(" SVG rasterization failed!");
                                }
                            }
                        }
                        // Try generic image load
                        if let Ok(img) = image::load_from_memory(&data) {
                            return Some(img);
                        }
                    }
                } else {
                    let _ = close();
                }
            }
        } else {
            eprintln!("Failed to open clipboard");
        }

        eprintln!("No supported image format found on clipboard");
        None
    }

    #[cfg(target_os = "macos")]
    fn read_html_or_svg_from_clipboard_macos() -> Option<image::DynamicImage> {
        use std::process::Command;

        eprintln!("Trying macOS pasteboard for HTML/SVG...");

        let types_script = r#"
use framework "AppKit"
set pb to current application's NSPasteboard's generalPasteboard()
set types to pb's types() as list
set output to ""
repeat with t in types
    set output to output & (t as text) & ","
end repeat
return output
"#;

        let types_output = Command::new("osascript")
            .args(&["-e", types_script])
            .output()
            .ok()?;

        let types_str = String::from_utf8_lossy(&types_output.stdout);
        let types: Vec<&str> = types_str.trim().split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).collect();
        eprintln!("macOS pasteboard types: {:?}", types);

        let has_html = types.iter().any(|t| *t == "public.html" || *t == "Apple HTML pasteboard type");
        let has_svg = types.iter().any(|t| *t == "public.svg-image" || *t == "public.svg");
        let has_tiff = types.iter().any(|t| *t == "public.tiff" || *t == "public.png");

        if has_html {
            let html_script = r#"
use framework "AppKit"
set pb to current application's NSPasteboard's generalPasteboard()
set htmlData to pb's dataForType:"public.html"
if htmlData is missing value then
    set htmlData to pb's dataForType:"Apple HTML pasteboard type"
end if
if htmlData is not missing value then
    set htmlString to (current application's NSString's alloc()'s initWithData:htmlData encoding:4) as text
    return htmlString
end if
return ""
"#;
            if let Ok(output) = Command::new("osascript").args(&["-e", html_script]).output() {
                let html = String::from_utf8_lossy(&output.stdout).trim().to_string();
                eprintln!("macOS HTML clipboard ({} bytes), first 500: {:?}", html.len(), html.chars().take(500).collect::<String>());

                if !html.is_empty() {
                    let lower = html.to_lowercase();

                    if lower.contains("<svg") {
                        eprintln!("Found SVG in HTML clipboard!");
                        if let Some(img) = rasterize_svg(&html) {
                            return Some(img);
                        }
                    }

                    if lower.contains("<img") {
                        eprintln!("Found <img> in HTML clipboard, extracting src...");
                        if let Some(img_url) = extract_img_url_from_html(&html) {
                            eprintln!("Extracted image URL: {}", img_url);
                            if let Some(img) = download_image_from_url(&img_url) {
                                eprintln!("Downloaded image from clipboard URL!");
                                return Some(img);
                            } else {
                                eprintln!("Failed to download image from URL: {}", img_url);
                            }
                        } else {
                            eprintln!("<img> found but no src attribute");
                        }
                    }
                }
            }
        }

        if has_svg {
            let svg_script = r#"
use framework "AppKit"
set pb to current application's NSPasteboard's generalPasteboard()
set svgData to pb's dataForType:"public.svg-image"
if svgData is missing value then
    set svgData to pb's dataForType:"public.svg"
end if
if svgData is not missing value then
    set svgString to (current application's NSString's alloc()'s initWithData:svgData encoding:4) as text
    return svgString
end if
return ""
"#;
            if let Ok(output) = Command::new("osascript").args(&["-e", svg_script]).output() {
                let svg = String::from_utf8_lossy(&output.stdout).trim().to_string();
                eprintln!("macOS SVG clipboard ({} bytes)", svg.len());
                if !svg.is_empty() && svg.contains("<svg") {
                    eprintln!("Found SVG in pasteboard!");
                    if let Some(img) = rasterize_svg(&svg) {
                        return Some(img);
                    }
                }
            }
        }

        if has_tiff {
            let tmp_out = std::env::temp_dir().join("qr_clipboard_macos.png");
            let tmp_out_str = tmp_out.display().to_string();
            let img_script = format!(r#"
use framework "AppKit"
set pb to current application's NSPasteboard's generalPasteboard()
set imgData to pb's dataForType:"public.tiff"
if imgData is missing value then
    set imgData to pb's dataForType:"public.png"
end if
if imgData is not missing value then
    set nsImage to current application's NSImage's alloc()'s initWithData:imgData
    if nsImage is not missing value then
        set tiffData to nsImage's TIFFRepresentation()
        set nsBitmap to current application's NSBitmapImageRep's alloc()'s initWithData:tiffData
        set pngData to nsBitmap's representationUsingType:(current application's NSPNGFileType) |properties|:(missing value)
        pngData's writeToFile:"{}" atomically:true
        return "{}"
    end if
end if
return ""
"#, tmp_out_str, tmp_out_str);
            if let Ok(output) = Command::new("osascript").args(&["-e", &img_script]).output() {
                let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
                if !path.is_empty() && tmp_out.exists() {
                    eprintln!("macOS: saved clipboard image to {}", path);
                    if let Ok(img) = image::open(&tmp_out) {
                        let _ = std::fs::remove_file(&tmp_out);
                        return Some(img);
                    }
                    let _ = std::fs::remove_file(&tmp_out);
                } else {
                    let err = String::from_utf8_lossy(&output.stderr);
                    eprintln!("macOS TIFF osascript failed: stderr={}", err);
                }
            }
        }

        eprintln!("No image found in macOS clipboard");
        None
    }

    fn scan_file(&mut self, path: &str) {
        self.debug_info = format!("Loading: {}", path);

        let path_lower = path.to_lowercase();
        
        // Handle SVG files separately - image crate doesn't support SVG
        if path_lower.ends_with(".svg") {
            if let Some(rasterized) = Self::rasterize_svg_from_file(path) {
                match qr_scanner::scan_image(&rasterized) {
                    Ok(result) => {
                        self.result_text = result.text.clone();
                        self.history.push(result.text);
                        if self.auto_copy {
                            Self::copy_to_clipboard(&self.result_text);
                        }
                        self.debug_info = "QR code scanned from SVG file!".to_string();
                        return;
                    }
                    Err(e) => {
                        self.result_text = format!("No QR code found: {}", e);
                        self.debug_info = format!("Scan failed: {}", e);
                        return;
                    }
                }
            }
            self.result_text = "Failed to parse SVG".to_string();
            self.debug_info = "Error: could not parse SVG file".to_string();
            return;
        }
        
        // Try to open as regular image
        match image::open(path) {
            Ok(dyn_img) => {
                match qr_scanner::scan_image(&dyn_img) {
                    Ok(result) => {
                        self.result_text = result.text.clone();
                        self.history.push(result.text);
                        if self.auto_copy {
                            Self::copy_to_clipboard(&self.result_text);
                        }
                        self.debug_info = "QR code scanned from file!".to_string();
                    }
                    Err(e) => {
                        self.result_text = format!("No QR code found: {}", e);
                        self.debug_info = format!("Scan failed: {}", e);
                    }
                }
            }
            Err(e) => {
                self.result_text = format!("Failed to open image: {}", path);
                self.debug_info = format!("Error: {}", e);
            }
        }
    }

    fn rasterize_svg_from_file(path: &str) -> Option<image::DynamicImage> {
        eprintln!("Rasterizing SVG from file: {}", path);
        let svg_text = std::fs::read_to_string(path).ok()?;
        rasterize_svg(&svg_text)
    }
}

impl eframe::App for QrScannerApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        let mut trigger_paste = false;

        let events = ctx.input(|i| i.events.clone());
        for event in &events {
            match event {
                egui::Event::Key { key, pressed: _, modifiers, .. } => {
                    // Trigger on V with Ctrl, regardless of pressed state (some systems report weirdly)
                    if *key == egui::Key::V && modifiers.ctrl {
                        eprintln!("Ctrl+V detected in events!");
                        trigger_paste = true;
                    }
                }
                egui::Event::Paste(_) => {
                    trigger_paste = true;
                }
                _ => {}
            }
        }

    if trigger_paste {
        self.paste_from_clipboard();
    }

    // Main UI
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.heading("QR Scanner");
            ui.separator();

            ui.horizontal(|ui| {
                if ui.button("Scan QR (area selection)").clicked() {
                    self.start_scan();
                }
                ui.checkbox(&mut self.auto_copy, "Auto-copy");
            });
            ui.label("Windows: uses OS Snipping Tool. macOS/Linux: click and drag to select.");

            ui.separator();
            ui.label("Result:");
            ui.text_edit_multiline(&mut self.result_text);

            ui.horizontal(|ui| {
                if ui.button("Copy").clicked() {
                    Self::copy_to_clipboard(&self.result_text);
                }
                if ui.button("Clear").clicked() {
                    self.result_text.clear();
                }
            });

            ui.separator();

            ui.label("Paste from clipboard:");
            ui.horizontal(|ui| {
                if ui.button("Paste Image (Ctrl+V)").clicked() {
                    self.paste_from_clipboard();
                }
            });
            ui.label("In browser: right-click image -> Copy Image");

            ui.separator();

            ui.collapsing("Or open file", |ui| {
                if ui.button("Open File...").clicked() {
                    if let Some(path) = rfd::FileDialog::new()
                        .add_filter("Images", &["png", "jpg", "jpeg", "gif", "bmp", "webp", "svg"])
                        .pick_file()
                    {
                        self.scan_file(path.to_str().unwrap());
                    }
                }
            });

            ui.separator();

            ui.collapsing("History", |ui| {
                egui::ScrollArea::vertical().show(ui, |ui| {
                    for (i, item) in self.history.iter().enumerate().rev().take(20) {
                        ui.horizontal(|ui| {
                            ui.label(format!("{}:", self.history.len() - i));
                            let display = if item.len() > 40 {
                                format!("{}...", &item[..40])
                            } else {
                                item.clone()
                            };
                            ui.monospace(display);
                            if ui.button("Copy").clicked() {
                                Self::copy_to_clipboard(item);
                            }
                        });
                    }
                });
                if !self.history.is_empty() {
                    if ui.button("Clear History").clicked() {
                        self.history.clear();
                    }
                }
            });

            ui.separator();

            ui.label("Debug:");
            ui.monospace(&self.debug_info);
        });
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let native_options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default()
            .with_inner_size([450.0, 550.0])
            .with_title("QR Scanner"),
        ..Default::default()
    };

    eframe::run_native(
        "QR Scanner",
        native_options,
        Box::new(|_cc| Ok(Box::new(QrScannerApp::new()))),
    )?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use qr_scanner::scan_image;

    fn generate_qr_image(data: &str, size: usize) -> image::DynamicImage {
        use qrcode::QrCode;
        let code = QrCode::new(data).unwrap();
        let size = size as u32;
        let svg_str = code.render::<qrcode::render::svg::Color>().min_dimensions(size, size).dark_color(qrcode::render::svg::Color("#000000")).light_color(qrcode::render::svg::Color("#ffffff")).build();
        rasterize_svg(&svg_str).expect("Failed to rasterize QR SVG")
    }

    #[test]
    fn test_convert_bgra_to_rgba_red_pixel() {
        let bgra: &[u8] = &[0, 0, 255, 255];
        let rgba = convert_bgra_to_rgba(bgra);
        assert_eq!(rgba, vec![255, 0, 0, 255]);
    }

    #[test]
    fn test_convert_bgra_to_rgba_green_pixel() {
        let bgra: &[u8] = &[0, 255, 0, 255];
        let rgba = convert_bgra_to_rgba(bgra);
        assert_eq!(rgba, vec![0, 255, 0, 255]);
    }

    #[test]
    fn test_convert_bgra_to_rgba_blue_pixel() {
        let bgra: &[u8] = &[255, 0, 0, 255];
        let rgba = convert_bgra_to_rgba(bgra);
        assert_eq!(rgba, vec![0, 0, 255, 255]);
    }

    #[test]
    fn test_convert_bgra_to_rgba_multiple_pixels() {
        let bgra: &[u8] = &[
            0, 0, 255, 255,  // red
            0, 255, 0, 255,  // green
            255, 0, 0, 255,  // blue
        ];
        let rgba = convert_bgra_to_rgba(bgra);
        assert_eq!(rgba, vec![
            255, 0, 0, 255,
            0, 255, 0, 255,
            0, 0, 255, 255,
        ]);
    }

    #[test]
    fn test_convert_bgra_to_rgba_empty() {
        let bgra: &[u8] = &[];
        let rgba = convert_bgra_to_rgba(bgra);
        assert!(rgba.is_empty());
    }

    #[test]
    fn test_convert_bgra_to_rgba_incomplete_pixel_ignored() {
        let bgra: &[u8] = &[0, 0, 255, 255, 42];
        let rgba = convert_bgra_to_rgba(bgra);
        assert_eq!(rgba, vec![255, 0, 0, 255]);
    }

    #[test]
    fn test_scan_qr_from_generated_image() {
        let img = generate_qr_image("https://example.com", 256);
        let result = scan_image(&img).expect("Should decode QR");
        assert_eq!(result.text, "https://example.com");
    }

    #[test]
    fn test_scan_qr_with_longer_text() {
        let img = generate_qr_image("Hello, QR Scanner Test!", 256);
        let result = scan_image(&img).expect("Should decode QR");
        assert_eq!(result.text, "Hello, QR Scanner Test!");
    }

    #[test]
    fn test_scan_qr_no_qr_in_image() {
        let img = image::DynamicImage::ImageRgba8(
            image::ImageBuffer::from_pixel(100, 100, image::Rgba([128, 128, 128, 255]))
        );
        let result = scan_image(&img);
        assert!(result.is_err());
    }

    #[test]
    fn test_rasterize_svg_valid() {
        let svg = r#"<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
  <rect width="100" height="100" fill="white"/>
  <rect x="10" y="10" width="20" height="20" fill="black"/>
</svg>"#;
        let img = rasterize_svg(svg);
        assert!(img.is_some());
        let img = img.unwrap();
        assert_eq!(img.width(), 100);
        assert_eq!(img.height(), 100);
    }

    #[test]
    fn test_rasterize_svg_invalid() {
        let svg = "not an svg at all";
        let img = rasterize_svg(svg);
        assert!(img.is_none());
    }

    #[test]
    fn test_rasterize_svg_empty() {
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0"></svg>"#;
        let img = rasterize_svg(svg);
        assert!(img.is_none());
    }

    #[test]
    fn test_rasterize_svg_qr_code() {
        let img = generate_qr_image("test-svg-qr", 128);
        let result = scan_image(&img);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().text, "test-svg-qr");
    }

    #[test]
    fn test_extract_img_url_double_quotes() {
        let html = r#"<html><body><img src="https://example.com/qr.png"></body></html>"#;
        let url = extract_img_url_from_html(html);
        assert_eq!(url, Some("https://example.com/qr.png".to_string()));
    }

    #[test]
    fn test_extract_img_url_single_quotes() {
        let html = r#"<html><body><img src='https://example.com/qr.png'></body></html>"#;
        let url = extract_img_url_from_html(html);
        assert_eq!(url, Some("https://example.com/qr.png".to_string()));
    }

    #[test]
    fn test_extract_img_url_no_src() {
        let html = "<html><body><p>No image here</p></body></html>";
        let url = extract_img_url_from_html(html);
        assert_eq!(url, None);
    }

    #[test]
    fn test_extract_img_url_empty_src() {
        let html = r#"<img src="">"#;
        let url = extract_img_url_from_html(html);
        assert_eq!(url, Some("".to_string()));
    }

    #[test]
    fn test_extract_img_url_with_surrounding_attributes() {
        let html = r#"<img alt="QR Code" src="https://cdn.example.com/qr/abc123.png" width="200">"#;
        let url = extract_img_url_from_html(html);
        assert_eq!(url, Some("https://cdn.example.com/qr/abc123.png".to_string()));
    }

    #[test]
    fn test_extract_img_url_html_entities() {
        let html = r#"<img src="https://api.qr.com/v1?text=hello&amp;width=300&amp;format=PNG">"#;
        let url = extract_img_url_from_html(html);
        assert_eq!(url, Some("https://api.qr.com/v1?text=hello&width=300&format=PNG".to_string()));
    }

    #[test]
    fn test_decode_html_entities() {
        assert_eq!(decode_html_entities("&amp;"), "&");
        assert_eq!(decode_html_entities("&lt;"), "<");
        assert_eq!(decode_html_entities("&gt;"), ">");
        assert_eq!(decode_html_entities("&quot;"), "\"");
        assert_eq!(decode_html_entities("a&amp;b&amp;c"), "a&b&c");
    }

    #[test]
    fn test_full_paste_flow_bgra_to_qr() {
        let img = generate_qr_image("https://paste-test.com", 256);
        let rgba_img = img.to_rgba8();
        let (w, h) = rgba_img.dimensions();

        let mut bgra = Vec::with_capacity((w * h * 4) as usize);
        for pixel in rgba_img.pixels() {
            bgra.push(pixel[2]);
            bgra.push(pixel[1]);
            bgra.push(pixel[0]);
            bgra.push(pixel[3]);
        }

        let rgba_converted = convert_bgra_to_rgba(&bgra);
        let restored = image::ImageBuffer::<image::Rgba<u8>, _>::from_raw(w, h, rgba_converted)
            .expect("Should create image buffer");
        let dyn_img = image::DynamicImage::ImageRgba8(restored);

        let result = scan_image(&dyn_img).expect("Should decode QR from round-tripped BGRA data");
        assert_eq!(result.text, "https://paste-test.com");
    }

    #[test]
    fn test_full_paste_flow_svg_to_qr() {
        use qrcode::QrCode;
        let code = QrCode::new("https://svg-paste-test.com").unwrap();
        let svg_str = code.render::<qrcode::render::svg::Color>()
            .min_dimensions(200, 200)
            .dark_color(qrcode::render::svg::Color("#000000"))
            .light_color(qrcode::render::svg::Color("#ffffff"))
            .build();

        let img = rasterize_svg(&svg_str).expect("Should rasterize SVG");
        let result = scan_image(&img).expect("Should decode QR from SVG");
        assert_eq!(result.text, "https://svg-paste-test.com");
    }
}