oxigdal-wasm 0.1.4

WebAssembly bindings for OxiGDAL - Browser-based geospatial processing
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
//! Tile streaming and progressive loading
//!
//! This module provides advanced streaming capabilities for loading geospatial tiles
//! progressively, managing bandwidth, implementing adaptive quality, and providing
//! smooth user experience even with slow or unreliable network connections.
//!
//! # Overview
//!
//! The streaming module implements intelligent tile loading strategies:
//!
//! - **Adaptive Quality**: Automatically adjusts quality based on bandwidth
//! - **Bandwidth Estimation**: Tracks download speed and adjusts behavior
//! - **Progressive Loading**: Shows low-res immediately, enhances gradually
//! - **Priority-Based Loading**: Loads visible tiles before off-screen tiles
//! - **Stream Buffering**: Maintains buffer of recently loaded tiles
//! - **Prefetch Scheduling**: Intelligently prefetches likely-needed tiles
//! - **Multi-Resolution**: Supports loading multiple quality levels simultaneously
//!
//! # Adaptive Quality System
//!
//! Quality automatically adjusts based on network conditions:
//!
//! ```text
//! Bandwidth           Quality    Tile Size    Load Time
//! ────────────────────────────────────────────────────
//! < 2 Mbps           Low        128x128      Fast
//! 2-10 Mbps          Medium     256x256      Moderate
//! > 10 Mbps          High       512x512      Slow
//! Variable           Adaptive   Dynamic      Reactive
//! ```
//!
//! # Bandwidth Estimation
//!
//! The module continuously measures download performance:
//!
//! 1. **Track Transfers**: Record size and duration for each download
//! 2. **Calculate Average**: Compute rolling average over recent samples
//! 3. **Estimate Time**: Predict download time for future requests
//! 4. **Adjust Quality**: Lower quality if bandwidth drops
//!
//! ```rust
//! use oxigdal_wasm::BandwidthEstimator;
//!
//! let mut estimator = BandwidthEstimator::new();
//!
//! // Record downloads
//! estimator.record_transfer(262_144, 500.0); // 256KB in 500ms
//!
//! // Check bandwidth
//! let mbps = estimator.bandwidth_mbps();
//! println!("Current bandwidth: {:.2} Mbps", mbps);
//!
//! // Get quality suggestion
//! let quality = estimator.suggest_quality();
//! println!("Suggested quality: {:?}", quality);
//! ```
//!
//! # Progressive Loading Strategy
//!
//! Load tiles in order of importance:
//!
//! ```text
//! Priority  What                 When            Why
//! ──────────────────────────────────────────────────────
//! 1         Viewport center      Immediate       User focus
//! 2         Viewport edges       0-100ms         Nearby
//! 3         Adjacent tiles       100-500ms       Panning
//! 4         Parent tiles         500-1000ms      Zoom out
//! 5         Child tiles          1000-2000ms     Zoom in
//! 6         Distant tiles        2000ms+         Prefetch
//! ```
//!
//! # Stream Buffer Management
//!
//! The stream buffer acts as an LRU cache:
//!
//! ```ignore
//! use oxigdal_wasm::streaming::StreamBuffer;
//!
//! let mut buffer = StreamBuffer::new(50 * 1024 * 1024); // 50 MB
//!
//! // Add tiles to buffer
//! buffer.add(coord, tile_data)?;
//!
//! // Retrieve from buffer
//! if let Some(data) = buffer.get(&coord) {
//!     render_tile(data);
//! }
//!
//! // Check statistics
//! let stats = buffer.stats();
//! println!("Buffer: {} tiles, {:.1}% full",
//!     stats.tile_count,
//!     stats.utilization * 100.0
//! );
//! ```
//!
//! # Importance-Based Loading
//!
//! Tiles closer to viewport center load first:
//!
//! ```ignore
//! use oxigdal_wasm::streaming::ImportanceCalculator;
//!
//! let calc = ImportanceCalculator::new(
//!     (viewport_center_x, viewport_center_y),
//!     (viewport_width, viewport_height)
//! );
//!
//! let mut tiles = vec![
//!     TileCoord::new(0, 10, 10),  // Center
//!     TileCoord::new(0, 15, 15),  // Far
//!     TileCoord::new(0, 11, 10),  // Near
//! ];
//!
//! // Sort by importance (center first)
//! calc.sort_by_importance(&mut tiles);
//!
//! // Load in order
//! for tile in tiles {
//!     load_tile(tile).await;
//! }
//! ```
//!
//! # Complete Streaming Example
//!
//! ```ignore
//! use oxigdal_wasm::streaming::{TileStreamer, StreamingQuality};
//!
//! // Create streamer with 50 MB buffer
//! let mut streamer = TileStreamer::new(50);
//!
//! // Enable adaptive quality
//! streamer.set_quality(StreamingQuality::Adaptive);
//!
//! // Request visible tiles
//! for coord in visible_tiles {
//!     streamer.request_tile(coord, timestamp);
//! }
//!
//! // Simulate tile load
//! async fn load_tiles(streamer: &mut TileStreamer) {
//!     while let Some(coord) = next_tile_to_load() {
//!         let start = js_sys::Date::now();
//!         let data = fetch_tile(coord).await?;
//!         let elapsed = js_sys::Date::now() - start;
//!
//!         // Complete the load (updates bandwidth estimate)
//!         streamer.complete_tile(
//!             coord,
//!             data,
//!             elapsed,
//!             start + elapsed
//!         )?;
//!     }
//! }
//!
//! // Check streaming stats
//! let stats = streamer.stats();
//! println!("Quality: {:?}", stats.current_quality);
//! println!("Bandwidth: {:.2} Mbps", stats.bandwidth_mbps);
//! println!("Pending: {}", stats.pending_tiles);
//! ```
//!
//! # Multi-Resolution Streaming
//!
//! Load multiple quality levels simultaneously:
//!
//! ```ignore
//! use oxigdal_wasm::streaming::MultiResolutionStreamer;
//!
//! let mut mstreamer = MultiResolutionStreamer::new();
//!
//! // Add streamers for different resolutions
//! mstreamer.add_resolution(128, 20);  // Low-res, 20MB buffer
//! mstreamer.add_resolution(256, 50);  // Mid-res, 50MB buffer
//! mstreamer.add_resolution(512, 100); // High-res, 100MB buffer
//!
//! // Request at appropriate resolution
//! if bandwidth < 2.0 {
//!     mstreamer.request_tile(128, coord, timestamp);
//! } else if bandwidth < 10.0 {
//!     mstreamer.request_tile(256, coord, timestamp);
//! } else {
//!     mstreamer.request_tile(512, coord, timestamp);
//! }
//! ```
//!
//! # Prefetch Scheduling
//!
//! Intelligently schedule background tile loading:
//!
//! ```ignore
//! use oxigdal_wasm::streaming::{PrefetchScheduler, RequestPriority};
//!
//! let mut scheduler = PrefetchScheduler::new(4); // 4 concurrent prefetches
//!
//! // Schedule high-priority tiles (visible)
//! for coord in visible_tiles {
//!     scheduler.schedule(coord, RequestPriority::High);
//! }
//!
//! // Schedule low-priority tiles (prefetch)
//! for coord in adjacent_tiles {
//!     scheduler.schedule(coord, RequestPriority::Low);
//! }
//!
//! // Process next batch
//! while let Some(coord) = scheduler.next(timestamp) {
//!     tokio::spawn(async move {
//!         load_and_cache_tile(coord).await;
//!         scheduler.complete(coord);
//!     });
//! }
//! ```
//!
//! # Performance Characteristics
//!
//! ## Bandwidth Estimation
//! - Sample window: 20 recent transfers
//! - Update frequency: Every transfer
//! - Accuracy: ±10% typical
//! - Latency: Adapts within 5-10 transfers
//!
//! ## Buffer Management
//! - Lookup: O(1) average
//! - Insert: O(1) average
//! - Eviction: O(1)
//! - Overhead: ~100 bytes per tile + data
//!
//! ## Importance Calculation
//! - Euclidean distance from viewport center
//! - Normalized to [0, 1] range
//! - Sorting: O(n log n)
//! - Typical: < 1ms for 1000 tiles
//!
//! # Best Practices
//!
//! 1. **Start Low**: Begin with low quality for quick feedback
//! 2. **Enhance Progressively**: Load higher quality incrementally
//! 3. **Monitor Bandwidth**: Track and react to changes
//! 4. **Prioritize Visible**: Always load visible tiles first
//! 5. **Limit Prefetch**: Don't prefetch more than 2-3 levels away
//! 6. **Clean Up**: Remove old tiles from buffer periodically
//! 7. **Handle Errors**: Network can fail, always have fallbacks
//! 8. **Test Mobile**: Mobile bandwidth varies greatly
//! 9. **Consider Latency**: High latency hurts more than low bandwidth
//! 10. **Profile Real Users**: Test on actual user connections
//!
//! # Common Patterns
//!
//! ## Pattern: Smooth Zooming
//! Load intermediate levels during zoom:
//! ```ignore
//! async fn zoom_smoothly(target_level: u32) {
//!     // Load each intermediate level
//!     for level in current_level..=target_level {
//!         load_level(level).await;
//!         render();
//!         await next_frame();
//!     }
//! }
//! ```
//!
//! ## Pattern: Bandwidth-Adaptive Quality
//! Adjust quality based on measured performance:
//! ```ignore
//! fn update_quality(streamer: &mut TileStreamer) {
//!     let stats = streamer.stats();
//!
//!     match stats.bandwidth_mbps {
//!         bw if bw < 1.0 => streamer.set_quality(StreamingQuality::Low),
//!         bw if bw < 5.0 => streamer.set_quality(StreamingQuality::Medium),
//!         _ => streamer.set_quality(StreamingQuality::High),
//!     }
//! }
//! ```
//!
//! ## Pattern: Predictive Prefetching
//! Prefetch based on pan direction:
//! ```ignore
//! fn prefetch_ahead(pan_vector: (f64, f64)) {
//!     let (dx, dy) = pan_vector;
//!
//!     // Prefetch 3 tiles ahead in pan direction
//!     for i in 1..=3 {
//!         let x = current_x + (dx * i as f64) as i32;
//!         let y = current_y + (dy * i as f64) as i32;
//!         schedule_prefetch(TileCoord::new(level, x, y));
//!     }
//! }
//! ```
//!
//! # Troubleshooting
//!
//! ## Slow Initial Load
//! - Reduce initial quality
//! - Increase buffer size
//! - Enable CDN caching
//! - Optimize server response time
//!
//! ## Stuttering During Pan
//! - Increase prefetch radius
//! - Lower quality during motion
//! - Use double buffering
//! - Preload adjacent tiles
//!
//! ## High Memory Usage
//! - Reduce buffer size
//! - Enable compression
//! - Clear old tiles more aggressively
//! - Monitor with profiler
//!
//! ## Bandwidth Estimation Inaccurate
//! - Increase sample window
//! - Filter outliers
//! - Account for CDN caching
//! - Consider connection type detection

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};

use crate::error::{WasmError, WasmResult};
use crate::fetch::RequestPriority;
use crate::tile::TileCoord;

/// Streaming quality level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum StreamingQuality {
    /// Low quality (fast loading)
    Low,
    /// Medium quality (balanced)
    Medium,
    /// High quality (slow loading)
    High,
    /// Adaptive (adjusts based on connection)
    Adaptive,
}

impl StreamingQuality {
    /// Returns the quality multiplier
    pub const fn multiplier(&self) -> f64 {
        match self {
            Self::Low => 0.5,
            Self::Medium => 1.0,
            Self::High => 2.0,
            Self::Adaptive => 1.0, // Will be adjusted dynamically
        }
    }

    /// Returns the tile resolution for this quality
    pub const fn resolution(&self) -> u32 {
        match self {
            Self::Low => 128,
            Self::Medium => 256,
            Self::High => 512,
            Self::Adaptive => 256,
        }
    }
}

/// Bandwidth estimator
#[derive(Debug, Clone)]
pub struct BandwidthEstimator {
    /// Recent transfer sizes (bytes)
    transfer_sizes: VecDeque<usize>,
    /// Recent transfer times (milliseconds)
    transfer_times: VecDeque<f64>,
    /// Maximum samples to keep
    max_samples: usize,
    /// Current estimated bandwidth (bytes per second)
    estimated_bandwidth: f64,
}

impl BandwidthEstimator {
    /// Creates a new bandwidth estimator
    pub fn new() -> Self {
        Self {
            transfer_sizes: VecDeque::new(),
            transfer_times: VecDeque::new(),
            max_samples: 20,
            estimated_bandwidth: 1_000_000.0, // Default: 1 MB/s
        }
    }

    /// Records a transfer
    pub fn record_transfer(&mut self, bytes: usize, time_ms: f64) {
        self.transfer_sizes.push_back(bytes);
        self.transfer_times.push_back(time_ms);

        if self.transfer_sizes.len() > self.max_samples {
            self.transfer_sizes.pop_front();
            self.transfer_times.pop_front();
        }

        self.update_estimate();
    }

    /// Records a download (alias for record_transfer for test compatibility)
    pub fn record_download(&mut self, bytes: usize, time_ms: f64) {
        self.record_transfer(bytes, time_ms);
    }

    /// Estimates current bandwidth (returns bandwidth in bytes per second)
    pub const fn estimate(&self) -> f64 {
        self.estimated_bandwidth
    }

    /// Updates the bandwidth estimate
    fn update_estimate(&mut self) {
        if self.transfer_sizes.is_empty() || self.transfer_times.is_empty() {
            return;
        }

        let total_bytes: usize = self.transfer_sizes.iter().sum();
        let total_time: f64 = self.transfer_times.iter().sum();

        if total_time > 0.0 {
            // Convert to bytes per second
            self.estimated_bandwidth = (total_bytes as f64 / total_time) * 1000.0;
        }
    }

    /// Returns the current bandwidth estimate in bytes per second
    pub const fn bandwidth_bps(&self) -> f64 {
        self.estimated_bandwidth
    }

    /// Returns the current bandwidth estimate in megabits per second
    pub fn bandwidth_mbps(&self) -> f64 {
        (self.estimated_bandwidth * 8.0) / 1_000_000.0
    }

    /// Estimates time to download a given size (in milliseconds)
    pub fn estimate_download_time(&self, bytes: usize) -> f64 {
        if self.estimated_bandwidth > 0.0 {
            (bytes as f64 / self.estimated_bandwidth) * 1000.0
        } else {
            f64::MAX
        }
    }

    /// Checks if bandwidth is sufficient for quality
    pub fn is_sufficient_for_quality(&self, quality: StreamingQuality) -> bool {
        let required_bps = match quality {
            StreamingQuality::Low => 500_000.0,      // 500 KB/s
            StreamingQuality::Medium => 1_000_000.0, // 1 MB/s
            StreamingQuality::High => 5_000_000.0,   // 5 MB/s
            StreamingQuality::Adaptive => 0.0,       // Always sufficient
        };

        self.estimated_bandwidth >= required_bps
    }

    /// Suggests optimal quality based on bandwidth
    pub fn suggest_quality(&self) -> StreamingQuality {
        let mbps = self.bandwidth_mbps();

        if mbps < 2.0 {
            StreamingQuality::Low
        } else if mbps < 10.0 {
            StreamingQuality::Medium
        } else {
            StreamingQuality::High
        }
    }
}

impl Default for BandwidthEstimator {
    fn default() -> Self {
        Self::new()
    }
}

/// Quality adapter for dynamic quality adjustment based on bandwidth
#[derive(Debug, Clone)]
pub struct QualityAdapter {
    /// Bandwidth estimator
    estimator: BandwidthEstimator,
    /// Current quality level
    current_quality: StreamingQuality,
    /// Quality change hysteresis to prevent oscillation
    hysteresis_count: usize,
    /// Number of consecutive samples before changing quality
    hysteresis_threshold: usize,
}

impl QualityAdapter {
    /// Creates a new quality adapter
    pub fn new() -> Self {
        Self {
            estimator: BandwidthEstimator::new(),
            current_quality: StreamingQuality::Medium,
            hysteresis_count: 0,
            hysteresis_threshold: 3,
        }
    }

    /// Updates bandwidth measurement
    pub fn update_bandwidth(&mut self, bandwidth_bps: f64, _timestamp: f64) {
        // Simulate a transfer to update the estimator
        // Clamp bandwidth to prevent overflow (max 1 GB/s = 1_000_000_000 bytes/s)
        let bandwidth_bps = bandwidth_bps.min(8_000_000_000.0); // 8 Gbps max
        let bytes = (bandwidth_bps / 8.0) as usize; // 1 second worth of data
        self.estimator.record_transfer(bytes, 1000.0); // 1 second = 1000ms

        self.update_quality();
    }

    /// Returns the current quality level
    pub const fn current_quality(&self) -> StreamingQuality {
        self.current_quality
    }

    /// Updates quality level based on bandwidth with hysteresis
    fn update_quality(&mut self) {
        let suggested = self.estimator.suggest_quality();

        if suggested != self.current_quality {
            self.hysteresis_count += 1;

            if self.hysteresis_count >= self.hysteresis_threshold {
                self.current_quality = suggested;
                self.hysteresis_count = 0;
            }
        } else {
            self.hysteresis_count = 0;
        }
    }
}

impl Default for QualityAdapter {
    fn default() -> Self {
        Self::new()
    }
}

/// Stream buffer for managing loaded tiles
#[derive(Debug, Clone)]
pub struct StreamBuffer {
    /// Buffered tiles
    tiles: HashMap<TileCoord, Vec<u8>>,
    /// Buffer size limit in bytes
    max_size: usize,
    /// Current buffer size
    current_size: usize,
    /// Access order for LRU eviction
    access_order: VecDeque<TileCoord>,
}

impl StreamBuffer {
    /// Creates a new stream buffer
    pub fn new(max_size: usize) -> Self {
        Self {
            tiles: HashMap::new(),
            max_size,
            current_size: 0,
            access_order: VecDeque::new(),
        }
    }

    /// Adds a tile to the buffer
    pub fn add(&mut self, coord: TileCoord, data: Vec<u8>) -> WasmResult<()> {
        let data_size = data.len();

        // Evict tiles if necessary
        while self.current_size + data_size > self.max_size && !self.access_order.is_empty() {
            self.evict_oldest()?;
        }

        // Check if tile would fit
        if data_size > self.max_size {
            return Err(WasmError::OutOfMemory {
                requested: data_size,
                available: Some(self.max_size),
            });
        }

        // Remove old entry if exists
        if self.tiles.contains_key(&coord) {
            if let Some(old_data) = self.tiles.remove(&coord) {
                self.current_size -= old_data.len();
            }
            if let Some(pos) = self.access_order.iter().position(|c| *c == coord) {
                self.access_order.remove(pos);
            }
        }

        // Add new tile
        self.tiles.insert(coord, data);
        self.current_size += data_size;
        self.access_order.push_back(coord);

        Ok(())
    }

    /// Gets a tile from the buffer
    pub fn get(&mut self, coord: &TileCoord) -> Option<&[u8]> {
        if let Some(data) = self.tiles.get(coord) {
            // Update access order
            if let Some(pos) = self.access_order.iter().position(|c| c == coord) {
                self.access_order.remove(pos);
                self.access_order.push_back(*coord);
            }

            Some(data)
        } else {
            None
        }
    }

    /// Evicts the oldest tile
    fn evict_oldest(&mut self) -> WasmResult<()> {
        if let Some(coord) = self.access_order.pop_front() {
            if let Some(data) = self.tiles.remove(&coord) {
                self.current_size -= data.len();
            }
        }

        Ok(())
    }

    /// Checks if a tile is in the buffer
    pub fn contains(&self, coord: &TileCoord) -> bool {
        self.tiles.contains_key(coord)
    }

    /// Clears the buffer
    pub fn clear(&mut self) {
        self.tiles.clear();
        self.access_order.clear();
        self.current_size = 0;
    }

    /// Returns buffer statistics
    pub fn stats(&self) -> StreamBufferStats {
        StreamBufferStats {
            tile_count: self.tiles.len(),
            current_size: self.current_size,
            max_size: self.max_size,
            utilization: self.current_size as f64 / self.max_size as f64,
        }
    }
}

/// Stream buffer statistics
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct StreamBufferStats {
    /// Number of tiles in buffer
    pub tile_count: usize,
    /// Current buffer size in bytes
    pub current_size: usize,
    /// Maximum buffer size in bytes
    pub max_size: usize,
    /// Buffer utilization (0.0 to 1.0)
    pub utilization: f64,
}

/// Load strategy for streaming
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoadStrategy {
    /// Load nearest tiles first
    Nearest,
    /// Load in spiral pattern from center
    Spiral,
    /// Load by importance (based on viewport)
    Importance,
    /// Load adaptively based on connection
    Adaptive,
}

/// Tile importance calculator
pub struct ImportanceCalculator {
    /// Viewport center
    viewport_center: (f64, f64),
    /// Viewport size
    viewport_size: (f64, f64),
}

impl ImportanceCalculator {
    /// Creates a new importance calculator
    pub const fn new(viewport_center: (f64, f64), viewport_size: (f64, f64)) -> Self {
        Self {
            viewport_center,
            viewport_size,
        }
    }

    /// Calculates importance score for a tile (0.0 to 1.0)
    pub fn calculate(&self, coord: &TileCoord) -> f64 {
        let tile_center_x = (f64::from(coord.x) + 0.5) * 256.0;
        let tile_center_y = (f64::from(coord.y) + 0.5) * 256.0;

        // Distance from viewport center
        let dx = tile_center_x - self.viewport_center.0;
        let dy = tile_center_y - self.viewport_center.1;
        let distance = (dx * dx + dy * dy).sqrt();

        // Normalize by viewport size
        let max_distance = (self.viewport_size.0 * self.viewport_size.0
            + self.viewport_size.1 * self.viewport_size.1)
            .sqrt();

        let normalized_distance = if max_distance > 0.0 {
            (distance / max_distance).min(1.0)
        } else {
            0.0
        };

        // Importance is inverse of distance
        1.0 - normalized_distance
    }

    /// Sorts tiles by importance
    pub fn sort_by_importance(&self, tiles: &mut Vec<TileCoord>) {
        tiles.sort_by(|a, b| {
            let imp_a = self.calculate(a);
            let imp_b = self.calculate(b);
            imp_b
                .partial_cmp(&imp_a)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
    }
}

/// Progressive tile streamer
pub struct TileStreamer {
    /// Stream buffer
    buffer: StreamBuffer,
    /// Bandwidth estimator
    bandwidth: BandwidthEstimator,
    /// Current quality
    quality: StreamingQuality,
    /// Load strategy
    strategy: LoadStrategy,
    /// Pending tile requests
    pending: HashMap<TileCoord, f64>, // coord -> request_time
    /// Completed tile loads
    completed: HashMap<TileCoord, f64>, // coord -> completion_time
}

impl TileStreamer {
    /// Creates a new tile streamer
    pub fn new(buffer_size_mb: usize) -> Self {
        Self {
            buffer: StreamBuffer::new(buffer_size_mb * 1024 * 1024),
            bandwidth: BandwidthEstimator::new(),
            quality: StreamingQuality::Adaptive,
            strategy: LoadStrategy::Adaptive,
            pending: HashMap::new(),
            completed: HashMap::new(),
        }
    }

    /// Sets the streaming quality
    pub fn set_quality(&mut self, quality: StreamingQuality) {
        self.quality = quality;
    }

    /// Sets the load strategy
    pub fn set_strategy(&mut self, strategy: LoadStrategy) {
        self.strategy = strategy;
    }

    /// Requests a tile
    pub fn request_tile(&mut self, coord: TileCoord, timestamp: f64) {
        if !self.buffer.contains(&coord) && !self.pending.contains_key(&coord) {
            self.pending.insert(coord, timestamp);
        }
    }

    /// Marks a tile as completed
    pub fn complete_tile(
        &mut self,
        coord: TileCoord,
        data: Vec<u8>,
        load_time_ms: f64,
        timestamp: f64,
    ) -> WasmResult<()> {
        self.pending.remove(&coord);
        self.completed.insert(coord, timestamp);
        self.bandwidth.record_transfer(data.len(), load_time_ms);
        self.buffer.add(coord, data)?;

        // Adjust quality if adaptive
        if matches!(self.quality, StreamingQuality::Adaptive) {
            let suggested = self.bandwidth.suggest_quality();
            self.quality = suggested;
        }

        Ok(())
    }

    /// Gets a tile from the buffer
    pub fn get_tile(&mut self, coord: &TileCoord) -> Option<&[u8]> {
        self.buffer.get(coord)
    }

    /// Returns the number of pending requests
    pub fn pending_count(&self) -> usize {
        self.pending.len()
    }

    /// Returns the current quality
    pub const fn current_quality(&self) -> StreamingQuality {
        self.quality
    }

    /// Returns streaming statistics
    pub fn stats(&self) -> StreamingStats {
        StreamingStats {
            buffer: self.buffer.stats(),
            bandwidth_mbps: self.bandwidth.bandwidth_mbps(),
            pending_tiles: self.pending.len(),
            completed_tiles: self.completed.len(),
            current_quality: self.quality,
        }
    }

    /// Clears all state
    pub fn clear(&mut self) {
        self.buffer.clear();
        self.pending.clear();
        self.completed.clear();
    }
}

/// Streaming statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamingStats {
    /// Buffer statistics
    pub buffer: StreamBufferStats,
    /// Current bandwidth in Mbps
    pub bandwidth_mbps: f64,
    /// Number of pending tiles
    pub pending_tiles: usize,
    /// Number of completed tiles
    pub completed_tiles: usize,
    /// Current quality level
    pub current_quality: StreamingQuality,
}

/// Multi-resolution tile streamer
pub struct MultiResolutionStreamer {
    /// Streamers for different resolutions
    streamers: HashMap<u32, TileStreamer>,
}

impl MultiResolutionStreamer {
    /// Creates a new multi-resolution streamer
    pub fn new() -> Self {
        Self {
            streamers: HashMap::new(),
        }
    }

    /// Adds a streamer for a resolution
    pub fn add_resolution(&mut self, resolution: u32, buffer_size_mb: usize) {
        self.streamers
            .insert(resolution, TileStreamer::new(buffer_size_mb));
    }

    /// Requests a tile at a specific resolution
    pub fn request_tile(&mut self, resolution: u32, coord: TileCoord, timestamp: f64) {
        if let Some(streamer) = self.streamers.get_mut(&resolution) {
            streamer.request_tile(coord, timestamp);
        }
    }

    /// Gets a tile at a specific resolution
    pub fn get_tile(&mut self, resolution: u32, coord: &TileCoord) -> Option<&[u8]> {
        self.streamers
            .get_mut(&resolution)
            .and_then(|s| s.get_tile(coord))
    }

    /// Returns statistics for all resolutions
    pub fn all_stats(&self) -> HashMap<u32, StreamingStats> {
        self.streamers
            .iter()
            .map(|(&res, streamer)| (res, streamer.stats()))
            .collect()
    }
}

impl Default for MultiResolutionStreamer {
    fn default() -> Self {
        Self::new()
    }
}

/// Prefetch scheduler
pub struct PrefetchScheduler {
    /// Tiles to prefetch
    queue: VecDeque<(TileCoord, RequestPriority)>,
    /// Maximum concurrent prefetches
    max_concurrent: usize,
    /// Active prefetches
    active: HashMap<TileCoord, f64>, // coord -> start_time
}

impl PrefetchScheduler {
    /// Creates a new prefetch scheduler
    pub fn new(max_concurrent: usize) -> Self {
        Self {
            queue: VecDeque::new(),
            max_concurrent,
            active: HashMap::new(),
        }
    }

    /// Schedules a tile for prefetch
    pub fn schedule(&mut self, coord: TileCoord, priority: RequestPriority) {
        // Remove from queue if already scheduled
        self.queue.retain(|(c, _)| *c != coord);

        // Insert based on priority
        let pos = self
            .queue
            .iter()
            .position(|(_, p)| *p < priority)
            .unwrap_or(self.queue.len());

        self.queue.insert(pos, (coord, priority));
    }

    /// Gets the next tile to prefetch
    pub fn next(&mut self, timestamp: f64) -> Option<TileCoord> {
        if self.active.len() >= self.max_concurrent {
            return None;
        }

        if let Some((coord, _)) = self.queue.pop_front() {
            self.active.insert(coord, timestamp);
            Some(coord)
        } else {
            None
        }
    }

    /// Marks a prefetch as complete
    pub fn complete(&mut self, coord: TileCoord) {
        self.active.remove(&coord);
    }

    /// Returns the number of pending prefetches
    pub fn pending_count(&self) -> usize {
        self.queue.len()
    }

    /// Returns the number of active prefetches
    pub fn active_count(&self) -> usize {
        self.active.len()
    }

    /// Clears all state
    pub fn clear(&mut self) {
        self.queue.clear();
        self.active.clear();
    }

    /// Schedules prefetch for viewport
    pub fn schedule_prefetch(&self, viewport: &crate::Viewport, _level: usize) -> Vec<TileCoord> {
        // Calculate visible tiles based on viewport
        let bounds = viewport.bounds();
        let mut tiles = Vec::new();

        // Estimate tile range based on viewport bounds
        let min_x = (bounds.0.max(0.0) / viewport.width as f64) as u32;
        let min_y = (bounds.1.max(0.0) / viewport.height as f64) as u32;
        let max_x = ((bounds.2 / viewport.width as f64).ceil() as u32).min(100);
        let max_y = ((bounds.3 / viewport.height as f64).ceil() as u32).min(100);

        for x in min_x..=max_x {
            for y in min_y..=max_y {
                tiles.push(TileCoord::new(0, x, y));
            }
        }

        tiles
    }
}

/// Progressive loader for prioritized tile loading
#[derive(Debug, Clone)]
pub struct ProgressiveLoader {
    /// Loaded tiles
    loaded: Vec<TileCoord>,
}

impl ProgressiveLoader {
    /// Creates a new progressive loader
    pub const fn new() -> Self {
        Self { loaded: Vec::new() }
    }

    /// Prioritizes tiles by distance from center
    pub fn prioritize_tiles(&self, tiles: &[TileCoord]) -> Vec<TileCoord> {
        let mut result = tiles.to_vec();

        // Sort by level (lower levels first for progressive loading)
        result.sort_by_key(|coord| coord.level);

        result
    }

    /// Marks a tile as loaded
    pub fn mark_loaded(&mut self, coord: TileCoord) {
        if !self.loaded.contains(&coord) {
            self.loaded.push(coord);
        }
    }

    /// Checks if a tile is loaded
    pub fn is_loaded(&self, coord: &TileCoord) -> bool {
        self.loaded.contains(coord)
    }

    /// Clears loaded tiles
    pub fn clear(&mut self) {
        self.loaded.clear();
    }
}

impl Default for ProgressiveLoader {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_bandwidth_estimator() {
        let mut estimator = BandwidthEstimator::new();

        // Record some transfers
        estimator.record_transfer(1_000_000, 1000.0); // 1 MB in 1 second
        estimator.record_transfer(2_000_000, 2000.0); // 2 MB in 2 seconds

        let bps = estimator.bandwidth_bps();
        assert!(bps > 900_000.0 && bps < 1_100_000.0);
    }

    #[test]
    fn test_bandwidth_quality_suggestion() {
        let mut estimator = BandwidthEstimator::new();

        // Simulate slow connection
        estimator.record_transfer(100_000, 1000.0); // 100 KB/s
        assert_eq!(estimator.suggest_quality(), StreamingQuality::Low);

        // Simulate fast connection
        estimator.record_transfer(10_000_000, 1000.0); // 10 MB/s
        assert_eq!(estimator.suggest_quality(), StreamingQuality::High);
    }

    #[test]
    fn test_stream_buffer() {
        let mut buffer = StreamBuffer::new(1000);
        let coord = TileCoord::new(0, 0, 0);
        let data = vec![1, 2, 3, 4, 5];

        buffer.add(coord, data.clone()).expect("Add failed");
        assert!(buffer.contains(&coord));

        let retrieved = buffer.get(&coord).expect("Get failed");
        assert_eq!(retrieved, &data[..]);
    }

    #[test]
    fn test_stream_buffer_eviction() {
        let mut buffer = StreamBuffer::new(20); // Very small buffer

        let coord1 = TileCoord::new(0, 0, 0);
        let coord2 = TileCoord::new(0, 1, 0);

        buffer.add(coord1, vec![0; 15]).expect("Add 1");
        buffer.add(coord2, vec![0; 15]).expect("Add 2");

        // First tile should be evicted
        assert!(!buffer.contains(&coord1));
        assert!(buffer.contains(&coord2));
    }

    #[test]
    fn test_importance_calculator() {
        let calc = ImportanceCalculator::new((500.0, 500.0), (1000.0, 1000.0));

        // Tile at center should have high importance
        let center_tile = TileCoord::new(0, 2, 2);
        let center_imp = calc.calculate(&center_tile);
        assert!(center_imp > 0.8);

        // Tile far from center should have lower importance
        let far_tile = TileCoord::new(0, 10, 10);
        let far_imp = calc.calculate(&far_tile);
        assert!(far_imp < center_imp);
    }

    #[test]
    fn test_tile_streamer() {
        let mut streamer = TileStreamer::new(10); // 10 MB buffer
        let coord = TileCoord::new(0, 0, 0);

        streamer.request_tile(coord, 0.0);
        assert_eq!(streamer.pending_count(), 1);

        let data = vec![0u8; 1000];
        streamer
            .complete_tile(coord, data, 10.0, 1.0)
            .expect("Complete failed");

        assert_eq!(streamer.pending_count(), 0);
        assert!(streamer.get_tile(&coord).is_some());
    }

    #[test]
    fn test_prefetch_scheduler() {
        let mut scheduler = PrefetchScheduler::new(2);

        scheduler.schedule(TileCoord::new(0, 0, 0), RequestPriority::Low);
        scheduler.schedule(TileCoord::new(0, 1, 0), RequestPriority::High);

        // High priority should come first
        let next = scheduler.next(0.0).expect("Should have tile");
        assert_eq!(next, TileCoord::new(0, 1, 0));

        assert_eq!(scheduler.active_count(), 1);
        assert_eq!(scheduler.pending_count(), 1);
    }
}