computeruse-rs 2.0.0

A Playwright-style SDK for automating desktop GUI applications
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
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
use crate::errors::AutomationError;
use crate::selector::Selector;
use crate::ScreenshotResult;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::HashMap;
use std::fmt;
use std::fmt::Debug;
use tracing::{debug, info, instrument, warn};

use super::{ClickResult, Locator};

#[cfg(target_os = "windows")]
use crate::{hide_action_overlay, show_action_overlay};

/// Response structure for exploration result
#[derive(Debug, Default)]
pub struct ExploredElementDetail {
    pub role: String,
    pub name: Option<String>, // Use 'name' consistently for the primary label/text
    pub id: Option<String>,
    pub bounds: Option<(f64, f64, f64, f64)>, // Include bounds for spatial context
    pub value: Option<String>,
    pub description: Option<String>,
    pub text: Option<String>,
    pub parent_id: Option<String>,
    pub children_ids: Vec<String>,
    pub suggested_selector: String,
}

impl ExploredElementDetail {
    /// Create a new ExploredElementDetail from a UIElement
    pub fn from_element(
        element: &UIElement,
        parent_id: Option<String>,
    ) -> Result<Self, AutomationError> {
        let id = element.id_or_empty();
        Ok(Self {
            role: element.role(),
            name: element.name(),
            id: if id.is_empty() {
                None
            } else {
                Some(id.clone())
            },
            bounds: element.bounds().ok(),
            value: element.attributes().value,
            description: element.attributes().description,
            text: element.text(1).ok(),
            parent_id,
            children_ids: Vec::new(),
            suggested_selector: format!("#{id}"),
        })
    }
}

/// Response structure for exploration result
#[derive(Debug)]
pub struct ExploreResponse {
    pub parent: UIElement,                    // The parent element explored
    pub children: Vec<ExploredElementDetail>, // List of direct children details
}

/// Represents a UI element in a desktop application
#[derive(Debug)]
pub struct UIElement {
    inner: Box<dyn UIElementImpl>,
}

/// Serializable version of UIElement for JSON storage and transmission
///
/// This struct contains the same data as UIElement but can be both serialized
/// and deserialized. It's useful for storing UI element data in files, databases,
/// or sending over network connections.
///
/// Note: This struct only contains the element's properties and cannot perform
/// any UI automation actions. To interact with UI elements, you need a live UIElement.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializableUIElement {
    #[serde(skip_serializing_if = "is_empty_string")]
    pub id: Option<String>,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub role: String,
    #[serde(skip_serializing_if = "is_empty_string")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bounds: Option<(f64, f64, f64, f64)>,
    #[serde(skip_serializing_if = "is_empty_string")]
    pub value: Option<String>,
    #[serde(skip_serializing_if = "is_empty_string")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "is_empty_string")]
    pub window_and_application_name: Option<String>,
    #[serde(skip_serializing_if = "is_empty_string")]
    pub window_title: Option<String>,
    #[serde(skip_serializing_if = "is_empty_string")]
    pub url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub process_id: Option<u32>,
    #[serde(skip_serializing_if = "is_empty_string")]
    pub process_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub children: Option<Vec<SerializableUIElement>>,

    // Additional fields for better LLM understanding of UI state
    #[serde(skip_serializing_if = "is_empty_string")]
    pub label: Option<String>,
    #[serde(skip_serializing_if = "is_empty_string")]
    pub text: Option<String>,
    #[serde(skip_serializing_if = "is_false_bool")]
    pub is_keyboard_focusable: Option<bool>,
    #[serde(skip_serializing_if = "is_false_bool")]
    pub is_focused: Option<bool>,
    #[serde(skip_serializing_if = "is_false_bool")]
    pub is_toggled: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    #[serde(skip_serializing_if = "is_false_bool")]
    pub is_selected: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub child_count: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index_in_parent: Option<usize>,
    /// Chained selector path from root to this element (e.g., "role:Window && name:App >> role:Button && name:Submit")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selector: Option<String>,
}

impl From<&UIElement> for SerializableUIElement {
    fn from(element: &UIElement) -> Self {
        let attrs = element.attributes();
        let bounds = element.bounds().ok();

        // Get process_id and derive process_name
        let process_id = element.process_id().ok();
        let process_name = process_id.and_then(|pid| {
            #[cfg(target_os = "windows")]
            {
                crate::platforms::windows::get_process_name_by_pid(pid as i32).ok()
            }

            #[cfg(not(target_os = "windows"))]
            {
                use sysinfo::{ProcessesToUpdate, System};
                let mut system = System::new();
                system.refresh_processes(ProcessesToUpdate::All, true);
                system
                    .process(sysinfo::Pid::from_u32(pid))
                    .map(|p| p.name().to_string_lossy().to_string())
            }
        });

        Self {
            id: element.id(),
            role: element.role(),
            name: attrs.name,
            bounds,
            value: attrs.value,
            description: attrs.description,
            window_and_application_name: Some(element.application_name()),
            window_title: Some(element.window_title()),
            url: element.url(),
            process_id,
            process_name,
            children: None,

            // Additional fields for better LLM understanding of UI state
            label: attrs.label,
            text: attrs.text,
            is_keyboard_focusable: attrs.is_keyboard_focusable,
            is_focused: attrs.is_focused,
            is_toggled: attrs.is_toggled,
            enabled: attrs.enabled,
            is_selected: attrs.is_selected,
            child_count: attrs.child_count,
            index_in_parent: attrs.index_in_parent,
            selector: None, // Selector is only available when built from tree context
        }
    }
}

impl SerializableUIElement {
    /// Create a new SerializableUIElement with minimal data
    pub fn new(role: String) -> Self {
        Self {
            id: None,
            role,
            name: None,
            bounds: None,
            value: None,
            description: None,
            window_and_application_name: None,
            window_title: None,
            url: None,
            process_id: None,
            process_name: None,
            children: None,

            // Additional fields for better LLM understanding of UI state
            label: None,
            text: None,
            is_keyboard_focusable: None,
            is_focused: None,
            is_toggled: None,
            enabled: None,
            is_selected: None,
            child_count: None,
            index_in_parent: None,
            selector: None,
        }
    }

    /// Convert to JSON string
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// Create from JSON string
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }

    /// Get a display name for this element
    pub fn display_name(&self) -> String {
        self.name
            .clone()
            .or_else(|| self.value.clone())
            .unwrap_or_else(|| self.role.clone())
    }
}

/// Helper functions for clean serialization
fn is_empty_string(opt: &Option<String>) -> bool {
    match opt {
        Some(s) => s.is_empty(),
        None => true,
    }
}

fn is_false_bool(opt: &Option<bool>) -> bool {
    matches!(opt, Some(false) | None)
}

fn is_empty_properties(props: &HashMap<String, Option<serde_json::Value>>) -> bool {
    props.is_empty() || props.values().all(|v| v.is_none())
}

/// Serializable OCR element for representing text detected via optical character recognition.
/// Mirrors the structure of SerializableUIElement for consistent tree formatting.
///
/// Hierarchy: OcrResult -> OcrLine -> OcrWord
/// - OcrResult: Root element containing all recognized text and rotation angle
/// - OcrLine: A line of text containing multiple words
/// - OcrWord: Individual word with precise bounding box
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OcrElement {
    /// Role type: "OcrResult", "OcrLine", or "OcrWord"
    pub role: String,
    /// The recognized text content
    #[serde(skip_serializing_if = "is_empty_string")]
    pub text: Option<String>,
    /// Bounding box as (x, y, width, height) in absolute screen coordinates
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bounds: Option<(f64, f64, f64, f64)>,
    /// Text rotation angle in degrees (only present on OcrResult)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text_angle: Option<f64>,
    /// Confidence score (0.0 to 1.0) if available
    #[serde(skip_serializing_if = "Option::is_none")]
    pub confidence: Option<f64>,
    /// Child elements (lines for OcrResult, words for OcrLine)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub children: Option<Vec<OcrElement>>,
}

impl OcrElement {
    /// Create a new OcrResult (root element)
    pub fn new_result(text: String, text_angle: Option<f64>, lines: Vec<OcrElement>) -> Self {
        Self {
            role: "OcrResult".to_string(),
            text: Some(text),
            bounds: None,
            text_angle,
            confidence: None,
            children: if lines.is_empty() { None } else { Some(lines) },
        }
    }

    /// Create a new OcrLine
    pub fn new_line(
        text: String,
        bounds: Option<(f64, f64, f64, f64)>,
        words: Vec<OcrElement>,
    ) -> Self {
        Self {
            role: "OcrLine".to_string(),
            text: Some(text),
            bounds,
            text_angle: None,
            confidence: None,
            children: if words.is_empty() { None } else { Some(words) },
        }
    }

    /// Create a new OcrWord
    pub fn new_word(text: String, bounds: (f64, f64, f64, f64), confidence: Option<f64>) -> Self {
        Self {
            role: "OcrWord".to_string(),
            text: Some(text),
            bounds: Some(bounds),
            text_angle: None,
            confidence,
            children: None,
        }
    }

    /// Get display name for tree formatting
    pub fn display_name(&self) -> String {
        self.text.clone().unwrap_or_default()
    }
}

/// Attributes associated with a UI element
#[derive(Clone, Serialize, Deserialize, Default)]
pub struct UIElementAttributes {
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub role: String,
    #[serde(default, skip_serializing_if = "is_empty_string")]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "is_empty_string")]
    pub label: Option<String>,
    #[serde(default, skip_serializing_if = "is_empty_string")]
    pub text: Option<String>,
    #[serde(default, skip_serializing_if = "is_empty_string")]
    pub value: Option<String>,
    #[serde(default, skip_serializing_if = "is_empty_string")]
    pub description: Option<String>,
    #[serde(default, skip_serializing_if = "is_empty_string")]
    pub application_name: Option<String>,
    #[serde(default, skip_serializing_if = "is_empty_properties")]
    pub properties: HashMap<String, Option<serde_json::Value>>,
    #[serde(default, skip_serializing_if = "is_false_bool")]
    pub is_keyboard_focusable: Option<bool>,
    #[serde(default, skip_serializing_if = "is_false_bool")]
    pub is_focused: Option<bool>,
    #[serde(default, skip_serializing_if = "is_false_bool")]
    pub is_toggled: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bounds: Option<(f64, f64, f64, f64)>, // Only populated for keyboard-focusable elements
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    #[serde(default, skip_serializing_if = "is_false_bool")]
    pub is_selected: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub child_count: Option<usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub index_in_parent: Option<usize>,
}

impl fmt::Debug for UIElementAttributes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut debug_struct = f.debug_struct("UIElementAttributes");

        // Only show non-empty role
        if !self.role.is_empty() {
            debug_struct.field("role", &self.role);
        }

        // Only show non-empty name
        if let Some(ref name) = self.name {
            if !name.is_empty() {
                debug_struct.field("name", name);
            }
        }

        // Only show non-empty label
        if let Some(ref label) = self.label {
            if !label.is_empty() {
                debug_struct.field("label", label);
            }
        }

        // Only show non-empty text
        if let Some(ref text) = self.text {
            if !text.is_empty() {
                debug_struct.field("text", text);
            }
        }

        // Only show non-empty value
        if let Some(ref value) = self.value {
            if !value.is_empty() {
                debug_struct.field("value", value);
            }
        }

        // Only show non-empty description
        if let Some(ref description) = self.description {
            if !description.is_empty() {
                debug_struct.field("description", description);
            }
        }

        // Only show non-empty properties
        if !self.properties.is_empty() && self.properties.values().any(|v| v.is_some()) {
            debug_struct.field("properties", &self.properties);
        }

        // Only show keyboard focusable if true
        if let Some(true) = self.is_keyboard_focusable {
            debug_struct.field("is_keyboard_focusable", &true);
        }

        // Only show focused if true
        if let Some(true) = self.is_focused {
            debug_struct.field("is_focused", &true);
        }

        // Only show toggled if true
        if let Some(true) = self.is_toggled {
            debug_struct.field("is_toggled", &true);
        }

        // Only show bounds if present
        if let Some(ref bounds) = self.bounds {
            debug_struct.field("bounds", bounds);
        }

        // Only show selected if true
        if let Some(true) = self.is_selected {
            debug_struct.field("is_selected", &true);
        }

        // Only show child_count if present
        if let Some(count) = self.child_count {
            debug_struct.field("child_count", &count);
        }

        // Only show index_in_parent if present
        if let Some(index) = self.index_in_parent {
            debug_struct.field("index_in_parent", &index);
        }

        debug_struct.finish()
    }
}

/// Interface for platform-specific element implementations
pub trait UIElementImpl: Send + Sync + Debug {
    fn object_id(&self) -> usize;
    fn id(&self) -> Option<String>;
    fn role(&self) -> String;
    fn attributes(&self) -> UIElementAttributes;
    fn name(&self) -> Option<String> {
        self.attributes().name
    }
    fn children(&self) -> Result<Vec<UIElement>, AutomationError>;
    fn parent(&self) -> Result<Option<UIElement>, AutomationError>;
    fn bounds(&self) -> Result<(f64, f64, f64, f64), AutomationError>; // x, y, width, height
    fn click(&self) -> Result<ClickResult, AutomationError>;
    fn double_click(&self) -> Result<ClickResult, AutomationError>;
    fn right_click(&self) -> Result<(), AutomationError>;
    /// Click at a specific position within the element bounds
    /// x_pct and y_pct are percentages (0-100) from top-left corner
    fn click_at_position(
        &self,
        x_pct: u8,
        y_pct: u8,
        click_type: crate::ClickType,
    ) -> Result<ClickResult, AutomationError> {
        // Default: not supported, platforms should override
        let _ = (x_pct, y_pct, click_type);
        Err(AutomationError::UnsupportedOperation(
            "click_at_position not implemented for this platform".into(),
        ))
    }
    fn hover(&self) -> Result<(), AutomationError>;
    fn focus(&self) -> Result<(), AutomationError>;
    fn invoke(&self) -> Result<(), AutomationError>;
    fn type_text(
        &self,
        text: &str,
        use_clipboard: bool,
        try_focus_before: bool,
        try_click_before: bool,
        restore_focus: bool,
    ) -> Result<(), AutomationError>;
    fn press_key(
        &self,
        key: &str,
        try_focus_before: bool,
        try_click_before: bool,
        restore_focus: bool,
    ) -> Result<(), AutomationError>;

    fn type_text_with_state(
        &self,
        text: &str,
        use_clipboard: bool,
        try_focus_before: bool,
        try_click_before: bool,
    ) -> Result<crate::ActionResult, AutomationError> {
        // Default implementation - platforms can override for state tracking
        self.type_text(
            text,
            use_clipboard,
            try_focus_before,
            try_click_before,
            false,
        )?;

        // Auto-verify by reading the value back
        let verification = match self.get_value() {
            Ok(Some(actual)) => {
                let passed = actual.contains(text);
                Some(crate::TypeVerification {
                    passed,
                    expected: text.to_string(),
                    actual: Some(actual),
                    error: if passed {
                        None
                    } else {
                        Some("Value does not contain expected text".to_string())
                    },
                })
            }
            Ok(None) => Some(crate::TypeVerification {
                passed: true, // Can't verify, assume success
                expected: text.to_string(),
                actual: None,
                error: None,
            }),
            Err(e) => Some(crate::TypeVerification {
                passed: true, // Can't verify, assume success
                expected: text.to_string(),
                actual: None,
                error: Some(format!("Could not read value: {}", e)),
            }),
        };

        Ok(crate::ActionResult {
            action: "type_text".to_string(),
            details: "No state tracking available".to_string(),
            data: Some(
                serde_json::json!({"text": text, "use_clipboard": use_clipboard, "try_focus_before": try_focus_before, "try_click_before": try_click_before}),
            ),
            verification,
        })
    }

    // New methods with state tracking
    fn invoke_with_state(&self) -> Result<crate::ActionResult, AutomationError> {
        // Default implementation - platforms can override for state tracking
        self.invoke()?;
        Ok(crate::ActionResult {
            action: "invoke".to_string(),
            details: "No state tracking available".to_string(),
            data: None,
            verification: None,
        })
    }

    fn press_key_with_state(
        &self,
        key: &str,
        try_focus_before: bool,
        try_click_before: bool,
    ) -> Result<crate::ActionResult, AutomationError> {
        // Default implementation - platforms can override for state tracking
        self.press_key(key, try_focus_before, try_click_before, false)?;
        Ok(crate::ActionResult {
            action: "press_key".to_string(),
            details: "No state tracking available".to_string(),
            data: Some(
                serde_json::json!({"key": key, "try_focus_before": try_focus_before, "try_click_before": try_click_before}),
            ),
            verification: None,
        })
    }
    fn get_text(&self, max_depth: usize) -> Result<String, AutomationError>;
    fn set_value(&self, value: &str) -> Result<(), AutomationError>;
    /// Get the current value of this element using the platform's Value pattern.
    /// Returns None if the element doesn't support the Value pattern.
    fn get_value(&self) -> Result<Option<String>, AutomationError>;
    fn is_enabled(&self) -> Result<bool, AutomationError>;
    fn is_visible(&self) -> Result<bool, AutomationError>;
    fn is_focused(&self) -> Result<bool, AutomationError>;
    fn perform_action(&self, action: &str) -> Result<(), AutomationError>;
    fn as_any(&self) -> &dyn std::any::Any;
    fn create_locator(&self, selector: Selector) -> Result<Locator, AutomationError>;
    fn scroll(&self, direction: &str, amount: f64) -> Result<(), AutomationError>;

    fn scroll_with_state(
        &self,
        direction: &str,
        amount: f64,
    ) -> Result<crate::ActionResult, AutomationError> {
        // Default implementation - platforms can override for state tracking
        self.scroll(direction, amount)?;
        Ok(crate::ActionResult {
            action: "scroll".to_string(),
            details: "No state tracking available".to_string(),
            data: Some(serde_json::json!({"direction": direction, "amount": amount})),
            verification: None,
        })
    }

    // New method to activate the window containing the element
    fn activate_window(&self) -> Result<(), AutomationError>;

    // New method to minimize the window containing the element
    fn minimize_window(&self) -> Result<(), AutomationError>;

    // New method to maximize the window containing the element
    fn maximize_window(&self) -> Result<(), AutomationError>;

    /// Maximize window using keyboard simulation (Win+Up) - for UWP apps
    fn maximize_window_keyboard(&self) -> Result<(), AutomationError>;

    /// Minimize window using keyboard simulation (Win+Down) - for UWP apps
    fn minimize_window_keyboard(&self) -> Result<(), AutomationError>;

    /// Get native window handle (HWND on Windows)
    fn get_native_window_handle(&self) -> Result<isize, AutomationError>;

    // Add a method to clone the box
    fn clone_box(&self) -> Box<dyn UIElementImpl>;

    // New method for keyboard focusable
    fn is_keyboard_focusable(&self) -> Result<bool, AutomationError>;

    // New method for mouse drag
    fn mouse_drag(
        &self,
        start_x: f64,
        start_y: f64,
        end_x: f64,
        end_y: f64,
    ) -> Result<(), AutomationError>;

    // New methods for mouse control
    fn mouse_click_and_hold(&self, x: f64, y: f64) -> Result<(), AutomationError>;
    fn mouse_move(&self, x: f64, y: f64) -> Result<(), AutomationError>;
    fn mouse_release(&self) -> Result<(), AutomationError>;

    // New methods to get containing application and window
    fn application(&self) -> Result<Option<UIElement>, AutomationError>;
    fn window(&self) -> Result<Option<UIElement>, AutomationError>;

    // New method to highlight the element with optional text overlay
    fn highlight(
        &self,
        color: Option<u32>,
        duration: Option<std::time::Duration>,
        text: Option<&str>,
        text_position: Option<crate::TextPosition>,
        font_style: Option<crate::FontStyle>,
    ) -> Result<crate::HighlightHandle, AutomationError>;

    /// Sets the transparency of the window.
    /// The percentage value ranges from 0 (completely transparent) to 100 (completely opaque).
    fn set_transparency(&self, percentage: u8) -> Result<(), AutomationError>;

    // New method to get the process ID of the element
    fn process_id(&self) -> Result<u32, AutomationError>;

    // New method to capture a screenshot of the element
    fn capture(&self) -> Result<ScreenshotResult, AutomationError>;

    /// Close the element if it's closable (like windows, applications)
    /// Does nothing for non-closable elements (like buttons, text, etc.)
    fn close(&self) -> Result<(), AutomationError>;

    // New method to get the URL if the element is in a browser window
    fn url(&self) -> Option<String>;

    // New high-level input functions
    fn select_option(&self, option_name: &str) -> Result<(), AutomationError>;
    fn list_options(&self) -> Result<Vec<String>, AutomationError>;

    fn select_option_with_state(
        &self,
        option_name: &str,
    ) -> Result<crate::ActionResult, AutomationError> {
        // Default implementation - platforms can override for state tracking
        self.select_option(option_name)?;
        Ok(crate::ActionResult {
            action: "select_option".to_string(),
            details: "No state tracking available".to_string(),
            data: Some(serde_json::json!({"option_selected": option_name})),
            verification: None,
        })
    }
    fn is_toggled(&self) -> Result<bool, AutomationError>;
    fn set_toggled(&self, state: bool) -> Result<(), AutomationError>;

    fn set_toggled_with_state(&self, state: bool) -> Result<crate::ActionResult, AutomationError> {
        // Default implementation - platforms can override for state tracking
        self.set_toggled(state)?;
        Ok(crate::ActionResult {
            action: "set_toggled".to_string(),
            details: "No state tracking available".to_string(),
            data: Some(serde_json::json!({"state": state})),
            verification: None,
        })
    }
    fn get_range_value(&self) -> Result<f64, AutomationError>;
    fn set_range_value(&self, value: f64) -> Result<(), AutomationError>;
    fn is_selected(&self) -> Result<bool, AutomationError>;
    fn set_selected(&self, state: bool) -> Result<(), AutomationError>;

    fn set_selected_with_state(&self, state: bool) -> Result<crate::ActionResult, AutomationError> {
        // Default implementation - platforms can override for state tracking
        self.set_selected(state)?;
        Ok(crate::ActionResult {
            action: "set_selected".to_string(),
            details: "No state tracking available".to_string(),
            data: Some(serde_json::json!({"state": state})),
            verification: None,
        })
    }

    /// Returns the `Monitor` object that contains this element.
    ///
    /// By default this implementation uses the element's bounding box and
    /// the `xcap` crate to locate the monitor that contains the element's
    /// top-left corner. Individual platforms can override this for a more
    /// accurate or cheaper implementation.
    fn monitor(&self) -> Result<crate::Monitor, AutomationError> {
        // 1. Get element bounds (x, y) with better error handling
        let (x, y, _w, _h) = match self.bounds() {
            Ok(bounds) => bounds,
            Err(e) => {
                // If we can't get bounds, fall back to primary monitor
                warn!("Failed to get element bounds for monitor detection: {}", e);
                return self.get_primary_monitor_fallback();
            }
        };

        // 2. Enumerate available monitors using xcap (already a dependency)
        let monitors = xcap::Monitor::all().map_err(|e| {
            AutomationError::PlatformError(format!("Failed to enumerate monitors: {e}"))
        })?;

        // 3. Find the first monitor whose geometry contains the element's
        //    upper-left corner.
        for (idx, mon) in monitors.iter().enumerate() {
            // Guard every call because each accessor returns Result<_>
            let mon_x = mon.x().map_err(|e| {
                AutomationError::PlatformError(format!("Failed to get monitor x: {e}"))
            })?;
            let mon_y = mon.y().map_err(|e| {
                AutomationError::PlatformError(format!("Failed to get monitor y: {e}"))
            })?;
            let mon_w = mon.width().map_err(|e| {
                AutomationError::PlatformError(format!("Failed to get monitor width: {e}"))
            })? as i32;
            let mon_h = mon.height().map_err(|e| {
                AutomationError::PlatformError(format!("Failed to get monitor height: {e}"))
            })? as i32;

            // Simple contains check (include edges)
            let within_x = (x as i32) >= mon_x && (x as i32) < mon_x + mon_w;
            let within_y = (y as i32) >= mon_y && (y as i32) < mon_y + mon_h;

            if within_x && within_y {
                // Build our internal Monitor struct from the xcap monitor
                let name = mon.name().map_err(|e| {
                    AutomationError::PlatformError(format!("Failed to get monitor name: {e}"))
                })?;
                let is_primary = mon.is_primary().map_err(|e| {
                    AutomationError::PlatformError(format!(
                        "Failed to get monitor primary flag: {e}"
                    ))
                })?;
                let scale_factor = mon.scale_factor().map_err(|e| {
                    AutomationError::PlatformError(format!(
                        "Failed to get monitor scale factor: {e}"
                    ))
                })? as f64;

                // Get work area for this monitor if it's Windows and primary
                let work_area = if is_primary {
                    #[cfg(target_os = "windows")]
                    {
                        use crate::platforms::windows::element::WorkArea;
                        WorkArea::get_primary().ok().map(|work_area| crate::WorkAreaBounds {
                            x: work_area.x,
                            y: work_area.y,
                            width: work_area.width as u32,
                            height: work_area.height as u32,
                        })
                    }
                    #[cfg(not(target_os = "windows"))]
                    {
                        // Non-Windows: best-effort; treat full monitor bounds as "work area".
                        Some(crate::WorkAreaBounds {
                            x: mon_x,
                            y: mon_y,
                            width: mon_w as u32,
                            height: mon_h as u32,
                        })
                    }
                } else {
                    Some(crate::WorkAreaBounds {
                        x: mon_x,
                        y: mon_y,
                        width: mon_w as u32,
                        height: mon_h as u32,
                    })
                };

                return Ok(crate::Monitor {
                    id: format!("monitor_{idx}"),
                    name,
                    is_primary,
                    width: mon_w as u32,
                    height: mon_h as u32,
                    x: mon_x,
                    y: mon_y,
                    scale_factor,
                    work_area,
                });
            }
        }

        // If no monitor found containing the element, fall back to primary
        warn!(
            "Element coordinates ({}, {}) not found on any monitor, falling back to primary",
            x, y
        );
        self.get_primary_monitor_fallback()
    }

    /// Helper method to get primary monitor as fallback
    fn get_primary_monitor_fallback(&self) -> Result<crate::Monitor, AutomationError> {
        let monitors = xcap::Monitor::all().map_err(|e| {
            AutomationError::PlatformError(format!("Failed to enumerate monitors: {e}"))
        })?;

        for (idx, monitor) in monitors.iter().enumerate() {
            let is_primary = monitor.is_primary().map_err(|e| {
                AutomationError::PlatformError(format!("Failed to check primary status: {e}"))
            })?;

            if is_primary {
                let name = monitor.name().map_err(|e| {
                    AutomationError::PlatformError(format!("Failed to get monitor name: {e}"))
                })?;
                let width = monitor.width().map_err(|e| {
                    AutomationError::PlatformError(format!("Failed to get monitor width: {e}"))
                })?;
                let height = monitor.height().map_err(|e| {
                    AutomationError::PlatformError(format!("Failed to get monitor height: {e}"))
                })?;
                let x = monitor.x().map_err(|e| {
                    AutomationError::PlatformError(format!("Failed to get monitor x: {e}"))
                })?;
                let y = monitor.y().map_err(|e| {
                    AutomationError::PlatformError(format!("Failed to get monitor y: {e}"))
                })?;
                let scale_factor = monitor.scale_factor().map_err(|e| {
                    AutomationError::PlatformError(format!(
                        "Failed to get monitor scale factor: {e}"
                    ))
                })? as f64;

                // Get work area for primary monitor
                let work_area = {
                    #[cfg(target_os = "windows")]
                    {
                        use crate::platforms::windows::element::WorkArea;
                        WorkArea::get_primary().ok().map(|work_area| crate::WorkAreaBounds {
                            x: work_area.x,
                            y: work_area.y,
                            width: work_area.width as u32,
                            height: work_area.height as u32,
                        })
                    }
                    #[cfg(not(target_os = "windows"))]
                    {
                        // Non-Windows: best-effort; treat full monitor bounds as "work area".
                        Some(crate::WorkAreaBounds {
                            x,
                            y,
                            width,
                            height,
                        })
                    }
                };

                return Ok(crate::Monitor {
                    id: format!("monitor_{idx}"),
                    name,
                    is_primary,
                    width,
                    height,
                    x,
                    y,
                    scale_factor,
                    work_area,
                });
            }
        }

        Err(AutomationError::PlatformError(
            "No primary monitor found".to_string(),
        ))
    }
}

impl UIElement {
    /// Create a new UI element from a platform-specific implementation
    pub fn new(impl_: Box<dyn UIElementImpl>) -> Self {
        Self { inner: impl_ }
    }

    /// Get the element's ID
    #[instrument(level = "debug", skip(self))]
    pub fn id(&self) -> Option<String> {
        self.inner.id()
    }

    /// Get the element's role (e.g., "button", "textfield")
    pub fn role(&self) -> String {
        self.inner.role()
    }

    /// Get all attributes of the element
    pub fn attributes(&self) -> UIElementAttributes {
        self.inner.attributes()
    }

    /// Get child elements
    pub fn children(&self) -> Result<Vec<UIElement>, AutomationError> {
        self.inner.children()
    }

    /// Get parent element
    pub fn parent(&self) -> Result<Option<UIElement>, AutomationError> {
        self.inner.parent()
    }

    /// Get element bounds (x, y, width, height)
    pub fn bounds(&self) -> Result<(f64, f64, f64, f64), AutomationError> {
        self.inner.bounds()
    }

    /// Build overlay info string from element name and role
    #[cfg(target_os = "windows")]
    fn overlay_info(&self) -> String {
        let name = self.name().unwrap_or_default();
        let role = self.role();
        if name.is_empty() {
            role
        } else if name.len() > 50 {
            format!("'{}...' {}", &name[..47], role)
        } else {
            format!("'{}' {}", name, role)
        }
    }

    /// Click on this element
    #[instrument(level = "debug", skip(self))]
    pub fn click(&self) -> Result<ClickResult, AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay("Clicking", Some(self.overlay_info()));
        let result = self.inner.click();
        #[cfg(target_os = "windows")]
        hide_action_overlay();
        result
    }

    /// Double-click on this element
    #[instrument(level = "debug", skip(self))]
    pub fn double_click(&self) -> Result<ClickResult, AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay("Double-clicking", Some(self.overlay_info()));
        let result = self.inner.double_click();
        #[cfg(target_os = "windows")]
        hide_action_overlay();
        result
    }

    /// Right-click on this element
    #[instrument(level = "debug", skip(self))]
    pub fn right_click(&self) -> Result<(), AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay("Right-clicking", Some(self.overlay_info()));
        let result = self.inner.right_click();
        #[cfg(target_os = "windows")]
        hide_action_overlay();
        result
    }

    /// Click at a specific position within the element bounds
    /// x_pct and y_pct are percentages (0-100) from top-left corner
    #[instrument(level = "debug", skip(self))]
    pub fn click_at_position(
        &self,
        x_pct: u8,
        y_pct: u8,
        click_type: crate::ClickType,
    ) -> Result<ClickResult, AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay("Clicking", Some(self.overlay_info()));
        let result = self.inner.click_at_position(x_pct, y_pct, click_type);
        #[cfg(target_os = "windows")]
        hide_action_overlay();
        result
    }

    /// Hover over this element
    pub fn hover(&self) -> Result<(), AutomationError> {
        self.inner.hover()
    }

    /// Focus this element
    pub fn focus(&self) -> Result<(), AutomationError> {
        self.inner.focus()
    }

    /// Invoke this element
    #[instrument(level = "debug", skip(self))]
    pub fn invoke(&self) -> Result<(), AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay("Invoking", Some(self.overlay_info()));
        let result = self.inner.invoke();
        #[cfg(target_os = "windows")]
        hide_action_overlay();
        result
    }

    /// Invoke this element with state tracking
    #[instrument(level = "debug", skip(self))]
    pub fn invoke_with_state(&self) -> Result<crate::ActionResult, AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay("Invoking", Some(self.overlay_info()));
        let result = self.inner.invoke_with_state();
        #[cfg(target_os = "windows")]
        hide_action_overlay();
        result
    }

    /// Type text into this element
    pub fn type_text(&self, text: &str, use_clipboard: bool) -> Result<(), AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay("Typing", Some(self.overlay_info()));
        // Default: try both focus and click, no focus restore
        let result = self.inner.type_text(text, use_clipboard, true, true, false);
        #[cfg(target_os = "windows")]
        hide_action_overlay();
        result
    }

    /// Type text with state tracking
    #[instrument(level = "debug", skip(self))]
    pub fn type_text_with_state(
        &self,
        text: &str,
        use_clipboard: bool,
    ) -> Result<crate::ActionResult, AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay("Typing", Some(self.overlay_info()));
        // Default: try both focus and click
        let result = self
            .inner
            .type_text_with_state(text, use_clipboard, true, true);
        #[cfg(target_os = "windows")]
        hide_action_overlay();
        result
    }

    /// Type text with state tracking and custom focus/click behavior
    #[instrument(level = "debug", skip(self))]
    pub fn type_text_with_state_and_focus(
        &self,
        text: &str,
        use_clipboard: bool,
        try_focus_before: bool,
        try_click_before: bool,
    ) -> Result<crate::ActionResult, AutomationError> {
        self.type_text_with_state_and_focus_restore(
            text,
            use_clipboard,
            try_focus_before,
            try_click_before,
            false,
        )
    }

    /// Type text with state tracking, custom focus/click behavior, and optional focus restoration
    #[instrument(level = "debug", skip(self))]
    pub fn type_text_with_state_and_focus_restore(
        &self,
        text: &str,
        use_clipboard: bool,
        try_focus_before: bool,
        try_click_before: bool,
        restore_focus: bool,
    ) -> Result<crate::ActionResult, AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay("Typing", Some(self.overlay_info()));

        // Call the underlying type_text with restore_focus
        let type_result = self.inner.type_text(
            text,
            use_clipboard,
            try_focus_before,
            try_click_before,
            restore_focus,
        );

        #[cfg(target_os = "windows")]
        hide_action_overlay();

        type_result?;

        // Auto-verify by reading the value back
        let verification = match self.inner.get_value() {
            Ok(Some(actual)) => {
                let passed = actual.contains(text);
                Some(crate::TypeVerification {
                    passed,
                    expected: text.to_string(),
                    actual: Some(actual),
                    error: if passed {
                        None
                    } else {
                        Some("Value does not contain expected text".to_string())
                    },
                })
            }
            Ok(None) => None,
            Err(_) => None,
        };

        Ok(crate::ActionResult {
            action: "type_text".to_string(),
            details: format!(
                "Typed {} chars with restore_focus={}",
                text.len(),
                restore_focus
            ),
            data: Some(serde_json::json!({
                "text": text,
                "use_clipboard": use_clipboard,
                "try_focus_before": try_focus_before,
                "try_click_before": try_click_before,
                "restore_focus": restore_focus,
            })),
            verification,
        })
    }

    /// Press a key while this element is focused
    pub fn press_key(&self, key: &str) -> Result<(), AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay(
            "Pressing key",
            Some(format!("{} on {}", key, self.overlay_info())),
        );
        // Default: try both focus and click, no focus restore
        let result = self.inner.press_key(key, true, true, false);
        #[cfg(target_os = "windows")]
        hide_action_overlay();
        result
    }

    /// Press a key with state tracking
    #[instrument(level = "debug", skip(self))]
    pub fn press_key_with_state(&self, key: &str) -> Result<crate::ActionResult, AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay(
            "Pressing key",
            Some(format!("{} on {}", key, self.overlay_info())),
        );
        // Default: try both focus and click
        let result = self.inner.press_key_with_state(key, true, true);
        #[cfg(target_os = "windows")]
        hide_action_overlay();
        result
    }

    /// Press a key with state tracking and custom focus/click behavior
    #[instrument(level = "debug", skip(self))]
    pub fn press_key_with_state_and_focus(
        &self,
        key: &str,
        try_focus_before: bool,
        try_click_before: bool,
    ) -> Result<crate::ActionResult, AutomationError> {
        #[cfg(target_os = "windows")]
        show_action_overlay(
            "Pressing key",
            Some(format!("{} on {}", key, self.overlay_info())),
        );
        let result = self
            .inner
            .press_key_with_state(key, try_focus_before, try_click_before);
        #[cfg(target_os = "windows")]
        hide_action_overlay();
        result
    }

    /// Get text content of this element
    pub fn text(&self, max_depth: usize) -> Result<String, AutomationError> {
        self.inner.get_text(max_depth)
    }

    /// Get the value attribute of this element (text inputs, combo boxes, etc.)
    /// This fetches the actual value using the platform's Value pattern, not the cached attributes.
    pub fn get_value(&self) -> Result<Option<String>, AutomationError> {
        self.inner.get_value()
    }

    /// Set value of this element
    pub fn set_value(&self, value: &str) -> Result<(), AutomationError> {
        self.inner.set_value(value)
    }

    /// Check if element is enabled
    #[instrument(level = "debug", skip(self))]
    pub fn is_enabled(&self) -> Result<bool, AutomationError> {
        self.inner.is_enabled()
    }

    /// Check if element is visible
    pub fn is_visible(&self) -> Result<bool, AutomationError> {
        self.inner.is_visible()
    }

    /// Check if element is focused
    pub fn is_focused(&self) -> Result<bool, AutomationError> {
        self.inner.is_focused()
    }

    /// Perform a named action on this element
    pub fn perform_action(&self, action: &str) -> Result<(), AutomationError> {
        self.inner.perform_action(action)
    }

    /// Get the underlying implementation as a specific type
    pub(crate) fn as_any(&self) -> &dyn std::any::Any {
        self.inner.as_any()
    }

    /// Find elements matching the selector within this element
    pub fn locator(&self, selector: impl Into<Selector>) -> Result<Locator, AutomationError> {
        let selector = selector.into();
        self.inner.create_locator(selector)
    }

    /// Scroll the element in a given direction
    pub fn scroll(&self, direction: &str, amount: f64) -> Result<(), AutomationError> {
        self.inner.scroll(direction, amount)
    }

    /// Scroll with state tracking
    #[instrument(level = "debug", skip(self))]
    pub fn scroll_with_state(
        &self,
        direction: &str,
        amount: f64,
    ) -> Result<crate::ActionResult, AutomationError> {
        self.inner.scroll_with_state(direction, amount)
    }

    /// Activate the window containing this element (bring to foreground)
    pub fn activate_window(&self) -> Result<(), AutomationError> {
        self.inner.activate_window()
    }

    pub fn minimize_window(&self) -> Result<(), AutomationError> {
        self.inner.minimize_window()
    }

    pub fn maximize_window(&self) -> Result<(), AutomationError> {
        self.inner.maximize_window()
    }

    /// Maximize window using keyboard simulation (Win+Up)
    /// Use this for UWP apps where ShowWindow does not work
    pub fn maximize_window_keyboard(&self) -> Result<(), AutomationError> {
        self.inner.maximize_window_keyboard()
    }

    /// Minimize window using keyboard simulation (Win+Down)
    /// Use this for UWP apps where ShowWindow does not work
    pub fn minimize_window_keyboard(&self) -> Result<(), AutomationError> {
        self.inner.minimize_window_keyboard()
    }

    /// Get native window handle (HWND on Windows)
    pub fn get_native_window_handle(&self) -> Result<isize, AutomationError> {
        self.inner.get_native_window_handle()
    }

    /// Get the element's name
    #[instrument(level = "debug", skip(self))]
    pub fn name(&self) -> Option<String> {
        self.inner.name()
    }

    /// Check if element is keyboard focusable
    pub fn is_keyboard_focusable(&self) -> Result<bool, AutomationError> {
        self.inner.is_keyboard_focusable()
    }

    /// Drag mouse from start to end coordinates
    pub fn mouse_drag(
        &self,
        start_x: f64,
        start_y: f64,
        end_x: f64,
        end_y: f64,
    ) -> Result<(), AutomationError> {
        self.inner.mouse_drag(start_x, start_y, end_x, end_y)
    }

    /// Press and hold mouse at (x, y)
    pub fn mouse_click_and_hold(&self, x: f64, y: f64) -> Result<(), AutomationError> {
        self.inner.mouse_click_and_hold(x, y)
    }

    /// Move mouse to (x, y)
    pub fn mouse_move(&self, x: f64, y: f64) -> Result<(), AutomationError> {
        self.inner.mouse_move(x, y)
    }

    /// Release mouse button
    pub fn mouse_release(&self) -> Result<(), AutomationError> {
        self.inner.mouse_release()
    }

    /// Get the containing application element
    pub fn application(&self) -> Result<Option<UIElement>, AutomationError> {
        self.inner.application()
    }

    /// Get the containing window element (e.g., tab, dialog)
    pub fn window(&self) -> Result<Option<UIElement>, AutomationError> {
        self.inner.window()
    }

    /// Highlights the element with a colored border and optional text overlay.
    ///
    /// # Arguments
    /// * `color` - Optional BGR color code (32-bit integer). Default: 0x0000FF (red)
    /// * `duration` - Optional duration for the highlight.
    /// * `text` - Optional text to display as overlay. Text will be truncated to 30 characters.
    /// * `text_position` - Optional position for the text overlay (Top, Bottom, etc.)
    /// * `font_style` - Optional font styling (size, bold, color)
    pub fn highlight(
        &self,
        color: Option<u32>,
        duration: Option<std::time::Duration>,
        text: Option<&str>,
        text_position: Option<crate::TextPosition>,
        font_style: Option<crate::FontStyle>,
    ) -> Result<crate::HighlightHandle, AutomationError> {
        self.inner
            .highlight(color, duration, text, text_position, font_style)
    }

    /// Ensures element is visible and highlights it before performing an action.
    ///
    /// This is a convenience method that combines scrolling the element into view
    /// and applying a visual highlight. Useful for debugging and visual feedback
    /// during automation.
    ///
    /// # Arguments
    /// * `action_name` - Name of the action about to be performed (e.g., "click", "type").
    ///   Used for logging and as fallback text if role is unavailable.
    ///
    /// # Behavior
    /// 1. Attempts to focus the element (many apps auto-scroll focused elements into view)
    /// 2. Calls `scroll_into_view()` if element is still not visible
    /// 3. Applies a green highlight (500ms) with the element's role as text overlay
    ///
    /// # Returns
    /// * `Ok(Option<HighlightHandle>)` - Handle to control the highlight, or None if highlight failed
    /// * Scrolling failures are logged but don't cause errors (best-effort)
    pub fn highlight_before_action(
        &self,
        action_name: &str,
    ) -> Result<Option<crate::HighlightHandle>, AutomationError> {
        use tracing::{info, warn};

        let start = std::time::Instant::now();

        // Step 1: Ensure element is visible (focus + scroll)
        let _ = self.ensure_in_view();

        // Step 2: Apply highlight with hardcoded settings
        let duration = Some(std::time::Duration::from_millis(500));
        let color = Some(0x00FF00); // Green in BGR
        let role_text = self.role();
        let text = Some(role_text.as_str());
        let text_position = Some(crate::TextPosition::Top);
        let font_style = Some(crate::FontStyle {
            size: 12,
            bold: true,
            color: 0xFFFFFF, // White text
        });

        info!(
            "HIGHLIGHT_BEFORE_{} duration={:?} role={}",
            action_name.to_uppercase(),
            duration,
            role_text
        );

        let handle = match self.highlight(color, duration, text, text_position, font_style) {
            Ok(h) => Some(h),
            Err(e) => {
                warn!(
                    "Failed to apply highlighting before {} action: {}",
                    action_name, e
                );
                None
            }
        };

        info!(
            "[PERF] highlight_before_action: {}ms",
            start.elapsed().as_millis()
        );

        Ok(handle)
    }

    /// Ensures element is visible by focusing and scrolling it into view.
    ///
    /// This is useful before performing actions on elements that may be off-screen.
    /// Unlike `scroll_into_view()`, this method tries `focus()` first which often
    /// triggers the application to auto-scroll the element into view.
    ///
    /// # Strategy
    /// 1. Attempts to focus the element (many apps auto-scroll focused elements)
    /// 2. Calls `scroll_into_view()` to ensure visibility
    ///
    /// # Returns
    /// * `Ok(())` - Element should now be visible
    /// * Errors from scrolling are logged but don't cause failure (best-effort)
    pub fn ensure_in_view(&self) -> Result<(), AutomationError> {
        use tracing::{debug, info, warn};

        let start = std::time::Instant::now();

        // Step 1: Try focus first - many apps auto-scroll on focus
        match self.focus() {
            Ok(()) => {
                debug!("Focus succeeded, app may have auto-scrolled element into view");
                // Give the app a moment to process the focus
                std::thread::sleep(std::time::Duration::from_millis(50));
            }
            Err(e) => {
                debug!("Focus failed (non-fatal): {}", e);
            }
        }

        // Step 2: Ensure element is scrolled into view
        let scroll_start = std::time::Instant::now();
        if let Err(e) = self.scroll_into_view() {
            warn!("scroll_into_view failed (non-fatal): {}", e);
            // Continue anyway - scrolling is best-effort
        }
        info!(
            "[PERF] scroll_into_view: {}ms",
            scroll_start.elapsed().as_millis()
        );

        info!("[PERF] ensure_in_view: {}ms", start.elapsed().as_millis());

        Ok(())
    }

    /// Capture a screenshot of the element
    pub fn capture(&self) -> Result<ScreenshotResult, AutomationError> {
        self.inner.capture()
    }

    /// Capture a screenshot of the element and perform OCR to extract text
    ///
    /// # Returns
    /// * `Ok(String)` - The extracted text from the element screenshot
    /// * `Err(AutomationError)` - If screenshot capture or OCR fails
    ///
    /// # Examples
    /// ```rust
    /// use computeruse::Desktop;
    ///
    /// # async fn example() -> Result<(), computeruse::AutomationError> {
    /// let desktop = Desktop::new(false, false)?;
    /// let element = desktop.locator("role:Button").first(None).await?;
    /// let text = element.ocr().await?;
    /// println!("Button text: {}", text);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn ocr(&self) -> Result<String, AutomationError> {
        // First capture the element screenshot
        let screenshot = self.capture()?;

        // Convert the screenshot to a DynamicImage
        let img_buffer = image::ImageBuffer::from_raw(
            screenshot.width,
            screenshot.height,
            screenshot.image_data,
        )
        .ok_or_else(|| {
            AutomationError::PlatformError(
                "Failed to create image buffer from screenshot data".to_string(),
            )
        })?;

        let dynamic_image = image::DynamicImage::ImageRgba8(img_buffer);

        // Perform OCR using uni_ocr directly
        let engine = uni_ocr::OcrEngine::new(uni_ocr::OcrProvider::Auto).map_err(|e| {
            AutomationError::PlatformError(format!("Failed to create OCR engine: {e}"))
        })?;

        let (text, _language, _confidence) = engine
            .recognize_image(&dynamic_image)
            .await
            .map_err(|e| AutomationError::PlatformError(format!("OCR recognition failed: {e}")))?;

        Ok(text)
    }

    /// Close the element if it's closable (like windows, applications)
    /// Does nothing for non-closable elements (like buttons, text, etc.)
    pub fn close(&self) -> Result<(), AutomationError> {
        self.inner.close()
    }

    /// Get the URL if the element is in a browser window
    pub fn url(&self) -> Option<String> {
        self.inner.url()
    }

    /// Selects an option in a dropdown or combobox by its visible text.
    pub fn select_option(&self, option_name: &str) -> Result<(), AutomationError> {
        self.inner.select_option(option_name)
    }

    /// Selects an option with state tracking
    #[instrument(level = "debug", skip(self))]
    pub fn select_option_with_state(
        &self,
        option_name: &str,
    ) -> Result<crate::ActionResult, AutomationError> {
        self.inner.select_option_with_state(option_name)
    }

    /// Lists all available option strings from a dropdown or list box.
    pub fn list_options(&self) -> Result<Vec<String>, AutomationError> {
        self.inner.list_options()
    }

    /// Checks if a control (like a checkbox or toggle switch) is currently toggled on.
    pub fn is_toggled(&self) -> Result<bool, AutomationError> {
        self.inner.is_toggled()
    }

    /// Sets the state of a toggleable control.
    /// It only performs an action if the control is not already in the desired state.
    pub fn set_toggled(&self, state: bool) -> Result<(), AutomationError> {
        self.inner.set_toggled(state)
    }

    /// Set toggle state with state tracking
    #[instrument(level = "debug", skip(self))]
    pub fn set_toggled_with_state(
        &self,
        state: bool,
    ) -> Result<crate::ActionResult, AutomationError> {
        self.inner.set_toggled_with_state(state)
    }

    /// Gets the current value from a range-based control like a slider or progress bar.
    pub fn get_range_value(&self) -> Result<f64, AutomationError> {
        self.inner.get_range_value()
    }

    /// Sets the value of a range-based control like a slider.
    pub fn set_range_value(&self, value: f64) -> Result<(), AutomationError> {
        self.inner.set_range_value(value)
    }

    /// Checks if a selectable item (e.g., in a calendar, list, or tab) is currently selected.
    pub fn is_selected(&self) -> Result<bool, AutomationError> {
        self.inner.is_selected()
    }

    /// Sets the selection state of a selectable item.
    pub fn set_selected(&self, state: bool) -> Result<(), AutomationError> {
        self.inner.set_selected(state)
    }

    /// Set selection state with state tracking
    #[instrument(level = "debug", skip(self))]
    pub fn set_selected_with_state(
        &self,
        state: bool,
    ) -> Result<crate::ActionResult, AutomationError> {
        self.inner.set_selected_with_state(state)
    }

    /// Return the `Monitor` that contains this UI element.
    ///
    /// This is useful when you need to perform monitor-specific operations
    /// (e.g. capturing the screen area around the element).
    pub fn monitor(&self) -> Result<crate::Monitor, AutomationError> {
        self.inner.monitor()
    }

    // Convenience methods to reduce verbosity with optional properties

    /// Scrolls until the element is visible within its window viewport.
    ///
    /// Strategy:
    /// - If already visible, returns immediately.
    /// - Otherwise, estimates direction based on the element vs window bounds and
    ///   issues small scroll steps using the existing `scroll` implementation
    ///   (which finds a scrollable ancestor and uses UIScrollPattern or key fallbacks).
    /// - Re-checks visibility and bounds after each step and stops when visible
    ///   or when the maximum number of steps is reached.
    pub fn scroll_into_view(&self) -> Result<(), AutomationError> {
        // Configuration tuned for reliability without over-scrolling
        const MAX_STEPS: usize = 24; // up to ~24 directional adjustments
        const STEP_AMOUNT: f64 = 0.5; // Reduced from 1.0 to avoid over-scrolling - uses smaller increments
        const MIN_ELEMENT_SIZE: f64 = 5.0; // Minimum width/height for a valid UI element

        // Initial snapshot for diagnostics
        let init_visible = self.is_visible().unwrap_or(false);
        let init_bounds = self.bounds().ok();
        let window_bounds = self.window().ok().flatten().and_then(|w| w.bounds().ok());

        debug!(
            "scroll_into_view:start visible={:?} elem_bounds={:?} window_bounds={:?}",
            init_visible, init_bounds, window_bounds
        );

        // Fast path
        if init_visible {
            return Ok(());
        }

        // Validate element bounds - skip scrolling for invalid/tiny elements
        if let Some((x, y, width, height)) = init_bounds {
            // Check for invalid bounds (e.g., 1x1 pixel elements at 0,0)
            if width <= MIN_ELEMENT_SIZE && height <= MIN_ELEMENT_SIZE {
                warn!(
                    "scroll_into_view:skipping_invalid_element bounds=({}, {}, {}, {}) - element too small to scroll",
                    x, y, width, height
                );
                // Return Ok instead of error - element exists but can't be scrolled
                return Ok(());
            }

            // Additional check for elements at origin with minimal size (likely invalid)
            if x == 0.0 && y == 0.0 && width <= 1.0 && height <= 1.0 {
                warn!("scroll_into_view:skipping_placeholder_element at origin with 1x1 size");
                return Ok(());
            }
        }

        // Iteratively adjust
        let mut steps_taken: usize = 0;
        loop {
            // Refresh visibility and geometry each iteration
            let visible = self.is_visible().unwrap_or(false);
            let elem_bounds = match self.bounds() {
                Ok(b) => b,
                Err(e) => {
                    warn!("scroll_into_view:failed_to_get_bounds error={}", e);
                    return Err(e);
                }
            };

            // Early exit if bounds became invalid during scrolling (e.g., element got hidden/repositioned)
            let (x, y, width, height) = elem_bounds;
            if width <= MIN_ELEMENT_SIZE && height <= MIN_ELEMENT_SIZE {
                warn!(
                    "scroll_into_view:bounds_became_invalid step={} bounds=({}, {}, {}, {}) - stopping scroll",
                    steps_taken, x, y, width, height
                );
                return Ok(());
            }

            // If visible with valid bounds, we're done - trust is_visible() for multi-monitor setups
            // The work_area check only considers primary monitor which breaks for secondary monitors
            if visible {
                info!(
                    "scroll_into_view:done (visible=true) steps_taken={} final_bounds={:?}",
                    steps_taken, elem_bounds
                );
                return Ok(());
            }

            // Determine scroll directions based on position relative to window bounds and work area
            let mut vertical_dir: Option<&'static str> = None;
            let mut horizontal_dir: Option<&'static str> = None;

            // Check if element is behind taskbar (Windows only)
            #[cfg(target_os = "windows")]
            {
                use crate::platforms::windows::element::WorkArea;
                if let Ok(work_area) = WorkArea::get_primary() {
                    let (_ex, ey, _ew, eh) = elem_bounds;
                    let work_bottom = (work_area.y + work_area.height) as f64;

                    // If element is below work area (behind taskbar), scroll down
                    if ey + eh > work_bottom {
                        vertical_dir = Some("down");
                    }
                }
            }

            // Standard scroll direction logic based on window bounds
            if vertical_dir.is_none() {
                if let Some((wx, wy, ww, wh)) = window_bounds {
                    let (ex, ey, ew, eh) = elem_bounds;
                    // Vertical
                    if ey + eh <= wy {
                        // Element is above the viewport -> scroll up
                        vertical_dir = Some("up");
                    } else if ey >= wy + wh {
                        // Element is below the viewport -> scroll down
                        vertical_dir = Some("down");
                    }
                    // Horizontal (best effort)
                    if ex + ew <= wx {
                        horizontal_dir = Some("left");
                    } else if ex >= wx + ww {
                        horizontal_dir = Some("right");
                    }
                } else {
                    // Without window bounds, attempt a sensible default sequence
                    // Try down first (common case), then up; we alternate if no progress.
                    vertical_dir = Some(if steps_taken.is_multiple_of(2) {
                        "down"
                    } else {
                        "up"
                    });
                }
            }

            // Perform one vertical step if needed
            if let Some(dir) = vertical_dir {
                debug!(
                    "scroll_into_view:vertical_step dir={} step={} amount={}",
                    dir,
                    steps_taken + 1,
                    STEP_AMOUNT
                );
                // Ignore individual step errors and continue to try alternate axes
                let _ = self.scroll(dir, STEP_AMOUNT);
                steps_taken += 1;
            }

            // Perform one horizontal step if needed (after vertical)
            if let Some(dir) = horizontal_dir {
                debug!(
                    "scroll_into_view:horizontal_step dir={} step={} amount={}",
                    dir,
                    steps_taken + 1,
                    STEP_AMOUNT
                );
                let _ = self.scroll(dir, STEP_AMOUNT);
                steps_taken += 1;
            }

            // Safety cap
            if steps_taken >= MAX_STEPS {
                return Err(AutomationError::Timeout(format!(
                    "scroll_into_view: exceeded max steps ({MAX_STEPS}). elem_bounds={elem_bounds:?} window_bounds={window_bounds:?}"
                )));
            }

            // Small delay to allow the UI to update between scrolls
            std::thread::sleep(std::time::Duration::from_millis(60));
        }
    }

    /// Get element ID or empty string if not available
    pub fn id_or_empty(&self) -> String {
        self.id().unwrap_or_default()
    }

    /// Get element name or empty string if not available  
    pub fn name_or_empty(&self) -> String {
        self.name().unwrap_or_default()
    }

    /// Get element name or fallback string if not available
    pub fn name_or(&self, fallback: &str) -> String {
        self.name().unwrap_or_else(|| fallback.to_string())
    }

    /// Get element value or empty string if not available
    pub fn value_or_empty(&self) -> String {
        self.attributes().value.unwrap_or_default()
    }

    /// Get element description or empty string if not available
    pub fn description_or_empty(&self) -> String {
        self.attributes().description.unwrap_or_default()
    }

    /// Get application name safely
    pub fn application_name(&self) -> String {
        self.application()
            .ok()
            .flatten()
            .and_then(|app| app.name())
            .unwrap_or_default()
    }

    /// Get window title safely
    pub fn window_title(&self) -> String {
        match self.window() {
            Ok(Some(window)) => window.name_or_empty(),
            _ => String::new(),
        }
    }

    /// Convert this UIElement to a SerializableUIElement
    ///
    /// This creates a snapshot of the element's current state that can be
    /// serialized to JSON, stored in files, or transmitted over networks.
    pub fn to_serializable(&self) -> SerializableUIElement {
        SerializableUIElement::from(self)
    }

    /// Sets the transparency of the window.
    /// The percentage value ranges from 0 (completely transparent) to 100 (completely opaque).
    pub fn set_transparency(&self, percentage: u8) -> Result<(), AutomationError> {
        self.inner.set_transparency(percentage)
    }

    /// Get the process ID of the application containing this element
    pub fn process_id(&self) -> Result<u32, AutomationError> {
        self.inner.process_id()
    }

    /// Get the process name of the application containing this element
    #[cfg(target_os = "windows")]
    pub fn process_name(&self) -> Result<String, AutomationError> {
        let pid = self.process_id()?;
        crate::platforms::windows::get_process_name_by_pid(pid as i32)
    }

    /// Get the process name of the application containing this element
    #[cfg(target_os = "macos")]
    pub fn process_name(&self) -> Result<String, AutomationError> {
        // macOS: use sysinfo to get process name
        use sysinfo::{ProcessesToUpdate, System};
        let pid = self.process_id()?;
        let mut system = System::new();
        system.refresh_processes(ProcessesToUpdate::All, true);
        system
            .process(sysinfo::Pid::from_u32(pid))
            .map(|p| p.name().to_string_lossy().to_string())
            .ok_or_else(|| AutomationError::Internal(format!("Process {} not found", pid)))
    }

    /// Get the process name of the application containing this element
    #[cfg(target_os = "linux")]
    pub fn process_name(&self) -> Result<String, AutomationError> {
        // Linux: use sysinfo to get process name
        use sysinfo::{ProcessesToUpdate, System};
        let pid = self.process_id()?;
        let mut system = System::new();
        system.refresh_processes(ProcessesToUpdate::All, true);
        system
            .process(sysinfo::Pid::from_u32(pid))
            .map(|p| p.name().to_string_lossy().to_string())
            .ok_or_else(|| AutomationError::Internal(format!("Process {} not found", pid)))
    }

    /// Recursively build a SerializableUIElement tree from this element.
    ///
    /// # Arguments
    /// * `max_depth` - Maximum depth to traverse (inclusive). Use a reasonable limit to avoid huge trees.
    ///
    /// # Example
    /// ```no_run
    /// # use computeruse::Desktop;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let desktop = Desktop::new(false, false)?;
    /// let element = desktop.locator("name:Calculator").first(None).await?;
    /// let tree = element.to_serializable_tree(5);
    /// println!("{}", serde_json::to_string_pretty(&tree).unwrap());
    /// # Ok(())
    /// # }
    /// ```
    pub fn to_serializable_tree(&self, max_depth: usize) -> SerializableUIElement {
        fn build(element: &UIElement, depth: usize, max_depth: usize) -> SerializableUIElement {
            let mut serializable = element.to_serializable();

            // For child elements (depth > 0), remove redundant window/app info.
            // This information is only needed at the root of the tree.
            if depth > 0 {
                serializable.window_and_application_name = None;
                serializable.window_title = None;
                serializable.process_id = None;
                serializable.process_name = None;
                serializable.url = None;
            }

            let children = if depth < max_depth {
                match element.children() {
                    Ok(children) => {
                        let v: Vec<SerializableUIElement> = children
                            .iter()
                            .map(|child| build(child, depth + 1, max_depth))
                            .collect();
                        if v.is_empty() {
                            None
                        } else {
                            Some(v)
                        }
                    }
                    Err(_) => None,
                }
            } else {
                None
            };
            serializable.children = children;
            serializable
        }
        build(self, 0, max_depth)
    }

    /// Execute JavaScript in the browser using dev tools console
    /// Opens dev tools with F12, switches to console, runs script, extracts result
    pub async fn execute_browser_script(&self, script: &str) -> Result<String, AutomationError> {
        crate::browser_script::execute_script(self, script).await
    }
}

impl PartialEq for UIElement {
    fn eq(&self, other: &Self) -> bool {
        self.inner.object_id() == other.inner.object_id()
    }
}

impl Eq for UIElement {}

impl std::hash::Hash for UIElement {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.inner.object_id().hash(state);
    }
}

impl Clone for UIElement {
    fn clone(&self) -> Self {
        // We can't directly clone the inner Box<dyn UIElementImpl>,
        // but we can create a new UIElement with the same identity
        // that will behave the same way
        Self {
            inner: self.inner.clone_box(),
        }
    }
}

/// Utility functions for working with UI elements
pub mod utils {
    use super::*;

    /// Get the display text for an element (name, value, or role as fallback)
    pub fn display_text(element: &UIElement) -> String {
        element
            .name()
            .or_else(|| element.attributes().value)
            .unwrap_or_else(|| element.role())
    }

    /// Check if element has any text content
    pub fn has_text_content(element: &UIElement) -> bool {
        element.name().is_some()
            || element.attributes().value.is_some()
            || !element.text(1).unwrap_or_default().trim().is_empty()
    }

    /// Get a human-readable identifier for the element
    pub fn element_identifier(element: &UIElement) -> String {
        if let Some(name) = element.name() {
            format!("{} ({})", name, element.role())
        } else if let Some(id) = element.id() {
            format!("#{} ({})", id, element.role())
        } else {
            element.role()
        }
    }

    /// Create a minimal attributes struct with just the essentials
    pub fn essential_attributes(element: &UIElement) -> UIElementAttributes {
        UIElementAttributes {
            role: element.role(),
            name: element.name(),
            text: None,
            value: element.attributes().value,
            application_name: None, // Deferred - avoid expensive calls
            bounds: None,           // Not included in minimal attributes
            ..Default::default()
        }
    }
}

/// Serialize implementation for UIElement
///
/// This implementation serializes the accessible properties of a UI element to JSON.
/// The following fields are included in the serialized output:
/// - `id`: Element identifier (if available)
/// - `role`: Element role (e.g., "button", "textfield")
/// - `name`: Element name/label (if available)
/// - `bounds`: Element position and size as (x, y, width, height)
/// - `value`: Element value (if available)
/// - `description`: Element description (if available)
/// - `application`: Name of the containing application
/// - `window_title`: Title of the containing window
///
/// Note: This serializes the element's current state and properties, but does not
/// serialize the underlying platform-specific implementation or maintain any
/// interactive capabilities.
impl Serialize for UIElement {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.to_serializable().serialize(serializer)
    }
}

// TODO: Deserialize is pretty much very experimental and untested

/// Deserialize implementation for UIElement
///
/// This implementation attempts to find the actual UI element in the current UI tree
/// using the deserialized data (ID, role, name, bounds). If the element cannot be found,
/// deserialization fails with an error.
///
/// This ensures all UIElement instances are always "live" and can perform UI operations.
/// There are no more "mock" or "dead" elements - if deserialization succeeds, the element
/// exists and can be interacted with.
///
/// Search strategy:
/// 1. Try to find by ID if available
/// 2. Try to find by role + name combination
/// 3. Verify bounds match (with 10px tolerance) if available
///
/// Note: This approach requires the UI element to actually exist in the current UI tree
/// at the time of deserialization. If the UI has changed since serialization,
/// deserialization will fail.
impl<'de> Deserialize<'de> for UIElement {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::de::Error;

        // First deserialize into our SerializableUIElement
        let serializable = SerializableUIElement::deserialize(deserializer)?;

        // Try to find the actual live element
        // Note: find_live_element now returns None instead of panicking in async contexts
        find_live_element(&serializable).ok_or_else(|| {
            // Check if we're in an async context - if so, provide a more helpful error message
            if tokio::runtime::Handle::try_current().is_ok() {
                Error::custom(format!(
                    "UIElement deserialization skipped in async context (role='{}', name={:?})",
                    serializable.role, serializable.name
                ))
            } else {
                Error::custom(format!(
                    "Could not find UI element with role '{}' and name '{:?}' in current UI tree",
                    serializable.role, serializable.name
                ))
            }
        })
    }
}

/// Attempts to find a live UI element matching the serializable data
fn find_live_element(serializable: &SerializableUIElement) -> Option<UIElement> {
    // Try to create a Desktop instance and search the UI tree
    // If any step fails (Desktop creation or element search), return None
    std::panic::catch_unwind(|| {
        // Desktop::new is now synchronous, so we can call it directly
        let desktop = crate::Desktop::new(false, false).ok()?;

        // Check if we're already in an async runtime context
        if let Ok(_handle) = tokio::runtime::Handle::try_current() {
            // We're already in an async context, use the existing runtime
            // This happens during MCP workflow conversion
            // We can't use block_on here, so just return None
            tracing::debug!(
                "Skipping live element lookup for role='{}', name={:?} (in async context)",
                serializable.role,
                serializable.name
            );
            return None;
        }

        // We're in a sync context, create a new runtime for the async operation
        let rt = tokio::runtime::Runtime::new().ok()?;
        rt.block_on(async { find_element_in_tree(&desktop, serializable).await })
    })
    .unwrap_or(None)
}

/// Helper function to search for element in the UI tree
async fn find_element_in_tree(
    desktop: &crate::Desktop,
    serializable: &SerializableUIElement,
) -> Option<crate::UIElement> {
    // Try to find by ID first
    if let Some(ref id) = serializable.id {
        let id_selector = format!("#{id}");
        if let Ok(element) = desktop
            .locator(id_selector.as_str())
            .first(Some(std::time::Duration::from_secs(1)))
            .await
        {
            return Some(element);
        }
    }

    // Try to find by role and name
    let mut selector = format!("role:{}", serializable.role);
    if let Some(ref name) = serializable.name {
        selector = format!("{selector}name:{name}");
    }

    if let Ok(element) = desktop
        .locator(selector.as_str())
        .first(Some(std::time::Duration::from_secs(1)))
        .await
    {
        // Verify bounds match (with tolerance) if available
        if let Some((target_x, target_y, target_w, target_h)) = serializable.bounds {
            if let Ok((fx, fy, fw, fh)) = element.bounds() {
                let tolerance = 10.0; // 10 pixel tolerance

                if (fx - target_x).abs() <= tolerance
                    && (fy - target_y).abs() <= tolerance
                    && (fw - target_w).abs() <= tolerance
                    && (fh - target_h).abs() <= tolerance
                {
                    return Some(element);
                }
            }
        } else {
            // If no bounds to check, return the element
            return Some(element);
        }
    }

    None
}