File size: 33,203 Bytes
f0ccdab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import MentraBluetoothSDK
import SwiftUI

struct DeviceScreen: View {
    @ObservedObject var model: BluetoothViewModel
    @Environment(\.keyboardVisible) private var keyboardVisible

    var body: some View {
        ScrollView {
            VStack(spacing: 0) {
                PageHeader(title: "Device")

                if model.glassesConnected {
                    heroCard
                        .padding(.horizontal, 16)
                        .padding(.top, 8)

                    statRow
                        .padding(.horizontal, 16)
                        .padding(.top, 12)

                    if model.otaStatus != nil || model.otaUpdateAvailable || model.otaNoUpdateVisible {
                        otaCard
                            .padding(.horizontal, 16)
                            .padding(.top, 10)
                    }

                    if !model.glassesWifiConnected {
                        OfflineNotice(message: "Connect the glasses to Wi-Fi from the System tab before checking or starting OTA updates. OTA downloads run over the glasses network connection.")
                            .padding(.horizontal, 16)
                            .padding(.top, 8)
                    }
                }

                quickActions
                    .padding(.horizontal, 16)
                    .padding(.top, model.glassesConnected ? 16 : 8)

                liveStatus
                    .padding(.horizontal, 16)
                    .padding(.top, 16)
            }
            .padding(.bottom, LayoutMetric.scrollBottomPadding(keyboardVisible: keyboardVisible))
        }
        .background(AppColor.bg)
        .scrollDismissesKeyboard(.interactively)
    }

    private var heroCard: some View {
        GlassCard {
            HStack(alignment: .top) {
                VStack(alignment: .leading, spacing: 4) {
                    Text(connectionLabel(model.glassesValues))
                        .font(.system(size: 10, weight: .semibold))
                        .tracking(1.2)
                        .foregroundColor(AppColor.greenAccent)
                    Text(modelLabel(model.glassesValues))
                        .font(.system(size: 28, weight: .heavy))
                        .tracking(-0.7)
                        .foregroundColor(AppColor.ink)
                    Text(deviceLabel(model.glassesValues))
                        .font(.system(size: 11, weight: .medium))
                        .foregroundColor(AppColor.muted)
                }
                Spacer()
                Image(glassesAssetName(model.glassesValues))
                    .resizable()
                    .scaledToFit()
                    .frame(width: 145, height: 52)
            }
            Divider().background(AppColor.hairline).padding(.top, 14).padding(.bottom, 12)
            HStack(alignment: .bottom) {
                VStack(alignment: .leading, spacing: 4) {
                    Text("BATTERY")
                        .font(.system(size: 10, weight: .semibold))
                        .tracking(1.2)
                        .foregroundColor(AppColor.muted)
                    HStack(alignment: .lastTextBaseline, spacing: 6) {
                        Text(batteryLevel(model.glassesValues).map(String.init) ?? "--")
                            .font(.system(size: 56, weight: .heavy))
                            .tracking(-2.2)
                            .foregroundColor(AppColor.ink)
                        Text("%")
                            .font(.system(size: 22, weight: .semibold))
                            .foregroundColor(AppColor.muted)
                    }
                    HStack(spacing: 6) {
                        Image(systemName: "bolt.fill").foregroundColor(AppColor.greenAccent).font(.system(size: 11))
                        Text(model.glassesValues?.battery?.charging == true ? "Charging" : "Not charging").font(.system(size: 12, weight: .semibold)).foregroundColor(AppColor.greenAccent)
                    }
                }
                Spacer()
                HStack(alignment: .bottom, spacing: 4) {
                    ForEach(0..<7, id: \.self) { i in
                        let heights: [CGFloat] = [14, 22, 30, 38, 46, 54, 62]
                        RoundedRectangle(cornerRadius: 3)
                            .fill((batteryLevel(model.glassesValues) ?? 0) > Int(Double(i) / 7.0 * 100.0) ? AppColor.greenAccent : Color.black.opacity(0.06))
                            .frame(width: 6, height: heights[i])
                    }
                }
                .padding(.bottom, 6)
            }
        }
    }

    private var statRow: some View {
        let currentWifi = connectedWifiStatus(model.glassesValues)
        return HStack(spacing: 10) {
            StatTile(label: "FIRMWARE", value: firmwareCardLabel(model: model), sub: firmwareCardSubLabel(model: model), subColor: AppColor.greenAccent)
            StatTile(label: "WI-FI", value: wifiLabel(model.glassesValues), sub: currentWifi?.localIp ?? "unknown", subColor: AppColor.muted, bold: true)
            StatTile(label: "RSSI", value: rssiLabel(model.glassesValues), sub: rssiUpdatedLabel(model.glassesValues), subColor: AppColor.greenAccent, bold: true)
        }
    }

    private var quickActions: some View {
        let connected = model.glassesConnected
        let canConnect = !connected && canConnectTarget(model: model)
        let hasDefaultTarget = hasSavedConnectionTarget(model.bluetoothValues)
        let displaySupported = connected && supportsDisplay(model.glassesValues)
        let otaWifiRequired = connected && !model.glassesWifiConnected
        let otaInProgress = isOtaInProgress(model: model)
        let canCheckOta = connected && model.glassesWifiConnected && !otaInProgress
        return GlassCard {
            HStack {
                Text(connected ? "Quick actions" : "Connect glasses")
                    .font(.system(size: 16, weight: .bold))
                    .foregroundColor(AppColor.inkAlt)
                Spacer()
                Text("SDK")
                    .font(.system(size: 10, weight: .semibold).monospaced())
                    .tracking(1.6)
                    .foregroundColor(AppColor.inkAlt.opacity(0.4))
            }
            .padding(.bottom, 16)

            scanModelPicker
                .padding(.bottom, 12)

            targetPicker
                .padding(.bottom, 12)

            VStack(spacing: 8) {
                HStack(spacing: 8) {
                    DarkActionButton(icon: "magnifyingglass", title: "Scan", bg: Color(hex: 0x0E2C1A), enabled: !connected, action: model.startScan)
                    DarkActionButton(icon: "link", title: connected ? "Connected" : "Connect", bg: AppColor.greenPrimary, enabled: canConnect, action: model.connect)
                }
                HStack(spacing: 8) {
                    LightActionButton(icon: "display", title: "Display Hello", enabled: displaySupported, action: model.displayHello)
                    LightActionButton(icon: "display.slash", title: "Clear Display", enabled: displaySupported, action: model.clearDisplay)
                }
                HStack(spacing: 8) {
                    LightActionButton(icon: "arrow.triangle.2.circlepath", title: otaWifiRequired ? "Connect Wi-Fi" : "Check OTA", enabled: canCheckOta, action: model.checkForOtaUpdate)
                    LightActionButton(icon: "arrow.down.to.line", title: model.otaStartPending ? "Starting OTA..." : "Start OTA", enabled: canStartOta(model: model), action: model.startOtaUpdate)
                }
                HStack(spacing: 8) {
                    LightActionButton(icon: "trash", title: "Clear Default", enabled: hasDefaultTarget, action: model.clearDefaultDevice)
                    let disconnecting = model.activeAction == "Disconnect"
                    Button {
                        model.disconnect()
                    } label: {
                        HStack(spacing: 8) {
                            Image(systemName: "wifi.slash").foregroundColor(.white).font(.system(size: 14, weight: .bold))
                            Text(disconnecting ? "Disconnecting" : "Disconnect").foregroundColor(.white).font(.system(size: 13, weight: .semibold))
                        }
                        .frame(maxWidth: .infinity).padding(.vertical, 14)
                        .background(LinearGradient(colors: [Color(hex: 0xDE3A30), Color(hex: 0xC43B30)], startPoint: .top, endPoint: .bottom))
                        .clipShape(RoundedRectangle(cornerRadius: 18))
                    }
                    .disabled(!connected || disconnecting)
                    .opacity(connected ? 1 : 0.45)
                }
                if connected && !supportsDisplay(model.glassesValues) {
                    DisplayCapabilityNotice(text: "\(modelLabel(model.glassesValues)) has no display, so display commands are disabled.")
                }
            }
        }
    }

    private var scanModelPicker: some View {
        let connected = model.glassesConnected
        return VStack(alignment: .leading, spacing: 8) {
            HStack {
                Text("SCAN MODEL")
                    .font(.system(size: 10, weight: .semibold).monospaced())
                    .tracking(1.4)
                    .foregroundColor(AppColor.inkAlt.opacity(0.45))
                Spacer()
                if connected {
                    Text("Disconnect to change")
                        .font(.system(size: 10, weight: .semibold))
                        .foregroundColor(AppColor.muted)
                }
            }

            HStack(spacing: 8) {
                ForEach(scanModelOptions, id: \.self) { scanModel in
                    ScanModelChip(
                        model: scanModel,
                        active: model.selectedScanModel == scanModel,
                        enabled: !connected
                    ) {
                        model.selectScanModel(scanModel)
                    }
                }
            }
        }
    }

    private var targetPicker: some View {
        VStack(alignment: .leading, spacing: 8) {
            HStack {
                Text(model.glassesConnected ? "Connected device" : "Connection target")
                    .font(.system(size: 10, weight: .semibold).monospaced())
                    .tracking(1.4)
                    .foregroundColor(AppColor.inkAlt.opacity(0.45))
                Spacer()
                if !model.glassesConnected, !model.discoveredDevices.isEmpty {
                    Text("\(model.discoveredDevices.count) found")
                        .font(.system(size: 10, weight: .semibold))
                        .foregroundColor(AppColor.greenDeep)
                }
            }

            if model.glassesConnected {
                TargetDeviceRow(
                    name: deviceLabel(model.glassesValues),
                    detail: "Active BLE connection",
                    selected: true,
                    enabled: false,
                    action: {}
                )
            } else if model.discoveredDevices.isEmpty, let hint = model.scanHint {
                VStack(alignment: .leading, spacing: 6) {
                    Text("Glasses may be in use").font(.subheadline.weight(.semibold))
                    Text(hint).font(.subheadline).foregroundStyle(AppColor.muted)
                }
            } else if model.discoveredDevices.isEmpty, hasSavedConnectionTarget(model.bluetoothValues) {
                TargetDeviceRow(
                    name: savedConnectionTargetName(model.bluetoothValues),
                    detail: savedConnectionTargetDetail(model.bluetoothValues),
                    selected: true,
                    enabled: false,
                    action: {}
                )
            } else if model.discoveredDevices.isEmpty {
                TargetDeviceRow(
                    name: "Scan required",
                    detail: "No saved default target yet. Scan to choose nearby glasses.",
                    selected: false,
                    enabled: false,
                    action: {}
                )
            } else {
                ForEach(Array(model.discoveredDevices.enumerated()), id: \.offset) { _, device in
                    TargetDeviceRow(
                        name: device.name,
                        detail: targetDeviceDetail(device),
                        selected: (model.selectedDiscoveredDevice.map { discoveredDeviceKey($0) } ?? "") == discoveredDeviceKey(device),
                        enabled: true
                    ) {
                        model.selectDiscoveredDevice(device)
                    }
                }
            }
        }
        .padding(12)
        .background(AppColor.ink.opacity(0.035))
        .clipShape(RoundedRectangle(cornerRadius: 18))
    }

    private var liveStatus: some View {
        GlassCard(padding: EdgeInsets(top: 22, leading: 0, bottom: 22, trailing: 0)) {
            HStack {
                HStack(spacing: 8) {
                    Circle().fill(AppColor.greenPrimary).frame(width: 7, height: 7)
                    Text("Live status").font(.system(size: 16, weight: .bold)).foregroundColor(AppColor.inkAlt)
                }
                Spacer()
                Text("REC")
                    .font(.system(size: 10, weight: .semibold).monospaced())
                    .tracking(1.6)
                    .foregroundColor(AppColor.greenInk)
                    .padding(.horizontal, 10).padding(.vertical, 4)
                    .background(AppColor.greenInk.opacity(0.06))
                    .clipShape(Capsule())
            }
            .padding(.horizontal, 18)
            .padding(.bottom, 16)

            VStack(spacing: 0) {
                StatusKVRow(label: "LAST ACTION", value: model.lastAction, first: true)
                StatusKVRow(label: "CONNECTION", custom: AnyView(
                    HStack(spacing: 6) { Circle().fill(AppColor.greenPrimary).frame(width: 6, height: 6); Text(connectionLabel(model.glassesValues)).font(.system(size: 13, weight: .semibold)).foregroundColor(AppColor.greenInk) }
                ))
                StatusKVRow(label: "DEVICE", value: deviceLabel(model.glassesValues), mono: true)
                if shouldShowMentraLiveVersions(model.glassesValues) {
                    StatusKVRow(label: "ASG CLIENT", value: versionLabel(model.mentraLiveVersions.appVersion), mono: true)
                    StatusKVRow(label: "MTK", value: versionLabel(model.mentraLiveVersions.mtkFirmwareVersion), mono: true)
                    StatusKVRow(label: "BES", value: versionLabel(model.mentraLiveVersions.besFirmwareVersion), mono: true)
                }
                StatusKVRow(label: "BATTERY", custom: AnyView(
                    HStack(spacing: 8) {
                        Text(batteryLabel(model.glassesValues)).font(.system(size: 13, weight: .semibold)).foregroundColor(AppColor.inkAlt)
                        HStack(spacing: 4) {
                            Image(systemName: "bolt.fill").foregroundColor(AppColor.greenPrimary).font(.system(size: 8))
                            Text("charging").font(.system(size: 11, weight: .semibold)).foregroundColor(AppColor.greenPrimary)
                        }
                        .padding(.horizontal, 8).padding(.vertical, 2)
                        .background(AppColor.greenPrimary.opacity(0.08)).clipShape(Capsule())
                    }
                ))
                StatusKVRow(label: "BLUETOOTH", value: bluetoothSearchLabel(model.bluetoothValues))
                StatusKVRow(label: "TARGET", value: connectionTargetLabel(model: model), mono: true)
                StatusKVRow(label: "DISCOVERED", value: model.discoveredDevices.map(\.name).joined(separator: ", ").isEmpty ? "None yet" : model.discoveredDevices.map(\.name).joined(separator: ", "), mono: true)
                StatusKVRow(label: "PERMISSIONS", value: "iOS prompts as needed")
                StatusKVRow(label: "CAMERA", value: model.cameraStatus)
                StatusKVRow(label: "OTA", value: otaStatusLine(model: model))
                StatusKVRow(label: "LATEST EVENT", custom: AnyView(
                    VStack(alignment: .leading, spacing: 4) {
                        HStack(spacing: 6) {
                            HStack(spacing: 4) {
                                Circle().fill(AppColor.greenPrimary).frame(width: 5, height: 5)
                                Text(model.events.first?.tag ?? "NONE").font(.system(size: 10, weight: .semibold).monospaced()).foregroundColor(AppColor.greenPrimary)
                            }
                            .padding(.horizontal, 7).padding(.vertical, 2).background(AppColor.greenPrimary.opacity(0.08)).clipShape(RoundedRectangle(cornerRadius: 5))
                            Text(model.events.first?.time ?? "--:--:--").font(.system(size: 11, design: .monospaced)).foregroundColor(AppColor.inkAlt.opacity(0.65))
                        }
                        Text(model.events.first?.text ?? "No events yet").font(.system(size: 13, weight: .medium)).foregroundColor(AppColor.inkAlt)
                    }
                ))
            }
            .padding(.horizontal, 18)
        }
    }

    private var otaCard: some View {
        let updateRequired = model.otaUpdateAvailable && model.otaStatus == nil
        let installing = isOtaInstalling(model: model)
        return VStack(alignment: .leading, spacing: 9) {
            HStack(alignment: .top) {
                HStack(alignment: .center, spacing: 10) {
                    if updateRequired {
                        ZStack {
                            RoundedRectangle(cornerRadius: 7).fill(AppColor.amber.opacity(0.10))
                            Image(systemName: "exclamationmark.triangle")
                                .font(.system(size: 14, weight: .semibold))
                                .foregroundColor(AppColor.amber)
                        }
                        .frame(width: 30, height: 30)
                    }
                    VStack(alignment: .leading, spacing: 3) {
                        Text(updateRequired ? "OTA UPDATE" : "OTA")
                            .font(.system(size: 10, weight: .semibold).monospaced())
                            .foregroundColor(updateRequired ? Color(hex: 0x9A5A00) : AppColor.muted)
                        Text(otaCardTitle(model: model))
                            .font(.system(size: updateRequired ? 16 : 15, weight: .bold))
                            .foregroundColor(AppColor.ink)
                    }
                }
                Spacer()
                if model.otaStartPending || installing {
                    ProgressView()
                        .tint(AppColor.greenPrimary)
                        .controlSize(.small)
                } else if let status = model.otaStatus {
                    Text("\(otaDisplayPercent(model: model, status: status))%")
                        .font(.system(size: 15, weight: .bold).monospacedDigit())
                        .foregroundColor(AppColor.greenInk)
                }
            }
            if let status = model.otaStatus, !installing {
                GeometryReader { geometry in
                    ZStack(alignment: .leading) {
                        Capsule().fill(AppColor.greenInk.opacity(0.08))
                        Capsule()
                            .fill(AppColor.greenPrimary)
                            .frame(width: geometry.size.width * CGFloat(min(max(otaDisplayPercent(model: model, status: status), 0), 100)) / 100.0)
                    }
                }
                .frame(height: 4)
            }
            Text(otaCardDetail(model: model))
                .font(.system(size: 12, weight: .medium))
                .foregroundColor(AppColor.muted)
                .fixedSize(horizontal: false, vertical: true)
            if updateRequired {
                Button(action: model.startOtaUpdate) {
                    HStack(spacing: 8) {
                        Image(systemName: "arrow.down.to.line")
                            .font(.system(size: 15, weight: .bold))
                        Text(model.otaStartPending ? "Starting OTA..." : canStartOta(model: model) ? "Start OTA" : "Connect Wi-Fi first")
                            .font(.system(size: 13, weight: .bold))
                    }
                    .foregroundColor(.white)
                    .frame(maxWidth: .infinity)
                    .frame(minHeight: 42)
                    .background(canStartOta(model: model) ? AppColor.greenInk : AppColor.muted.opacity(0.55))
                    .clipShape(RoundedRectangle(cornerRadius: 8))
                }
                .buttonStyle(.plain)
                .disabled(!canStartOta(model: model))
            }
        }
        .padding(14)
        .background(updateRequired ? Color(hex: 0xFFFCF5) : .white)
        .overlay(RoundedRectangle(cornerRadius: 12).stroke(updateRequired ? AppColor.amber.opacity(0.32) : AppColor.ink.opacity(0.08), lineWidth: 1))
        .clipShape(RoundedRectangle(cornerRadius: 12))
    }
}

private func glassesAssetName(_ values: GlassesRuntimeState?) -> String {
    let device = connectedGlassesInfo(values)
    let model = [
        device?.deviceModel?.deviceType,
        device?.bluetoothName,
    ]
    .compactMap { $0 }
    .joined(separator: " ")
    .lowercased()

    if model.contains("even"), model.contains("g2") {
        return "even_realities_g2"
    }
    if model.contains("even") || model.contains("g1") {
        return "even_realities_g1"
    }
    if model.contains("display") {
        return "mentra_display"
    }
    if model.contains("vuzix") || model.contains("z100") {
        return "vuzix_z100"
    }
    if model.contains("unknown") {
        return "unknown_wearable"
    }
    return "mentra_live"
}

private func targetDeviceDetail(_ device: Device) -> String {
    if let rssi = device.rssi {
        return "\(deviceModelLabel(device.model)) 路 \(rssi) dBm"
    }
    return deviceModelLabel(device.model)
}

@MainActor
private func canStartOta(model: BluetoothViewModel) -> Bool {
    model.glassesConnected && model.glassesWifiConnected && model.otaUpdateAvailable && !isOtaInProgress(model: model)
}

@MainActor
private func isOtaInProgress(model: BluetoothViewModel) -> Bool {
    model.otaStartPending || model.otaStatus?.status == "in_progress" || model.otaStatus?.status == "step_complete"
}

@MainActor
private func otaStatusLine(model: BluetoothViewModel) -> String {
    if model.otaStartPending {
        return "starting update"
    }
    if isOtaInstalling(model: model) {
        return "installing update"
    }
    if let status = model.otaStatus {
        return "\(status.status.replacingOccurrences(of: "_", with: " ")) 路 \(otaDisplayPercent(model: model, status: status))%"
    }
    if model.otaUpdateAvailable {
        return "Firmware update available"
    }
    if let message = model.otaStatusMessage {
        return message
    }
    return model.glassesConnected ? "Check not run" : "Connect glasses"
}

@MainActor
private func otaDisplayPercent(model: BluetoothViewModel, status: OtaStatusEvent) -> Int {
    model.otaDisplayPercent ?? status.overallPercent
}

@MainActor
private func otaCardTitle(model: BluetoothViewModel) -> String {
    if model.otaStartPending {
        return "Starting update"
    }
    if model.otaStatus?.status == "failed" {
        return "Update failed"
    }
    if isOtaInstalling(model: model) {
        return "Installing update"
    }
    if let status = model.otaStatus, isOtaInProgress(model: model) {
        return "Updating \(status.stepType.isEmpty ? "firmware" : status.stepType)"
    }
    if model.otaUpdateAvailable {
        return "Firmware update available"
    }
    if model.otaNoUpdateVisible {
        return "No OTA available"
    }
    return "OTA status"
}

@MainActor
private func otaCardDetail(model: BluetoothViewModel) -> String {
    if model.otaStartPending {
        return "Sending the OTA start request to the glasses."
    }
    if let error = model.otaStatus?.errorMessage {
        return error
    }
    if isOtaInstalling(model: model) {
        return "The glasses client may restart to finish installation."
    }
    if let status = model.otaStatus {
        return "\(status.phase.isEmpty ? "status" : status.phase) 路 step \(status.currentStep)/\(status.totalSteps)"
    }
    if model.otaUpdateAvailable {
        return "A newer firmware version is available for these glasses."
    }
    if model.otaNoUpdateVisible {
        return "Your glasses firmware is up to date."
    }
    return "Tap Check OTA to compare the current glasses version with the SDK OTA manifest."
}

@MainActor
private func isOtaInstalling(model: BluetoothViewModel) -> Bool {
    model.otaStatus?.status == "in_progress" && model.otaStatus?.phase == "install"
}

@MainActor
private func connectionTargetLabel(model: BluetoothViewModel) -> String {
    if model.glassesConnected {
        return deviceLabel(model.glassesValues)
    }
    if let device = model.selectedDiscoveredDevice {
        return device.name
    }
    if hasSavedConnectionTarget(model.bluetoothValues) {
        return savedConnectionTargetName(model.bluetoothValues)
    }
    return "Scan required"
}

@MainActor
private func canConnectTarget(model: BluetoothViewModel) -> Bool {
    model.selectedDiscoveredDevice != nil || !model.discoveredDevices.isEmpty || hasSavedConnectionTarget(model.bluetoothValues)
}

struct ScanModelChip: View {
    let model: DeviceModel
    let active: Bool
    let enabled: Bool
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            Text(deviceModelLabel(model))
                .font(.system(size: 13, weight: .bold))
                .foregroundColor(active ? AppColor.greenInk : AppColor.muted)
                .frame(maxWidth: .infinity)
                .padding(.horizontal, 12)
                .padding(.vertical, 9)
                .frame(minHeight: 40)
                .background(active ? AppColor.greenPrimary.opacity(0.10) : Color.white.opacity(0.72))
                .overlay(
                    Capsule()
                        .stroke(active ? AppColor.greenPrimary.opacity(0.32) : AppColor.hairline, lineWidth: 1)
                )
                .clipShape(Capsule())
        }
        .buttonStyle(.plain)
        .disabled(!enabled)
        .opacity(enabled ? 1 : 0.45)
    }
}

struct TargetDeviceRow: View {
    let name: String
    let detail: String
    let selected: Bool
    let enabled: Bool
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            HStack(spacing: 10) {
                ZStack {
                    Circle()
                        .fill(selected ? AppColor.greenPrimary : Color.white)
                        .frame(width: 18, height: 18)
                    if selected {
                        Image(systemName: "checkmark")
                            .font(.system(size: 10, weight: .heavy))
                            .foregroundColor(.white)
                    }
                }
                VStack(alignment: .leading, spacing: 2) {
                    Text(name)
                        .font(.system(size: 13, weight: .bold))
                        .foregroundColor(AppColor.inkAlt)
                    Text(detail)
                        .font(.system(size: 11, weight: .medium))
                        .foregroundColor(AppColor.muted)
                }
                Spacer()
            }
            .padding(.horizontal, 10)
            .padding(.vertical, 8)
            .background(selected ? AppColor.greenPrimary.opacity(0.08) : Color.white.opacity(0.7))
            .overlay(RoundedRectangle(cornerRadius: 14).stroke(selected ? AppColor.greenPrimary.opacity(0.18) : Color.white.opacity(0.7), lineWidth: 1))
            .clipShape(RoundedRectangle(cornerRadius: 14))
        }
        .buttonStyle(.plain)
        .disabled(!enabled)
    }
}

struct DisplayCapabilityNotice: View {
    let text: String

    var body: some View {
        HStack(alignment: .top, spacing: 8) {
            Image(systemName: "info.circle.fill")
                .font(.system(size: 13, weight: .semibold))
                .foregroundColor(AppColor.greenPrimary)
                .padding(.top, 1)
            Text(text)
                .font(.system(size: 12, weight: .medium))
                .foregroundColor(AppColor.muted)
                .fixedSize(horizontal: false, vertical: true)
            Spacer(minLength: 0)
        }
        .padding(.horizontal, 12)
        .padding(.vertical, 10)
        .background(AppColor.greenSoft.opacity(0.18))
        .clipShape(RoundedRectangle(cornerRadius: 16))
    }
}

struct StatTile: View {
    let label: String
    let value: String
    let sub: String
    let subColor: Color
    var bold: Bool = false
    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            Text(label).font(.system(size: 10, weight: .semibold)).tracking(1.1).foregroundColor(AppColor.muted)
            Text(value).font(.system(size: 15, weight: bold ? .bold : .semibold)).foregroundColor(AppColor.ink)
            Text(sub).font(.system(size: 12, weight: .medium)).foregroundColor(subColor)
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding(.horizontal, 14)
        .padding(.vertical, 13)
        .background(Color.white)
        .overlay(RoundedRectangle(cornerRadius: 18).stroke(Color.white.opacity(0.7), lineWidth: 1))
        .clipShape(RoundedRectangle(cornerRadius: 18))
        .shadow(color: Color(hex: 0x0F2A1D).opacity(0.07), radius: 22, x: 0, y: 8)
    }
}

struct DarkActionButton: View {
    let icon: String; let title: String; let bg: Color
    var enabled: Bool = true
    let action: () -> Void
    var body: some View {
        Button {
            action()
        } label: {
            HStack(spacing: 8) {
                Image(systemName: icon).foregroundColor(.white).font(.system(size: 14, weight: .bold))
                Text(title).foregroundColor(.white).font(.system(size: 13, weight: .semibold))
            }
            .frame(maxWidth: .infinity).padding(.vertical, 14)
            .background(bg).clipShape(RoundedRectangle(cornerRadius: 14))
        }
        .disabled(!enabled)
        .opacity(enabled ? 1 : 0.45)
    }
}

struct LightActionButton: View {
    let icon: String; let title: String
    var enabled: Bool = true
    let action: () -> Void
    var body: some View {
        Button {
            action()
        } label: {
            HStack(spacing: 8) {
                Image(systemName: icon).foregroundColor(AppColor.inkAlt).font(.system(size: 14, weight: .bold))
                Text(title).foregroundColor(AppColor.inkAlt).font(.system(size: 13, weight: .semibold))
            }
            .frame(maxWidth: .infinity).padding(.vertical, 14)
            .background(Color.white)
            .overlay(RoundedRectangle(cornerRadius: 14).stroke(Color(hex: 0xDBDBDB), lineWidth: 1))
            .clipShape(RoundedRectangle(cornerRadius: 14))
        }
        .disabled(!enabled)
        .opacity(enabled ? 1 : 0.45)
    }
}

struct StatusKVRow: View {
    let label: String
    var value: String? = nil
    var custom: AnyView? = nil
    var mono: Bool = false
    var first: Bool = false

    var body: some View {
        HStack(alignment: .top, spacing: 14) {
            Text(label)
                .font(.system(size: 10, weight: .semibold).monospaced())
                .tracking(1.4)
                .foregroundColor(AppColor.inkAlt.opacity(0.5))
                .frame(width: 90, alignment: .leading)
            Group {
                if let custom = custom { custom }
                else if let value = value {
                    Text(value)
                        .font(mono ? .system(size: 12, design: .monospaced) : .system(size: 13, weight: .medium))
                        .foregroundColor(AppColor.inkAlt)
                }
            }
            .frame(maxWidth: .infinity, alignment: .leading)
        }
        .padding(.vertical, 11)
        .overlay(alignment: .top) {
            if !first { Rectangle().fill(Color(hex: 0xF2EDE0)).frame(height: 1) }
        }
    }
}