summaryrefslogtreecommitdiff
path: root/App.tsx
blob: a8a20833e83394cdfae3d61e3d6f0cfbd25490f9 (plain)
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
import React, {useState, useEffect, useRef, useCallback} from 'react';
import {
  StyleSheet,
  View,
  Text,
  TouchableOpacity,
  FlatList,
  ScrollView,
  Platform,
  PermissionsAndroid,
  DeviceEventEmitter,
  NativeEventEmitter,
  NativeModules,
  ActivityIndicator,
} from 'react-native';
import {SafeAreaProvider, SafeAreaView} from 'react-native-safe-area-context';
import AsyncStorage from '@react-native-async-storage/async-storage';

// ── Native module types ──────────────────────────────────────────────

const DEVICE_TYPE_KEYS = [
  'AM3S', 'AM4', 'PO3', 'BP5', 'BP5S', 'BP3L', 'BP7', 'BP7S',
  'KN550', 'HS2', 'HS2S', 'HS4S', 'BG1', 'BG1S', 'BG5', 'BG5S',
  'ECG3', 'BTM', 'TS28B', 'NT13B',
] as const;

type DeviceTypeName = (typeof DEVICE_TYPE_KEYS)[number];
type DiscoveryConstant = string | number;

interface IHealthDeviceManager {
  AM3S: DiscoveryConstant; AM4: DiscoveryConstant; PO3: DiscoveryConstant;
  BP5: DiscoveryConstant; BP5S: DiscoveryConstant; BP3L: DiscoveryConstant;
  BP7: DiscoveryConstant; BP7S: DiscoveryConstant; KN550: DiscoveryConstant;
  HS2: DiscoveryConstant; HS2S: DiscoveryConstant; HS4S: DiscoveryConstant;
  BG1: DiscoveryConstant; BG1S: DiscoveryConstant; BG5: DiscoveryConstant;
  BG5S: DiscoveryConstant; ECG3: DiscoveryConstant; BTM: DiscoveryConstant;
  TS28B: DiscoveryConstant; NT13B: DiscoveryConstant;
  Event_Scan_Device: string;
  Event_Scan_Finish: string;
  Event_Device_Connected: string;
  Event_Device_Connect_Failed: string;
  Event_Device_Disconnect: string;
  Event_Authenticate_Result: string;
  startDiscovery(type: DeviceTypeName | string): void;
  stopDiscovery(): void;
  connectDevice(mac: string, type: string): void;
  disconnectDevice(mac: string, type: string): void;
  sdkAuthWithLicense(license: string): void;
  authenConfigureInfo(userName: string, clientID: string, clientSecret: string): void;
  getDevicesIDPS(mac: string, callback: (idps: Record<string, string>) => void): void;
}

interface IBP550BTModule {
  Event_Notify: string;
  getBattery(mac: string): void;
  getOffLineNum(mac: string): void;
  getOffLineData(mac: string): void;
  getFunctionInfo(mac: string): void;
  disconnect(mac: string): void;
  getAllConnectedDevices(): void;
}

interface IPO3Module {
  Event_Notify: string;
  getBattery(mac: string): void;
  startMeasure(mac: string): void;
  getHistoryData(mac: string): void;
  disconnect(mac: string): void;
}

interface IPT3SBTModule {
  Event_Notify: string;
  getBattery(mac: string): void;
  getHistoryData(mac: string): void;
  getHistoryCount(mac: string): void;
  setUnit(mac: string, unit: number): void;
  disconnect(mac: string): void;
}

interface IHS2SModule {
  Event_Notify: string;
  getBattery(mac: string): void;
  getMemoryDataCount(mac: string, id: number): void;
  getMemoryData(mac: string, id: number): void;
  getAnonymousMemoryData(mac: string): void;
  disconnect(mac: string): void;
}

interface IBG5SModule {
  Event_Notify: string;
  getStatusInfo(mac: string): void;
  getOfflineData(mac: string): void;
  startMeasure(mac: string, type: number): void;
  disConnect(mac: string): void;
}

const mgr = NativeModules.iHealthDeviceManagerModule as IHealthDeviceManager;
const bp550 = NativeModules.BP550BTModule as IBP550BTModule;
const po3 = NativeModules.PO3Module as IPO3Module;
const pt3sbt = NativeModules.PT3SBTModule as IPT3SBTModule;
const hs2s = NativeModules.HS2SModule as IHS2SModule;
const bg5s = NativeModules.BG5SModule as IBG5SModule;

// Device type → module mapping
const DEVICE_MODULES: Record<string, {module: {Event_Notify: string; disconnect: (mac: string) => void} | null; label: string}> = {
  KN550: {module: bp550, label: 'Blood Pressure'},
  'KN-550BT': {module: bp550, label: 'Blood Pressure'},
  PO3: {module: po3, label: 'Pulse Oximeter'},
  PT3SBT: {module: pt3sbt, label: 'Thermometer'},
  HS2S: {module: hs2s, label: 'Scale'},
  BG5S: {module: bg5s, label: 'Glucose Monitor'},
};

// ── Types ────────────────────────────────────────────────────────────

type Device = {mac: string; type: string; rssi?: number; timestamp: number};

type SavedDevice = {mac: string; type: string; addedAt: string};

type Reading = {
  mac: string;
  deviceType?: string;
  // BP
  sys?: number;
  dia?: number;
  pulse?: number;
  // SpO2
  spo2?: number;
  pulseRate?: number;
  // Temperature
  temperature?: number;
  tempUnit?: string;
  // Weight/Scale
  weight?: number;
  bodyFat?: number;
  bmi?: number;
  // Glucose
  glucose?: number;
  // Common
  battery?: number;
  date?: string;
  fetchedAt: string;
};

type Screen = 'home' | 'dashboard' | 'debug' | 'debug-device';

const STORAGE_KEY_DEVICES = '@ihealth/saved_devices';
const STORAGE_KEY_READINGS = '@ihealth/readings';

// ── Helpers ──────────────────────────────────────────────────────────

async function requestAndroidPermissions(): Promise<boolean> {
  if (Platform.OS !== 'android') return true;
  const perms: string[] = [];
  if (Platform.Version >= 31) {
    perms.push(
      PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
      PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
    );
  }
  perms.push(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
  const r = await PermissionsAndroid.requestMultiple(perms as any);
  return Object.values(r).every(v => v === PermissionsAndroid.RESULTS.GRANTED);
}

function getEmitter() {
  return Platform.OS === 'ios'
    ? new NativeEventEmitter(NativeModules.iHealthDeviceManagerModule)
    : DeviceEventEmitter;
}

function getBPEmitter() {
  return Platform.OS === 'ios'
    ? new NativeEventEmitter(NativeModules.BP550BTModule)
    : DeviceEventEmitter;
}

// ── Home Screen ──────────────────────────────────────────────────────

function HomeScreen({onNav}: {onNav: (s: Screen) => void}) {
  return (
    <SafeAreaView style={s.container}>
      <View style={s.homeCenter}>
        <Text style={s.homeTitle}>iHealth</Text>
        <Text style={s.homeSubtitle}>Blood Pressure Monitor</Text>

        <TouchableOpacity
          style={s.homeButton}
          onPress={() => onNav('dashboard')}>
          <Text style={s.homeButtonText}>Dashboard</Text>
          <Text style={s.homeButtonSub}>View your devices and readings</Text>
        </TouchableOpacity>

        <TouchableOpacity
          style={[s.homeButton, s.homeButtonSecondary]}
          onPress={() => onNav('debug')}>
          <Text style={[s.homeButtonText, s.homeButtonTextSecondary]}>
            Debug Scanner
          </Text>
          <Text style={[s.homeButtonSub, s.homeButtonTextSecondary]}>
            Scan all devices, raw logs
          </Text>
        </TouchableOpacity>
      </View>
    </SafeAreaView>
  );
}

// ── Dashboard Screen ─────────────────────────────────────────────────

function DashboardScreen({onBack}: {onBack: () => void}) {
  const [savedDevices, setSavedDevices] = useState<SavedDevice[]>([]);
  const [readings, setReadings] = useState<Reading[]>([]);
  const [foundDevices, setFoundDevices] = useState<Device[]>([]);
  const [status, setStatus] = useState('Starting...');
  const foundRef = useRef<Device[]>([]);
  const savedRef = useRef<SavedDevice[]>([]);
  const readingsRef = useRef<Reading[]>([]);
  const syncingMac = useRef<string | null>(null);

  // Load saved data on mount
  useEffect(() => {
    (async () => {
      const [devJson, readJson] = await Promise.all([
        AsyncStorage.getItem(STORAGE_KEY_DEVICES),
        AsyncStorage.getItem(STORAGE_KEY_READINGS),
      ]);
      const devs: SavedDevice[] = devJson ? JSON.parse(devJson) : [];
      const reads: Reading[] = readJson ? JSON.parse(readJson) : [];
      setSavedDevices(devs);
      savedRef.current = devs;
      setReadings(reads);
      readingsRef.current = reads;
      await requestAndroidPermissions();
    })();
  }, []);

  // Always-on scan loop: restart scan every time it finishes
  useEffect(() => {
    const emitter = getEmitter();
    const bpEmitter = getBPEmitter();

    const startScan = () => {
      try { mgr.startDiscovery('ALL'); }
      catch (_) {}
      setStatus('Scanning...');
    };

    // When a device is found
    const scanSub = emitter.addListener(
      mgr.Event_Scan_Device ?? 'event_scan_device',
      (e: {mac: string; type: string; rssi?: number}) => {
        if (e.type === 'AM3') return;

        // Track all found devices
        const exists = foundRef.current.find(d => d.mac === e.mac);
        if (!exists) {
          const updated = [...foundRef.current, {mac: e.mac, type: e.type, rssi: e.rssi, timestamp: Date.now()}];
          foundRef.current = updated;
          setFoundDevices(updated);
        }

        // Auto-connect if it's a saved device we haven't synced yet
        const isSaved = savedRef.current.some(d => d.mac === e.mac);
        if (isSaved && !syncingMac.current) {
          syncingMac.current = e.mac;
          setStatus(`Found ${e.mac.slice(-4)}, connecting...`);
          try { mgr.stopDiscovery(); } catch (_) {}
          mgr.connectDevice(e.mac, e.type);
        }
      },
    );

    // When scan finishes, restart after a short delay
    const finSub = emitter.addListener(
      mgr.Event_Scan_Finish ?? 'event_scan_finish',
      () => {
        if (!syncingMac.current) {
          setTimeout(startScan, 3000);
        }
      },
    );

    // Helper: listen for a native event, resolve on match, timeout
    const awaitEvent = <T,>(
      eventEmitter: typeof DeviceEventEmitter,
      eventName: string,
      match: (ev: Record<string, unknown>) => T | undefined,
      timeoutMs = 5000,
    ): Promise<T | undefined> =>
      new Promise(resolve => {
        const sub = eventEmitter.addListener(eventName, (ev: Record<string, unknown>) => {
          const result = match(ev);
          if (result !== undefined) { sub.remove(); resolve(result); }
        });
        setTimeout(() => { sub.remove(); resolve(undefined); }, timeoutMs);
      });

    // Helper: collect events into array until done signal
    const collectEvents = (
      eventEmitter: typeof DeviceEventEmitter,
      eventName: string,
      collect: (ev: Record<string, unknown>, results: Record<string, unknown>[]) => boolean, // return true when done
      timeoutMs = 10000,
    ): Promise<Record<string, unknown>[]> =>
      new Promise(resolve => {
        const results: Record<string, unknown>[] = [];
        const sub = eventEmitter.addListener(eventName, (ev: Record<string, unknown>) => {
          if (collect(ev, results)) { sub.remove(); resolve(results); }
        });
        setTimeout(() => { sub.remove(); resolve(results); }, timeoutMs);
      });

    // ── Per-device sync logic ──
    const syncBP550 = async (mac: string): Promise<Reading[]> => {
      const notify = bp550?.Event_Notify ?? 'event_notify';
      const bpEm = getBPEmitter();
      // Start listening BEFORE calling native method
      const batteryPromise = awaitEvent<number>(bpEm, notify,
        ev => ev.action === 'battery_bp' && ev.battery != null ? ev.battery as number : undefined);
      bp550.getBattery(mac);
      const battery = await batteryPromise;

      const dataPromise = collectEvents(bpEm, notify, (ev, res) => {
        if (ev.action === 'historicaldata_bp' && ev.data)
          res.push(...(ev.data as Record<string, unknown>[]));
        return ev.action === 'get_historical_over_bp' || (ev.action === 'offlinenum' && ev.offlinenum === 0);
      });
      bp550.getOffLineData(mac);
      const data = await dataPromise;

      const readings: Reading[] = data.map(d => ({
        mac, deviceType: 'BP', sys: d.sys as number, dia: d.dia as number,
        pulse: d.heartRate as number, date: d.date as string,
        battery, fetchedAt: new Date().toISOString(),
      }));
      if (readings.length === 0 && battery != null)
        readings.push({mac, deviceType: 'BP', battery, fetchedAt: new Date().toISOString()});
      try { bp550.disconnect(mac); } catch (_) {}
      return readings;
    };

    const syncPO3 = async (mac: string): Promise<Reading[]> => {
      const notify = po3?.Event_Notify ?? 'event_notify';
      const em = Platform.OS === 'ios' ? new NativeEventEmitter(NativeModules.PO3Module) : DeviceEventEmitter;
      const bp = awaitEvent<number>(em, notify,
        ev => ev.action === 'battery_po' && ev.battery != null ? ev.battery as number : undefined);
      po3.getBattery(mac);
      const battery = await bp;

      const dp = collectEvents(em, notify, (ev, res) => {
        if (ev.action === 'offlineData_po' && ev.offlinedata) {
          const arr = Array.isArray(ev.offlinedata) ? ev.offlinedata : [ev.offlinedata];
          res.push(...(arr as Record<string, unknown>[]));
        }
        return ev.action === 'offlineData_po' || ev.action === 'noOfflineData_po';
      });
      po3.getHistoryData(mac);
      const data = await dp;

      const readings: Reading[] = data.map(d => ({
        mac, deviceType: 'SpO2', spo2: d.bloodoxygen as number,
        pulseRate: d.heartrate as number, date: d.measuredate as string,
        battery, fetchedAt: new Date().toISOString(),
      }));
      if (readings.length === 0 && battery != null)
        readings.push({mac, deviceType: 'SpO2', battery, fetchedAt: new Date().toISOString()});
      try { po3.disconnect(mac); } catch (_) {}
      return readings;
    };

    const syncPT3SBT = async (mac: string): Promise<Reading[]> => {
      const notify = pt3sbt?.Event_Notify ?? 'event_notify';
      const em = Platform.OS === 'ios' ? new NativeEventEmitter(NativeModules.PT3SBTModule) : DeviceEventEmitter;
      const bp = awaitEvent<number>(em, notify,
        ev => ev.action === 'action_get_battery' && ev.battery != null ? ev.battery as number : undefined);
      pt3sbt.getBattery(mac);
      const battery = await bp;

      const dp = collectEvents(em, notify, (ev, res) => {
        if (ev.action === 'action_get_history_data' && ev.history) {
          const arr = Array.isArray(ev.history) ? ev.history : [ev.history];
          res.push(...(arr as Record<string, unknown>[]));
          return true;
        }
        return false;
      });
      pt3sbt.getHistoryData(mac);
      const data = await dp;

      const readings: Reading[] = data.map(d => ({
        mac, deviceType: 'Temp', temperature: d.Tbody as number,
        date: d.ts as string, battery, fetchedAt: new Date().toISOString(),
      }));
      if (readings.length === 0 && battery != null)
        readings.push({mac, deviceType: 'Temp', battery, fetchedAt: new Date().toISOString()});
      try { pt3sbt.disconnect(mac); } catch (_) {}
      return readings;
    };

    const syncHS2S = async (mac: string): Promise<Reading[]> => {
      const notify = hs2s?.Event_Notify ?? 'event_notify';
      const em = Platform.OS === 'ios' ? new NativeEventEmitter(NativeModules.HS2SModule) : DeviceEventEmitter;
      const bp = awaitEvent<number>(em, notify,
        ev => ev.action === 'battery_hs' && ev.battery != null ? ev.battery as number : undefined);
      hs2s.getBattery(mac);
      const battery = await bp;

      const dp = collectEvents(em, notify, (ev, res) => {
        if (ev.action === 'action_history_data' && ev.weight != null) {
          res.push(ev);
        }
        return ev.action === 'action_anonymous_data_num' || ev.action === 'action_anonymous_data';
      });
      hs2s.getAnonymousMemoryData(mac);
      const data = await dp;

      const readings: Reading[] = data.map(d => ({
        mac, deviceType: 'Scale', weight: d.weight as number,
        bodyFat: d.body_fit_percentage as number,
        bmi: d.body_mass_index as number,
        date: d.data_measure_time as string,
        battery, fetchedAt: new Date().toISOString(),
      }));
      if (readings.length === 0 && battery != null)
        readings.push({mac, deviceType: 'Scale', battery, fetchedAt: new Date().toISOString()});
      try { hs2s.disconnect(mac); } catch (_) {}
      return readings;
    };

    const syncBG5S = async (mac: string): Promise<Reading[]> => {
      const notify = bg5s?.Event_Notify ?? 'event_notify';
      const em = Platform.OS === 'ios' ? new NativeEventEmitter(NativeModules.BG5SModule) : DeviceEventEmitter;

      const dp = collectEvents(em, notify, (ev, res) => {
        if (ev.action === 'action_get_offline_data' && ev.offline_data) {
          const arr = Array.isArray(ev.offline_data) ? ev.offline_data : [ev.offline_data];
          res.push(...(arr as Record<string, unknown>[]));
          return true;
        }
        if (ev.action === 'action_get_status_info' && ev.info_offline_data_num === 0) return true;
        return false;
      });
      bg5s.getOfflineData(mac);
      const data = await dp;

      const readings: Reading[] = data.map(d => ({
        mac, deviceType: 'Glucose', glucose: d.data_value as number,
        date: d.data_measure_time as string,
        fetchedAt: new Date().toISOString(),
      }));
      try { bg5s.disConnect(mac); } catch (_) {}
      return readings;
    };

    // When connected, pull data based on device type
    const connSub = emitter.addListener(
      mgr.Event_Device_Connected ?? 'event_device_connected',
      async (e: {mac: string; type: string}) => {
        if (e.mac !== syncingMac.current) return;
        setStatus(`Connected to ${e.mac.slice(-4)}, reading...`);

        let newReadings: Reading[] = [];
        try {
          switch (e.type) {
            case 'KN550': case 'KN-550BT': newReadings = await syncBP550(e.mac); break;
            case 'PO3': newReadings = await syncPO3(e.mac); break;
            case 'PT3SBT': newReadings = await syncPT3SBT(e.mac); break;
            case 'HS2S': newReadings = await syncHS2S(e.mac); break;
            case 'BG5S': newReadings = await syncBG5S(e.mac); break;
            default:
              // Unknown device — just disconnect
              try { mgr.disconnectDevice(e.mac, e.type); } catch (_) {}
          }
        } catch (err) {
          console.log('Sync error:', err);
        }

        if (newReadings.length > 0) {
          const all = [...readingsRef.current, ...newReadings];
          readingsRef.current = all;
          setReadings(all);
          await AsyncStorage.setItem(STORAGE_KEY_READINGS, JSON.stringify(all));
        }

        setStatus(`Synced ${e.mac.slice(-4)} (${newReadings.length} readings)`);
        syncingMac.current = null;
        setTimeout(startScan, 3000);
      },
    );

    const failSub = emitter.addListener(
      mgr.Event_Device_Connect_Failed ?? 'event_device_connect_failed',
      () => {
        setStatus('Connect failed, resuming scan...');
        syncingMac.current = null;
        setTimeout(startScan, 3000);
      },
    );

    const dcSub = emitter.addListener(
      mgr.Event_Device_Disconnect ?? 'event_device_disconnect',
      () => {
        if (syncingMac.current) {
          syncingMac.current = null;
          setTimeout(startScan, 3000);
        }
      },
    );

    // Start first scan
    startScan();

    return () => {
      scanSub.remove(); finSub.remove(); connSub.remove();
      failSub.remove(); dcSub.remove();
      try { mgr.stopDiscovery(); } catch (_) {}
    };
  }, []);

  const addDevice = async (dev: Device) => {
    const newDev: SavedDevice = {mac: dev.mac, type: dev.type, addedAt: new Date().toISOString()};
    const updated = [...savedRef.current, newDev];
    savedRef.current = updated;
    setSavedDevices(updated);
    await AsyncStorage.setItem(STORAGE_KEY_DEVICES, JSON.stringify(updated));
    const f = foundRef.current.filter(d => d.mac !== dev.mac);
    foundRef.current = f;
    setFoundDevices(f);
  };

  const removeDevice = async (mac: string) => {
    const updated = savedRef.current.filter(d => d.mac !== mac);
    savedRef.current = updated;
    setSavedDevices(updated);
    await AsyncStorage.setItem(STORAGE_KEY_DEVICES, JSON.stringify(updated));
  };

  const isDeviceSaved = (mac: string) => savedRef.current.some(d => d.mac === mac);

  const recentReadings = readings
    .filter(r => r.sys != null || r.spo2 != null || r.temperature != null || r.weight != null || r.glucose != null)
    .slice(-30)
    .reverse();

  return (
    <SafeAreaView style={s.container}>
      <TouchableOpacity style={s.backButton} onPress={onBack}>
        <Text style={s.backText}>Home</Text>
      </TouchableOpacity>

      <Text style={s.title}>Dashboard 🦄✨</Text>

      {/* Show status only when actively syncing a device */}
      {status.includes('connecting') || status.includes('reading') || status.includes('Synced') ? (
        <View style={s.syncBar}>
          <ActivityIndicator size="small" color="#2196F3" />
          <Text style={s.syncText}>{status}</Text>
        </View>
      ) : null}

      {/* All devices — unified list, saved ones marked */}
      <Text style={s.sectionTitle}>
        Devices ({foundDevices.length} nearby, {savedDevices.length} saved)
      </Text>

      {/* Merge: all found devices + saved devices not currently found */}
      {(() => {
        const allMacs = new Set([
          ...foundDevices.map(d => d.mac),
          ...savedDevices.map(d => d.mac),
        ]);
        const merged = Array.from(allMacs).map(mac => {
          const found = foundDevices.find(d => d.mac === mac);
          const saved = savedDevices.find(d => d.mac === mac);
          return {
            mac,
            type: found?.type ?? saved?.type ?? 'KN550',
            rssi: found?.rssi,
            nearby: !!found,
            saved: !!saved,
          };
        });
        // Sort: saved first, then nearby, then rest
        merged.sort((a, b) => {
          if (a.saved !== b.saved) return a.saved ? -1 : 1;
          if (a.nearby !== b.nearby) return a.nearby ? -1 : 1;
          return 0;
        });
        return merged.map(d => (
          <View key={d.mac} style={s.savedRow}>
            <View style={[s.deviceIcon, d.saved && s.deviceIconSaved, !d.nearby && s.deviceOffline]}>
              <Text style={s.deviceIconText}>{d.type.substring(0, 3)}</Text>
            </View>
            <View style={[s.deviceInfo, !d.nearby && s.deviceOffline]}>
              <Text style={s.deviceType}>
                {d.type}
                {d.saved ? ' ' : ''}
                {d.saved && <Text style={s.savedBadge}>SAVED</Text>}
              </Text>
              <Text style={s.deviceMac}>
                {d.mac}
                {!d.nearby ? '  (offline)' : d.rssi != null ? `  ${d.rssi} dBm` : ''}
              </Text>
            </View>
            {d.saved ? (
              <TouchableOpacity onPress={() => removeDevice(d.mac)}>
                <Text style={s.removeText}>Remove</Text>
              </TouchableOpacity>
            ) : (
              <TouchableOpacity style={s.addButton} onPress={() => addDevice({mac: d.mac, type: d.type, timestamp: Date.now()})}>
                <Text style={s.addButtonText}>Save</Text>
              </TouchableOpacity>
            )}
          </View>
        ));
      })()}

      {/* Readings */}
      <Text style={[s.sectionTitle, {marginTop: 16}]}>
        Readings ({recentReadings.length})
      </Text>
      <FlatList
        data={recentReadings}
        keyExtractor={(_, i) => String(i)}
        style={s.list}
        renderItem={({item}) => (
          <View style={s.readingRow}>
            {item.sys != null && (
              <View style={s.readingValues}>
                <Text style={s.readingSys}>{item.sys}</Text>
                <Text style={s.readingSlash}>/</Text>
                <Text style={s.readingDia}>{item.dia ?? '--'}</Text>
                <Text style={s.readingUnit}>mmHg</Text>
                <Text style={s.readingPulse}>{item.pulse ?? '--'} bpm</Text>
              </View>
            )}
            {item.spo2 != null && (
              <View style={s.readingValues}>
                <Text style={s.readingSys}>{item.spo2}%</Text>
                <Text style={s.readingUnit}>SpO2</Text>
                <Text style={s.readingPulse}>{item.pulseRate ?? '--'} bpm</Text>
              </View>
            )}
            {item.temperature != null && (
              <View style={s.readingValues}>
                <Text style={s.readingSys}>{item.temperature}</Text>
                <Text style={s.readingUnit}>{item.tempUnit ?? 'C'}</Text>
              </View>
            )}
            {item.weight != null && (
              <View style={s.readingValues}>
                <Text style={s.readingSys}>{item.weight}</Text>
                <Text style={s.readingUnit}>kg</Text>
                {item.bodyFat != null && <Text style={s.readingPulse}>{item.bodyFat}% fat</Text>}
                {item.bmi != null && <Text style={s.readingPulse}>BMI {item.bmi}</Text>}
              </View>
            )}
            {item.glucose != null && (
              <View style={s.readingValues}>
                <Text style={s.readingSys}>{item.glucose}</Text>
                <Text style={s.readingUnit}>mg/dL</Text>
              </View>
            )}
            <View style={s.readingMeta}>
              <Text style={s.readingDate}>
                {item.deviceType ? `${item.deviceType}  ` : ''}
                {item.date ?? item.fetchedAt.split('T')[0]}
              </Text>
              <Text style={s.readingMac}>{item.mac}</Text>
            </View>
          </View>
        )}
        ListEmptyComponent={
          <Text style={s.emptyText}>
            {savedDevices.length === 0
              ? 'Add a device to start tracking'
              : 'No readings yet. Sync to pull data.'}
          </Text>
        }
      />
    </SafeAreaView>
  );
}

// ── Debug Scanner Screen ─────────────────────────────────────────────

function DebugScannerScreen({
  onBack,
  onSelectDevice,
}: {
  onBack: () => void;
  onSelectDevice: (d: Device) => void;
}) {
  const [devices, setDevices] = useState<Device[]>([]);
  const [scanning, setScanning] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const devicesRef = useRef<Device[]>([]);

  useEffect(() => {
    if (!mgr) { setError('iHealth native module not found.'); return; }
    const emitter = getEmitter();
    const sub = emitter.addListener(
      mgr.Event_Scan_Device ?? 'event_scan_device',
      (e: {mac: string; type: string; rssi?: number}) => {
        if (e.type === 'AM3') return;
        const {mac = '', type = 'Unknown', rssi} = e;
        const idx = devicesRef.current.findIndex(d => d.mac === mac);
        let updated: Device[];
        if (idx >= 0) {
          updated = [...devicesRef.current];
          updated[idx] = {mac, type, rssi, timestamp: Date.now()};
        } else {
          updated = [...devicesRef.current, {mac, type, rssi, timestamp: Date.now()}];
        }
        devicesRef.current = updated;
        setDevices(updated);
      },
    );
    const finSub = emitter.addListener(
      mgr.Event_Scan_Finish ?? 'event_scan_finish',
      () => setScanning(false),
    );
    return () => { sub.remove(); finSub.remove(); };
  }, []);

  const startScan = async () => {
    setError(null);
    const ok = await requestAndroidPermissions();
    if (!ok) { setError('Permissions denied'); return; }
    devicesRef.current = [];
    setDevices([]);
    setScanning(true);
    if (Platform.OS === 'android') {
      try { mgr.startDiscovery('ALL'); } catch (e) { console.log(e); }
    } else {
      const types = ['KN550', 'BP3L', 'BP5S', 'BP7S', 'AM3S', 'AM4',
        'PO3', 'HS2', 'HS2S', 'HS4S', 'BG5S', 'BG1S', 'PO1', 'ECG3'];
      let i = 0;
      const next = () => {
        if (i < types.length) {
          try { mgr.startDiscovery(types[i]); } catch (_) {}
          i++;
          setTimeout(next, 2000);
        }
      };
      setTimeout(next, 2000);
    }
  };

  const stopScan = () => {
    try { mgr.stopDiscovery(); } catch (_) {}
    setScanning(false);
  };

  return (
    <SafeAreaView style={s.container}>
      <TouchableOpacity style={s.backButton} onPress={onBack}>
        <Text style={s.backText}>Home</Text>
      </TouchableOpacity>
      <Text style={s.title}>Debug Scanner</Text>
      <Text style={s.subtitle}>
        {scanning ? `Scanning... (${devices.length})` : `${devices.length} device(s)`}
      </Text>
      {error && <Text style={s.error}>{error}</Text>}
      <TouchableOpacity
        style={[s.button, scanning && s.buttonStop]}
        onPress={scanning ? stopScan : startScan}>
        <Text style={s.buttonText}>{scanning ? 'Stop' : 'Start Scan'}</Text>
      </TouchableOpacity>
      <FlatList
        data={devices}
        keyExtractor={item => item.mac}
        renderItem={({item}) => (
          <TouchableOpacity style={s.deviceRow} onPress={() => { stopScan(); onSelectDevice(item); }}>
            <View style={s.deviceIcon}>
              <Text style={s.deviceIconText}>{item.type.substring(0, 3)}</Text>
            </View>
            <View style={s.deviceInfo}>
              <Text style={s.deviceType}>{item.type}</Text>
              <Text style={s.deviceMac}>{item.mac}</Text>
            </View>
            {item.rssi != null && <Text style={s.deviceRssi}>{item.rssi} dBm</Text>}
            <Text style={s.chevron}>{'>'}</Text>
          </TouchableOpacity>
        )}
        style={s.list}
        contentContainerStyle={devices.length === 0 && s.emptyList}
        ListEmptyComponent={
          <Text style={s.emptyText}>
            {scanning ? 'Scanning...' : 'Tap Start Scan'}
          </Text>
        }
      />
    </SafeAreaView>
  );
}

// ── Debug Device Screen ──────────────────────────────────────────────

function DebugDeviceScreen({device, onBack}: {device: Device; onBack: () => void}) {
  const [connected, setConnected] = useState(false);
  const [connecting, setConnecting] = useState(false);
  const [log, setLog] = useState<string[]>([]);

  const addLog = (msg: string) =>
    setLog(prev => [`[${new Date().toLocaleTimeString()}] ${msg}`, ...prev]);

  useEffect(() => {
    const emitter = getEmitter();
    const bpEmitter = getBPEmitter();
    const connSub = emitter.addListener(
      mgr.Event_Device_Connected ?? 'event_device_connected',
      (e: {mac: string}) => {
        if (e.mac === device.mac) { setConnected(true); setConnecting(false); addLog('Connected'); }
      },
    );
    const failSub = emitter.addListener(
      mgr.Event_Device_Connect_Failed ?? 'event_device_connect_failed',
      (e: {mac: string; errorid?: number}) => {
        if (e.mac === device.mac) { setConnecting(false); addLog(`Connect failed: ${e.errorid}`); }
      },
    );
    const dcSub = emitter.addListener(
      mgr.Event_Device_Disconnect ?? 'event_device_disconnect',
      (e: {mac: string}) => {
        if (e.mac === device.mac) { setConnected(false); addLog('Disconnected'); }
      },
    );
    const notSub = bpEmitter.addListener(
      bp550?.Event_Notify ?? 'event_notify',
      (e: Record<string, unknown>) => addLog(JSON.stringify(e, null, 2)),
    );
    return () => { connSub.remove(); failSub.remove(); dcSub.remove(); notSub.remove(); };
  }, [device.mac]);

  const actions = [
    {label: 'Get Battery', fn: () => { addLog('Battery...'); bp550.getBattery(device.mac); }},
    {label: 'Function Info', fn: () => { addLog('FuncInfo...'); bp550.getFunctionInfo(device.mac); }},
    {label: 'Offline Count', fn: () => { addLog('OfflineNum...'); bp550.getOffLineNum(device.mac); }},
    {label: 'Offline Data', fn: () => { addLog('OfflineData...'); bp550.getOffLineData(device.mac); }},
    {label: 'IDPS', fn: () => {
      addLog('IDPS...');
      mgr.getDevicesIDPS(device.mac, idps => addLog(JSON.stringify(idps, null, 2)));
    }},
  ];

  return (
    <SafeAreaView style={s.container}>
      <TouchableOpacity style={s.backButton} onPress={onBack}>
        <Text style={s.backText}>Back</Text>
      </TouchableOpacity>
      <Text style={s.title}>{device.type}</Text>
      <Text style={s.deviceMac}>{device.mac}</Text>
      <Text style={[s.statusBadge, connected && s.statusConnected]}>
        {connecting ? 'Connecting...' : connected ? 'Connected' : 'Disconnected'}
      </Text>
      {!connected && !connecting && (
        <TouchableOpacity style={s.button} onPress={() => {
          setConnecting(true); addLog('Connecting...');
          mgr.connectDevice(device.mac, device.type);
        }}>
          <Text style={s.buttonText}>Connect</Text>
        </TouchableOpacity>
      )}
      {connected && (
        <TouchableOpacity style={[s.button, s.buttonStop]} onPress={() => {
          addLog('Disconnecting...'); bp550.disconnect(device.mac);
        }}>
          <Text style={s.buttonText}>Disconnect</Text>
        </TouchableOpacity>
      )}
      <ScrollView horizontal showsHorizontalScrollIndicator={false} style={s.actionsRow}>
        {actions.map(a => (
          <TouchableOpacity
            key={a.label}
            style={[s.actionButton, !connected && s.actionDisabled]}
            disabled={!connected}
            onPress={a.fn}>
            <Text style={[s.actionText, !connected && s.actionTextDisabled]}>{a.label}</Text>
          </TouchableOpacity>
        ))}
      </ScrollView>
      <Text style={s.sectionTitle}>Log</Text>
      <FlatList
        data={log}
        keyExtractor={(_, i) => String(i)}
        renderItem={({item}) => <Text style={s.logLine}>{item}</Text>}
        style={s.logList}
      />
    </SafeAreaView>
  );
}

// ── App Root ─────────────────────────────────────────────────────────

function App() {
  const [screen, setScreen] = useState<Screen>('home');
  const [debugDevice, setDebugDevice] = useState<Device | null>(null);

  return (
    <SafeAreaProvider>
      {screen === 'home' && <HomeScreen onNav={setScreen} />}
      {screen === 'dashboard' && (
        <DashboardScreen onBack={() => setScreen('home')} />
      )}
      {screen === 'debug' && (
        <DebugScannerScreen
          onBack={() => setScreen('home')}
          onSelectDevice={d => { setDebugDevice(d); setScreen('debug-device'); }}
        />
      )}
      {screen === 'debug-device' && debugDevice && (
        <DebugDeviceScreen
          device={debugDevice}
          onBack={() => setScreen('debug')}
        />
      )}
    </SafeAreaProvider>
  );
}

// ── Styles ───────────────────────────────────────────────────────────

const s = StyleSheet.create({
  container: {flex: 1, backgroundColor: '#f5f5f5', paddingHorizontal: 16},
  // Home
  homeCenter: {flex: 1, justifyContent: 'center'},
  homeTitle: {fontSize: 36, fontWeight: '800', color: '#1a1a1a', textAlign: 'center'},
  homeSubtitle: {fontSize: 16, color: '#666', textAlign: 'center', marginBottom: 40},
  homeButton: {
    backgroundColor: '#2196F3', padding: 20, borderRadius: 14,
    marginBottom: 12, alignItems: 'center',
  },
  homeButtonSecondary: {backgroundColor: '#fff', borderWidth: 1, borderColor: '#ddd'},
  homeButtonText: {color: '#fff', fontSize: 18, fontWeight: '700'},
  homeButtonTextSecondary: {color: '#333'},
  homeButtonSub: {color: 'rgba(255,255,255,0.8)', fontSize: 13, marginTop: 4},
  // Common
  title: {fontSize: 28, fontWeight: '700', color: '#1a1a1a', marginTop: 16},
  subtitle: {fontSize: 14, color: '#666', marginTop: 4, marginBottom: 16},
  sectionTitle: {fontSize: 16, fontWeight: '600', color: '#333', marginTop: 8, marginBottom: 8},
  error: {color: '#d32f2f', fontSize: 13, marginBottom: 8, backgroundColor: '#ffebee', padding: 8, borderRadius: 6},
  button: {backgroundColor: '#2196F3', paddingVertical: 14, borderRadius: 10, alignItems: 'center', marginBottom: 12},
  buttonStop: {backgroundColor: '#f44336'},
  buttonText: {color: '#fff', fontSize: 16, fontWeight: '600'},
  buttonOutline: {backgroundColor: 'transparent', borderWidth: 1.5, borderColor: '#2196F3'},
  buttonTextOutline: {color: '#2196F3'},
  list: {flex: 1},
  emptyList: {flex: 1, justifyContent: 'center', alignItems: 'center'},
  emptyText: {color: '#999', fontSize: 15, textAlign: 'center'},
  backButton: {marginTop: 12, marginBottom: 4},
  backText: {color: '#2196F3', fontSize: 16},
  chevron: {fontSize: 18, color: '#ccc', marginLeft: 8},
  // Device rows
  deviceRow: {
    flexDirection: 'row', alignItems: 'center', backgroundColor: '#fff',
    padding: 14, borderRadius: 10, marginBottom: 8,
    shadowColor: '#000', shadowOffset: {width: 0, height: 1}, shadowOpacity: 0.05, shadowRadius: 2, elevation: 1,
  },
  deviceIcon: {
    width: 44, height: 44, borderRadius: 22, backgroundColor: '#e3f2fd',
    justifyContent: 'center', alignItems: 'center', marginRight: 12,
  },
  deviceIconText: {fontSize: 12, fontWeight: '700', color: '#1565c0'},
  deviceInfo: {flex: 1},
  deviceType: {fontSize: 16, fontWeight: '600', color: '#1a1a1a'},
  deviceMac: {fontSize: 12, color: '#888', marginTop: 2, fontFamily: Platform.OS === 'ios' ? 'Menlo' : 'monospace'},
  deviceRssi: {fontSize: 12, color: '#999', marginLeft: 8},
  // Dashboard
  savedRow: {
    flexDirection: 'row', alignItems: 'center', backgroundColor: '#fff',
    padding: 14, borderRadius: 10, marginBottom: 8, elevation: 1,
  },
  removeText: {color: '#f44336', fontSize: 13, fontWeight: '600'},
  deviceOffline: {opacity: 0.5},
  deviceIconSaved: {backgroundColor: '#c8e6c9'},
  savedBadge: {fontSize: 10, color: '#4caf50', fontWeight: '700'},
  foundRow: {
    flexDirection: 'row', alignItems: 'center', backgroundColor: '#e8f5e9',
    padding: 14, borderRadius: 10, marginBottom: 8,
  },
  addButton: {backgroundColor: '#4caf50', paddingVertical: 8, paddingHorizontal: 16, borderRadius: 8},
  addButtonText: {color: '#fff', fontSize: 14, fontWeight: '600'},
  syncBar: {flexDirection: 'row', alignItems: 'center', padding: 10, backgroundColor: '#e3f2fd', borderRadius: 8, marginBottom: 8},
  syncText: {color: '#1565c0', fontSize: 13, marginLeft: 8},
  syncDone: {color: '#4caf50', fontSize: 13, marginBottom: 8, fontWeight: '600'},
  scanningRow: {flexDirection: 'row', alignItems: 'center', padding: 12},
  scanningText: {color: '#666', fontSize: 13, marginLeft: 8},
  // Readings
  readingRow: {
    backgroundColor: '#fff', padding: 14, borderRadius: 10, marginBottom: 8, elevation: 1,
  },
  readingValues: {flexDirection: 'row', alignItems: 'baseline'},
  readingSys: {fontSize: 28, fontWeight: '700', color: '#1a1a1a'},
  readingSlash: {fontSize: 20, color: '#999', marginHorizontal: 4},
  readingDia: {fontSize: 28, fontWeight: '700', color: '#1a1a1a'},
  readingUnit: {fontSize: 12, color: '#999', marginLeft: 6},
  readingPulse: {fontSize: 14, color: '#666', marginLeft: 16},
  readingMeta: {flexDirection: 'row', justifyContent: 'space-between', marginTop: 4},
  readingDate: {fontSize: 12, color: '#999'},
  readingMac: {fontSize: 10, color: '#bbb', fontFamily: Platform.OS === 'ios' ? 'Menlo' : 'monospace'},
  // Debug device
  statusBadge: {fontSize: 13, color: '#f44336', marginTop: 8, marginBottom: 16, fontWeight: '600'},
  statusConnected: {color: '#4caf50'},
  actionsRow: {flexGrow: 0, marginBottom: 12},
  actionButton: {backgroundColor: '#e3f2fd', paddingVertical: 10, paddingHorizontal: 16, borderRadius: 8, marginRight: 8},
  actionDisabled: {backgroundColor: '#eee'},
  actionText: {color: '#1565c0', fontSize: 13, fontWeight: '600'},
  actionTextDisabled: {color: '#bbb'},
  logList: {flex: 1, marginTop: 4},
  logLine: {fontSize: 11, color: '#555', paddingVertical: 3, fontFamily: Platform.OS === 'ios' ? 'Menlo' : 'monospace'},
});

export default App;