blinc_core 0.4.0

Blinc core runtime - reactive signals, state machines, and event dispatch
Documentation
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
//! Native Bridge for Rust ↔ Platform FFI
//!
//! Enables Rust code to call platform-native functions (Android/iOS) via
//! registered namespace/name pairs with type-safe contracts.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────┐
//! │              Rust App                    │
//! │  native_call("device", "get_battery")   │
//! └────────────────┬────────────────────────┘
//!//!//! ┌─────────────────────────────────────────┐
//! │          NativeBridgeState              │
//! │  - Rust handlers (fallback/testing)     │
//! │  - Platform adapter (JNI/Swift FFI)     │
//! └────────────────┬────────────────────────┘
//!//!       ┌──────────┴──────────┐
//!       ▼                     ▼
//! ┌───────────┐        ┌───────────┐
//! │  Android  │        │    iOS    │
//! │  (JNI)    │        │ (C FFI)   │
//! └───────────┘        └───────────┘
//! ```
//!
//! # Example
//!
//! ```ignore
//! use blinc_core::native_bridge::{native_call, native_register, NativeValue};
//!
//! // Register a Rust-side handler (for testing/fallback)
//! native_register("math", "add", |args| {
//!     let a = args.get(0).and_then(|v| v.as_i32()).unwrap_or(0);
//!     let b = args.get(1).and_then(|v| v.as_i32()).unwrap_or(0);
//!     Ok(NativeValue::Int32(a + b))
//! });
//!
//! // Call native function
//! let battery: String = native_call("device", "get_battery_level", ()).unwrap();
//! ```
//!
//! # Platform Registration
//!
//! Native platforms register handlers at app initialization:
//!
//! **Kotlin (Android):**
//! ```kotlin
//! BlincNativeBridge.register("device", "get_battery_level") {
//!     BatteryManager.getBatteryLevel().toString()
//! }
//! ```
//!
//! **Swift (iOS):**
//! ```swift
//! BlincNativeBridge.shared.register(namespace: "device", name: "get_battery_level") {
//!     String(UIDevice.current.batteryLevel)
//! }
//! ```

use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, OnceLock, RwLock};

// ============================================================================
// Types
// ============================================================================

/// Global native bridge singleton
static NATIVE_BRIDGE: OnceLock<NativeBridgeState> = OnceLock::new();

/// Result type for native bridge operations
pub type NativeResult<T> = Result<T, NativeBridgeError>;

/// Handler function type for native calls
pub type NativeHandler = Arc<dyn Fn(Vec<NativeValue>) -> NativeResult<NativeValue> + Send + Sync>;

/// Error type for native bridge operations
#[derive(Debug, Clone)]
pub enum NativeBridgeError {
    /// Function not registered in any handler
    NotRegistered { namespace: String, name: String },
    /// Type mismatch when extracting return value
    TypeMismatch {
        expected: &'static str,
        actual: String,
    },
    /// Platform-specific error (JNI, Swift FFI, etc.)
    PlatformError(String),
    /// JSON serialization/deserialization error
    SerializationError(String),
    /// Bridge not initialized
    NotInitialized,
}

impl fmt::Display for NativeBridgeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotRegistered { namespace, name } => {
                write!(f, "Native function '{}.{}' not registered", namespace, name)
            }
            Self::TypeMismatch { expected, actual } => {
                write!(f, "Type mismatch: expected {}, got {}", expected, actual)
            }
            Self::PlatformError(msg) => write!(f, "Platform error: {}", msg),
            Self::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
            Self::NotInitialized => write!(f, "Native bridge not initialized"),
        }
    }
}

impl std::error::Error for NativeBridgeError {}

/// Value type for cross-FFI transport
///
/// This enum represents values that can be passed between Rust and native code.
/// Complex types should use `Json` variant with serde serialization.
#[derive(Debug, Clone, PartialEq)]
pub enum NativeValue {
    /// No value (void return)
    Void,
    /// Boolean value
    Bool(bool),
    /// 32-bit signed integer
    Int32(i32),
    /// 64-bit signed integer
    Int64(i64),
    /// 32-bit float
    Float32(f32),
    /// 64-bit float
    Float64(f64),
    /// UTF-8 string
    String(String),
    /// Raw bytes
    Bytes(Vec<u8>),
    /// JSON-encoded complex type
    Json(String),
}

impl NativeValue {
    /// Extract as bool
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            NativeValue::Bool(v) => Some(*v),
            _ => None,
        }
    }

    /// Extract as i32
    pub fn as_i32(&self) -> Option<i32> {
        match self {
            NativeValue::Int32(v) => Some(*v),
            _ => None,
        }
    }

    /// Extract as i64
    pub fn as_i64(&self) -> Option<i64> {
        match self {
            NativeValue::Int64(v) => Some(*v),
            NativeValue::Int32(v) => Some(*v as i64),
            _ => None,
        }
    }

    /// Extract as f32
    pub fn as_f32(&self) -> Option<f32> {
        match self {
            NativeValue::Float32(v) => Some(*v),
            _ => None,
        }
    }

    /// Extract as f64
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            NativeValue::Float64(v) => Some(*v),
            NativeValue::Float32(v) => Some(*v as f64),
            _ => None,
        }
    }

    /// Extract as string reference
    pub fn as_str(&self) -> Option<&str> {
        match self {
            NativeValue::String(v) => Some(v),
            _ => None,
        }
    }

    /// Extract as owned string
    pub fn into_string(self) -> Option<String> {
        match self {
            NativeValue::String(v) => Some(v),
            _ => None,
        }
    }

    /// Extract as bytes reference
    pub fn as_bytes(&self) -> Option<&[u8]> {
        match self {
            NativeValue::Bytes(v) => Some(v),
            _ => None,
        }
    }

    /// Extract as JSON string reference
    pub fn as_json(&self) -> Option<&str> {
        match self {
            NativeValue::Json(v) => Some(v),
            _ => None,
        }
    }

    /// Get type name for error messages
    pub fn type_name(&self) -> &'static str {
        match self {
            NativeValue::Void => "Void",
            NativeValue::Bool(_) => "Bool",
            NativeValue::Int32(_) => "Int32",
            NativeValue::Int64(_) => "Int64",
            NativeValue::Float32(_) => "Float32",
            NativeValue::Float64(_) => "Float64",
            NativeValue::String(_) => "String",
            NativeValue::Bytes(_) => "Bytes",
            NativeValue::Json(_) => "Json",
        }
    }
}

// ============================================================================
// Conversion Traits
// ============================================================================

/// Trait for converting Rust types to NativeValue arguments
pub trait IntoNativeArgs {
    fn into_native_args(self) -> Vec<NativeValue>;
}

// Unit type (no args)
impl IntoNativeArgs for () {
    fn into_native_args(self) -> Vec<NativeValue> {
        vec![]
    }
}

// Single value tuples
impl IntoNativeArgs for (i32,) {
    fn into_native_args(self) -> Vec<NativeValue> {
        vec![NativeValue::Int32(self.0)]
    }
}

impl IntoNativeArgs for (i64,) {
    fn into_native_args(self) -> Vec<NativeValue> {
        vec![NativeValue::Int64(self.0)]
    }
}

impl IntoNativeArgs for (f32,) {
    fn into_native_args(self) -> Vec<NativeValue> {
        vec![NativeValue::Float32(self.0)]
    }
}

impl IntoNativeArgs for (f64,) {
    fn into_native_args(self) -> Vec<NativeValue> {
        vec![NativeValue::Float64(self.0)]
    }
}

impl IntoNativeArgs for (bool,) {
    fn into_native_args(self) -> Vec<NativeValue> {
        vec![NativeValue::Bool(self.0)]
    }
}

impl IntoNativeArgs for (String,) {
    fn into_native_args(self) -> Vec<NativeValue> {
        vec![NativeValue::String(self.0)]
    }
}

impl IntoNativeArgs for (&str,) {
    fn into_native_args(self) -> Vec<NativeValue> {
        vec![NativeValue::String(self.0.to_string())]
    }
}

// Two value tuples
impl IntoNativeArgs for (i32, i32) {
    fn into_native_args(self) -> Vec<NativeValue> {
        vec![NativeValue::Int32(self.0), NativeValue::Int32(self.1)]
    }
}

impl IntoNativeArgs for (String, String) {
    fn into_native_args(self) -> Vec<NativeValue> {
        vec![NativeValue::String(self.0), NativeValue::String(self.1)]
    }
}

impl IntoNativeArgs for (&str, &str) {
    fn into_native_args(self) -> Vec<NativeValue> {
        vec![
            NativeValue::String(self.0.to_string()),
            NativeValue::String(self.1.to_string()),
        ]
    }
}

// Vec of NativeValue (direct)
impl IntoNativeArgs for Vec<NativeValue> {
    fn into_native_args(self) -> Vec<NativeValue> {
        self
    }
}

/// Trait for extracting return values from NativeValue
pub trait FromNativeValue: Sized {
    fn from_native_value(value: NativeValue) -> NativeResult<Self>;
}

impl FromNativeValue for () {
    fn from_native_value(value: NativeValue) -> NativeResult<Self> {
        match value {
            NativeValue::Void => Ok(()),
            _ => Ok(()), // Accept any value for void return
        }
    }
}

impl FromNativeValue for bool {
    fn from_native_value(value: NativeValue) -> NativeResult<Self> {
        value
            .as_bool()
            .ok_or_else(|| NativeBridgeError::TypeMismatch {
                expected: "Bool",
                actual: value.type_name().to_string(),
            })
    }
}

impl FromNativeValue for i32 {
    fn from_native_value(value: NativeValue) -> NativeResult<Self> {
        value
            .as_i32()
            .ok_or_else(|| NativeBridgeError::TypeMismatch {
                expected: "Int32",
                actual: value.type_name().to_string(),
            })
    }
}

impl FromNativeValue for i64 {
    fn from_native_value(value: NativeValue) -> NativeResult<Self> {
        value
            .as_i64()
            .ok_or_else(|| NativeBridgeError::TypeMismatch {
                expected: "Int64",
                actual: value.type_name().to_string(),
            })
    }
}

impl FromNativeValue for f32 {
    fn from_native_value(value: NativeValue) -> NativeResult<Self> {
        value
            .as_f32()
            .ok_or_else(|| NativeBridgeError::TypeMismatch {
                expected: "Float32",
                actual: value.type_name().to_string(),
            })
    }
}

impl FromNativeValue for f64 {
    fn from_native_value(value: NativeValue) -> NativeResult<Self> {
        value
            .as_f64()
            .ok_or_else(|| NativeBridgeError::TypeMismatch {
                expected: "Float64",
                actual: value.type_name().to_string(),
            })
    }
}

impl FromNativeValue for String {
    fn from_native_value(value: NativeValue) -> NativeResult<Self> {
        value
            .into_string()
            .ok_or_else(|| NativeBridgeError::TypeMismatch {
                expected: "String",
                actual: "non-string".to_string(),
            })
    }
}

impl FromNativeValue for Vec<u8> {
    fn from_native_value(value: NativeValue) -> NativeResult<Self> {
        match value {
            NativeValue::Bytes(v) => Ok(v),
            _ => Err(NativeBridgeError::TypeMismatch {
                expected: "Bytes",
                actual: value.type_name().to_string(),
            }),
        }
    }
}

impl FromNativeValue for NativeValue {
    fn from_native_value(value: NativeValue) -> NativeResult<Self> {
        Ok(value)
    }
}

// ============================================================================
// Platform Adapter Trait
// ============================================================================

/// Trait for platform-specific native call adapters
///
/// Implemented by `AndroidNativeBridgeAdapter` and `IOSNativeBridgeAdapter`.
pub trait PlatformAdapter: Send + Sync {
    /// Call a native function by namespace and name
    fn call(
        &self,
        namespace: &str,
        name: &str,
        args: Vec<NativeValue>,
    ) -> NativeResult<NativeValue>;
}

// ============================================================================
// Native Bridge State
// ============================================================================

/// Global native bridge singleton
///
/// Manages Rust-side handlers and platform adapters for native function calls.
pub struct NativeBridgeState {
    /// Rust-registered handlers: namespace -> (name -> handler)
    handlers: RwLock<HashMap<String, HashMap<String, NativeHandler>>>,
    /// Platform adapter (JNI for Android, C FFI for iOS)
    platform_adapter: RwLock<Option<Arc<dyn PlatformAdapter>>>,
}

impl NativeBridgeState {
    /// Initialize the native bridge singleton
    ///
    /// Call once at app startup (typically in BlincContextState::init).
    ///
    /// # Panics
    ///
    /// Panics if called more than once.
    pub fn init() {
        let state = NativeBridgeState {
            handlers: RwLock::new(HashMap::new()),
            platform_adapter: RwLock::new(None),
        };

        if NATIVE_BRIDGE.set(state).is_err() {
            // Already initialized - this is fine, don't panic
            tracing::debug!("NativeBridgeState already initialized");
        }
    }

    /// Get the singleton instance
    ///
    /// # Panics
    ///
    /// Panics if `init()` has not been called.
    pub fn get() -> &'static NativeBridgeState {
        NATIVE_BRIDGE.get().expect(
            "NativeBridgeState not initialized. Call NativeBridgeState::init() at app startup.",
        )
    }

    /// Try to get the singleton (returns None if not initialized)
    pub fn try_get() -> Option<&'static NativeBridgeState> {
        NATIVE_BRIDGE.get()
    }

    /// Check if the bridge has been initialized
    pub fn is_initialized() -> bool {
        NATIVE_BRIDGE.get().is_some()
    }

    /// Set the platform adapter
    ///
    /// Called during platform initialization to wire up JNI/Swift FFI.
    pub fn set_platform_adapter(&self, adapter: Arc<dyn PlatformAdapter>) {
        *self.platform_adapter.write().unwrap() = Some(adapter);
    }

    /// Clear the platform adapter
    pub fn clear_platform_adapter(&self) {
        *self.platform_adapter.write().unwrap() = None;
    }

    /// Register a Rust-side handler
    ///
    /// Rust handlers are checked before platform adapters, useful for:
    /// - Testing without native code
    /// - Cross-platform fallback implementations
    /// - Rust-only functionality
    pub fn register<F>(&self, namespace: &str, name: &str, handler: F)
    where
        F: Fn(Vec<NativeValue>) -> NativeResult<NativeValue> + Send + Sync + 'static,
    {
        let mut handlers = self.handlers.write().unwrap();
        let ns_handlers = handlers.entry(namespace.to_string()).or_default();
        ns_handlers.insert(name.to_string(), Arc::new(handler));
    }

    /// Unregister a Rust-side handler
    pub fn unregister(&self, namespace: &str, name: &str) -> bool {
        let mut handlers = self.handlers.write().unwrap();
        if let Some(ns_handlers) = handlers.get_mut(namespace) {
            return ns_handlers.remove(name).is_some();
        }
        false
    }

    /// Call a native function
    ///
    /// Resolution order:
    /// 1. Rust-registered handlers
    /// 2. Platform adapter (JNI/Swift FFI)
    pub fn call<R, A>(&self, namespace: &str, name: &str, args: A) -> NativeResult<R>
    where
        R: FromNativeValue,
        A: IntoNativeArgs,
    {
        let native_args = args.into_native_args();

        // First check Rust-registered handlers
        {
            let handlers = self.handlers.read().unwrap();
            if let Some(ns_handlers) = handlers.get(namespace) {
                if let Some(handler) = ns_handlers.get(name) {
                    let result = handler(native_args.clone())?;
                    return R::from_native_value(result);
                }
            }
        }

        // Fall back to platform adapter
        if let Some(adapter) = self.platform_adapter.read().unwrap().as_ref() {
            let result = adapter.call(namespace, name, native_args)?;
            return R::from_native_value(result);
        }

        Err(NativeBridgeError::NotRegistered {
            namespace: namespace.to_string(),
            name: name.to_string(),
        })
    }

    /// Check if a handler is registered (Rust or platform)
    pub fn has_handler(&self, namespace: &str, name: &str) -> bool {
        // Check Rust handlers
        let handlers = self.handlers.read().unwrap();
        if let Some(ns_handlers) = handlers.get(namespace) {
            if ns_handlers.contains_key(name) {
                return true;
            }
        }

        // Platform adapter existence (can't check specific functions)
        self.platform_adapter.read().unwrap().is_some()
    }

    /// List all registered Rust namespaces
    pub fn namespaces(&self) -> Vec<String> {
        self.handlers.read().unwrap().keys().cloned().collect()
    }

    /// List all registered Rust functions in a namespace
    pub fn functions(&self, namespace: &str) -> Vec<String> {
        self.handlers
            .read()
            .unwrap()
            .get(namespace)
            .map(|ns| ns.keys().cloned().collect())
            .unwrap_or_default()
    }
}

// ============================================================================
// Convenience Free Functions
// ============================================================================

/// Call a native function
///
/// Convenience wrapper around `NativeBridgeState::get().call()`.
///
/// # Example
///
/// ```ignore
/// use blinc_core::native_bridge::native_call;
///
/// // Call with no args, String return
/// let battery: String = native_call("device", "get_battery_level", ())?;
///
/// // Call with args, void return
/// native_call::<(), _>("haptics", "vibrate", (100i32,))?;
/// ```
///
/// # Panics
///
/// Panics if `NativeBridgeState::init()` has not been called.
pub fn native_call<R, A>(namespace: &str, name: &str, args: A) -> NativeResult<R>
where
    R: FromNativeValue,
    A: IntoNativeArgs,
{
    NativeBridgeState::get().call(namespace, name, args)
}

/// Register a Rust-side native handler
///
/// Convenience wrapper around `NativeBridgeState::get().register()`.
///
/// # Example
///
/// ```ignore
/// use blinc_core::native_bridge::{native_register, NativeValue, NativeResult};
///
/// native_register("math", "add", |args| {
///     let a = args.get(0).and_then(|v| v.as_i32()).unwrap_or(0);
///     let b = args.get(1).and_then(|v| v.as_i32()).unwrap_or(0);
///     Ok(NativeValue::Int32(a + b))
/// });
/// ```
///
/// # Panics
///
/// Panics if `NativeBridgeState::init()` has not been called.
pub fn native_register<F>(namespace: &str, name: &str, handler: F)
where
    F: Fn(Vec<NativeValue>) -> NativeResult<NativeValue> + Send + Sync + 'static,
{
    NativeBridgeState::get().register(namespace, name, handler)
}

/// Set the platform adapter
///
/// Convenience wrapper for platform initialization code.
///
/// # Panics
///
/// Panics if `NativeBridgeState::init()` has not been called.
pub fn set_platform_adapter(adapter: Arc<dyn PlatformAdapter>) {
    NativeBridgeState::get().set_platform_adapter(adapter)
}

// ============================================================================
// JSON Helpers
// ============================================================================

/// Parse a JSON result string from native code
///
/// Expected format:
/// ```json
/// { "success": true, "value": ... }
/// { "success": false, "errorType": "...", "errorMessage": "..." }
/// ```
pub fn parse_native_result_json(json: &str) -> NativeResult<NativeValue> {
    // Simple JSON parsing without serde dependency in core
    // Platform adapters can use serde_json for full parsing

    if json.contains("\"success\":true") || json.contains("\"success\": true") {
        // Extract value - simplified parsing
        if let Some(value_start) = json.find("\"value\":") {
            let value_part = &json[value_start + 8..];
            let value_str = value_part.trim();

            if value_str.starts_with("null") || value_str.starts_with("\"null\"") {
                return Ok(NativeValue::Void);
            } else if value_str.starts_with("true") {
                return Ok(NativeValue::Bool(true));
            } else if value_str.starts_with("false") {
                return Ok(NativeValue::Bool(false));
            } else if let Some(stripped) = value_str.strip_prefix('"') {
                // String value - find closing quote
                if let Some(end) = stripped.find('"') {
                    let s = &stripped[..end];
                    return Ok(NativeValue::String(s.to_string()));
                }
            } else if let Ok(n) = value_str
                .chars()
                .take_while(|c| c.is_ascii_digit() || *c == '-' || *c == '.')
                .collect::<String>()
                .parse::<i64>()
            {
                if n >= i32::MIN as i64 && n <= i32::MAX as i64 {
                    return Ok(NativeValue::Int32(n as i32));
                } else {
                    return Ok(NativeValue::Int64(n));
                }
            }
        }
        Ok(NativeValue::Void)
    } else {
        // Error response
        let error_type = extract_json_string(json, "errorType").unwrap_or("Unknown");
        let error_msg = extract_json_string(json, "errorMessage").unwrap_or("Unknown error");

        match error_type {
            "NotRegistered" => Err(NativeBridgeError::NotRegistered {
                namespace: "unknown".to_string(),
                name: "unknown".to_string(),
            }),
            _ => Err(NativeBridgeError::PlatformError(error_msg.to_string())),
        }
    }
}

/// Helper to extract a string value from JSON
fn extract_json_string<'a>(json: &'a str, key: &str) -> Option<&'a str> {
    let search = format!("\"{}\":\"", key);
    if let Some(start) = json.find(&search) {
        let value_start = start + search.len();
        if let Some(end) = json[value_start..].find('"') {
            return Some(&json[value_start..value_start + end]);
        }
    }
    None
}

// ============================================================================
// Bidirectional Data Streams
// ============================================================================

/// Stream handle for bidirectional data between Rust and platform
///
/// Used for continuous data flows: camera frames, audio buffers,
/// sensor events, real-time communication.
///
/// # Example
///
/// ```ignore
/// use blinc_core::native_bridge::{native_stream, NativeValue};
///
/// // Open a stream from the camera
/// let stream = native_stream("camera", "preview", vec![
///     NativeValue::Int32(640),  // width
///     NativeValue::Int32(480),  // height
/// ], |data| {
///     // Called on each frame with RGBA bytes
///     let rgba = data.as_bytes().unwrap();
///     render_camera_frame(rgba);
/// });
///
/// // Stream is active until dropped
/// drop(stream);
/// ```
pub struct NativeStream {
    namespace: String,
    name: String,
    _callback_id: u64,
}

impl Drop for NativeStream {
    fn drop(&mut self) {
        remove_stream_callback(self._callback_id);
        let _ = native_call::<(), _>(&self.namespace, &format!("{}_stop", self.name), ());
    }
}

/// Callback type for stream data
pub type StreamCallback = Arc<dyn Fn(NativeValue) + Send + Sync>;

/// Global stream callback registry
static STREAM_CALLBACKS: std::sync::Mutex<Option<HashMap<u64, StreamCallback>>> =
    std::sync::Mutex::new(None);
static NEXT_STREAM_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);

/// Open a bidirectional data stream with the platform.
///
/// The callback fires on each data event from the platform side.
/// The stream stays active until the returned `NativeStream` is dropped.
pub fn native_stream<F>(
    namespace: &str,
    name: &str,
    args: Vec<NativeValue>,
    callback: F,
) -> NativeStream
where
    F: Fn(NativeValue) + Send + Sync + 'static,
{
    let id = NEXT_STREAM_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);

    {
        let mut guard = STREAM_CALLBACKS.lock().unwrap();
        let map = guard.get_or_insert_with(HashMap::new);
        map.insert(id, Arc::new(callback));
    }

    let mut start_args = args;
    start_args.push(NativeValue::Int64(id as i64));
    let _ = native_call::<(), _>(namespace, &format!("{}_start", name), start_args);

    NativeStream {
        namespace: namespace.to_string(),
        name: name.to_string(),
        _callback_id: id,
    }
}

/// Dispatch stream data from the platform to the registered callback.
///
/// Called by platform FFI when stream data arrives.
pub fn dispatch_stream_data(stream_id: u64, data: NativeValue) {
    let callback = {
        let guard = STREAM_CALLBACKS.lock().unwrap();
        guard.as_ref().and_then(|map| map.get(&stream_id).cloned())
    };
    if let Some(cb) = callback {
        cb(data);
    }
}

fn remove_stream_callback(id: u64) {
    let mut guard = STREAM_CALLBACKS.lock().unwrap();
    if let Some(map) = guard.as_mut() {
        map.remove(&id);
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_native_value_accessors() {
        assert_eq!(NativeValue::Bool(true).as_bool(), Some(true));
        assert_eq!(NativeValue::Int32(42).as_i32(), Some(42));
        assert_eq!(NativeValue::Int64(100).as_i64(), Some(100));
        assert_eq!(NativeValue::Float32(3.25).as_f32(), Some(3.25));
        assert_eq!(
            NativeValue::String("hello".to_string()).as_str(),
            Some("hello")
        );
    }

    #[test]
    fn test_native_value_type_mismatch() {
        assert_eq!(NativeValue::Int32(42).as_bool(), None);
        assert_eq!(NativeValue::Bool(true).as_i32(), None);
    }

    #[test]
    fn test_into_native_args() {
        let args: Vec<NativeValue> = ().into_native_args();
        assert!(args.is_empty());

        let args: Vec<NativeValue> = (42i32,).into_native_args();
        assert_eq!(args.len(), 1);
        assert_eq!(args[0].as_i32(), Some(42));

        let args: Vec<NativeValue> = (1i32, 2i32).into_native_args();
        assert_eq!(args.len(), 2);
    }

    #[test]
    fn test_from_native_value() {
        assert_eq!(i32::from_native_value(NativeValue::Int32(42)).unwrap(), 42);
        assert_eq!(
            String::from_native_value(NativeValue::String("test".to_string())).unwrap(),
            "test"
        );
        assert!(bool::from_native_value(NativeValue::Int32(42)).is_err());
    }

    #[test]
    fn test_parse_native_result_json() {
        let success = r#"{"success":true,"value":"hello"}"#;
        let result = parse_native_result_json(success).unwrap();
        assert_eq!(result.as_str(), Some("hello"));

        let error = r#"{"success":false,"errorType":"NotRegistered","errorMessage":"not found"}"#;
        let result = parse_native_result_json(error);
        assert!(result.is_err());
    }
}