tiles_tools 0.2.0

High-performance tile-based game development toolkit with comprehensive coordinate systems (hexagonal, square, triangular, isometric), pathfinding, ECS integration, and grid management.
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
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
//! Visual debugging tools and utilities for tile-based game development.
//!
//! This module provides comprehensive debugging capabilities including grid visualization,
//! pathfinding overlays, ECS component inspection, performance profiling, and diagnostic
//! tools. These utilities are essential for development, testing, and optimization of
//! tile-based games.
//!
//! # Debugging Features
//!
//! - **Grid Visualization**: Render coordinate systems with customizable styles
//! - **Pathfinding Debug**: Visualize A* paths, flow fields, and navigation costs
//! - **ECS Inspector**: Runtime component inspection and entity tracking
//! - **Performance Profiler**: Frame timing, memory usage, and bottleneck detection
//! - **Spatial Debug**: Quadtree visualization and collision boundary display
//! - **Event Monitoring**: Real-time event system diagnostics
//!
//! # Debug Output Formats
//!
//! - **ASCII Art**: Console-based visualization for headless debugging
//! - **SVG Export**: Vector graphics for documentation and analysis
//! - **JSON Reports**: Machine-readable diagnostic data
//! - **HTML Dashboard**: Interactive web-based debugging interface
//!
//! # Examples
//!
//! ```rust
//! use tiles_tools::debug::*;
//! use tiles_tools::coordinates::hexagonal::{Coordinate, Axial, Pointy};
//!
//! // Create a debug grid renderer
//! let mut renderer = GridRenderer::new()
//!     .with_size(10, 8)
//!     .with_style(GridStyle::Hexagonal);
//!
//! // Add some debug markers
//! renderer.add_marker(Coordinate::<Axial, Pointy>::new(2, 3), "S", "Start position");
//! renderer.add_marker(Coordinate::<Axial, Pointy>::new(7, 5), "G", "Goal position");
//!
//! // Render as ASCII art
//! println!("{}", renderer.render_ascii());
//!
//! // Export as SVG
//! renderer.export_svg("debug_grid.svg").expect("Failed to export SVG");
//! ```

use std::collections::{HashMap, VecDeque};
use std::time::{Instant, Duration};
use std::fs::{File, create_dir_all};
use std::io::{Write, BufWriter};
use std::path::Path;
use crate::coordinates::{Distance, Neighbors};

/// Visual debugging renderer for coordinate grids.
pub struct GridRenderer
{
  width: usize,
  height: usize,
  style: GridStyle,
  markers: HashMap<(i32, i32), DebugMarker>,
  highlights: Vec<DebugHighlight>,
  annotations: Vec<DebugAnnotation>,
}

/// Style options for grid rendering.
#[ derive( Debug, Clone, Copy ) ]
pub enum GridStyle
{
  /// Square grid with 4-connected neighbors
  Square4,
  /// Square grid with 8-connected neighbors
  Square8,
  /// Hexagonal grid with pointy-top orientation
  Hexagonal,
  /// Triangular tessellation
  Triangular,
  /// Isometric projection
  Isometric,
}

/// Debug marker for highlighting specific coordinates.
#[ derive( Debug, Clone ) ]
pub struct DebugMarker
{
  /// Display symbol (single character)
  pub symbol: String,
  /// Tooltip description
  pub description: String,
  /// Display color (for colored output)
  pub color: DebugColor,
  /// Marker priority (higher priority shown on top)
  pub priority: u32,
}

/// Debug highlight for marking areas or paths.
#[derive(Debug, Clone)]
pub struct DebugHighlight {
  /// Coordinates to highlight
  pub coordinates: Vec<(i32, i32)>,
  /// Highlight style
  pub style: HighlightStyle,
  /// Color for the highlight
  pub color: DebugColor,
  /// Description of the highlight
  pub label: String,
}

/// Styles for highlighting areas.
#[derive(Debug, Clone, Copy)]
pub enum HighlightStyle {
  /// Outline the highlighted area
  Outline,
  /// Fill the highlighted area
  Fill,
  /// Show as dotted lines
  Dotted,
  /// Animated highlight (for interactive displays)
  Animated,
}

/// Debug annotation for adding text labels.
#[derive(Debug, Clone)]
pub struct DebugAnnotation {
  /// Position of the annotation
  pub position: (i32, i32),
  /// Text content
  pub text: String,
  /// Text color
  pub color: DebugColor,
  /// Offset from the coordinate center
  pub offset: (i32, i32),
}

/// Color options for debug rendering.
#[derive(Debug, Clone, Copy)]
pub enum DebugColor {
  /// Default color (usually white/black)
  Default,
  /// Red color (errors, obstacles)
  Red,
  /// Green color (valid paths, goals)
  Green,
  /// Blue color (water, special areas)
  Blue,
  /// Yellow color (warnings, temporary)
  Yellow,
  /// Purple color (special entities)
  Purple,
  /// Orange color (intermediate states)
  Orange,
  /// Gray color (disabled/inactive)
  Gray,
}

impl GridRenderer
{
  /// Creates a new grid renderer.
  pub fn new() -> Self
  {
    Self
    {
      width: 20,
      height: 15,
      style: GridStyle::Square4,
      markers: HashMap::new(),
      highlights: Vec::new(),
      annotations: Vec::new(),
    }
  }

  /// Sets the grid size.
  pub fn with_size(mut self, width: usize, height: usize) -> Self {
    self.width = width;
    self.height = height;
    self
  }

  /// Sets the grid style.
  pub fn with_style(mut self, style: GridStyle) -> Self {
    self.style = style;
    self
  }

  /// Adds a debug marker at the specified coordinate.
  pub fn add_marker<C>(&mut self, coord: C, symbol: &str, description: &str)
  where
    C: Into<(i32, i32)>,
  {
    let pos = coord.into();
    self.markers.insert(pos, DebugMarker {
      symbol: symbol.to_string(),
      description: description.to_string(),
      color: DebugColor::Default,
      priority: 1,
    });
  }

  /// Adds a colored marker with priority.
  pub fn add_colored_marker<C>(
    &mut self, 
    coord: C, 
    symbol: &str, 
    description: &str, 
    color: DebugColor,
    priority: u32
  )
  where
    C: Into<(i32, i32)>,
  {
    let pos = coord.into();
    self.markers.insert(pos, DebugMarker {
      symbol: symbol.to_string(),
      description: description.to_string(),
      color,
      priority,
    });
  }

  /// Adds a path highlight.
  pub fn add_path<C>(&mut self, path: Vec<C>, label: &str, color: DebugColor)
  where
    C: Into<(i32, i32)>,
  {
    let coordinates = path.into_iter().map(|c| c.into()).collect();
    self.highlights.push(DebugHighlight {
      coordinates,
      style: HighlightStyle::Outline,
      color,
      label: label.to_string(),
    });
  }

  /// Adds an area highlight.
  pub fn add_area<C>(&mut self, area: Vec<C>, label: &str, color: DebugColor, style: HighlightStyle)
  where
    C: Into<(i32, i32)>,
  {
    let coordinates = area.into_iter().map(|c| c.into()).collect();
    self.highlights.push(DebugHighlight {
      coordinates,
      style,
      color,
      label: label.to_string(),
    });
  }

  /// Adds a text annotation.
  pub fn add_annotation<C>(&mut self, coord: C, text: &str, color: DebugColor)
  where
    C: Into<(i32, i32)>,
  {
    let position = coord.into();
    self.annotations.push(DebugAnnotation {
      position,
      text: text.to_string(),
      color,
      offset: (0, 0),
    });
  }

  /// Renders the grid as ASCII art.
  pub fn render_ascii(&self) -> String {
    let mut output = String::new();
    
    // Add header with grid information
    output.push_str(&format!("Debug Grid ({} x {}) - Style: {:?}\n", 
      self.width, self.height, self.style));
    output.push_str(&"=".repeat(50));
    output.push('\n');

    match self.style {
      GridStyle::Square4 | GridStyle::Square8 => self.render_square_ascii(&mut output),
      GridStyle::Hexagonal => self.render_hexagonal_ascii(&mut output),
      GridStyle::Triangular => self.render_triangular_ascii(&mut output),
      GridStyle::Isometric => self.render_isometric_ascii(&mut output),
    }

    // Add legend
    if !self.markers.is_empty() {
      output.push('\n');
      output.push_str("Legend:\n");
      let mut markers: Vec<_> = self.markers.iter().collect();
      markers.sort_by_key(|(_, marker)| marker.priority);
      
      for ((x, y), marker) in markers {
        output.push_str(&format!("  {} ({}, {}) - {}\n", 
          marker.symbol, x, y, marker.description));
      }
    }

    output
  }

  fn render_square_ascii(&self, output: &mut String) {
    // Render square grid with coordinates and markers
    for y in 0..self.height as i32 {
      // Top border
      for _x in 0..self.width as i32 {
        output.push_str("+---");
      }
      output.push_str("+\n");

      // Cell content
      for x in 0..self.width as i32 {
        output.push('|');
        
        let coord = (x, y);
        if let Some(marker) = self.markers.get(&coord) {
          output.push_str(&format!(" {} ", marker.symbol));
        } else if self.is_highlighted(coord) {
          output.push_str(" # ");
        } else {
          output.push_str("   ");
        }
      }
      output.push_str("|\n");
    }

    // Bottom border
    for _ in 0..self.width {
      output.push_str("+---");
    }
    output.push_str("+\n");
  }

  fn render_hexagonal_ascii(&self, output: &mut String) {
    // Simplified hexagonal grid representation
    output.push_str("Hexagonal Grid (simplified ASCII representation):\n");
    
    for y in 0..self.height as i32 {
      // Add offset for hexagonal layout
      let offset = if y % 2 == 1 { "  " } else { "" };
      output.push_str(offset);

      for x in 0..self.width as i32 {
        let coord = (x, y);
        if let Some(marker) = self.markers.get(&coord) {
          output.push_str(&format!("/{}\\ ", marker.symbol));
        } else if self.is_highlighted(coord) {
          output.push_str("/#\\ ");
        } else {
          output.push_str("/·\\ ");
        }
      }
      output.push('\n');
    }
  }

  fn render_triangular_ascii(&self, output: &mut String) {
    output.push_str("Triangular Grid (ASCII approximation):\n");
    
    for y in 0..self.height as i32 {
      for x in 0..self.width as i32 {
        let coord = (x, y);
        if let Some(marker) = self.markers.get(&coord) {
          if (x + y) % 2 == 0 {
            output.push_str(&format!("â–²{} ", marker.symbol));
          } else {
            output.push_str(&format!("â–¼{} ", marker.symbol));
          }
        } else if self.is_highlighted(coord) {
          output.push_str(if (x + y) % 2 == 0 { "â–²# " } else { "â–¼# " });
        } else {
          output.push_str(if (x + y) % 2 == 0 { "â–²  " } else { "â–¼  " });
        }
      }
      output.push('\n');
    }
  }

  fn render_isometric_ascii(&self, output: &mut String) {
    output.push_str("Isometric Grid (ASCII approximation):\n");
    
    for y in 0..self.height as i32 {
      let indent = " ".repeat((self.height as i32 - y - 1) as usize);
      output.push_str(&indent);
      
      for x in 0..self.width as i32 {
        let coord = (x, y);
        if let Some(marker) = self.markers.get(&coord) {
          output.push_str(&format!("â—Š{} ", marker.symbol));
        } else if self.is_highlighted(coord) {
          output.push_str("â—Š# ");
        } else {
          output.push_str("â—Š  ");
        }
      }
      output.push('\n');
    }
  }

  fn is_highlighted(&self, coord: (i32, i32)) -> bool {
    self.highlights.iter().any(|highlight| highlight.coordinates.contains(&coord))
  }

  /// Exports the grid as SVG.
  pub fn export_svg<P: AsRef<Path>>(&self, path: P) -> Result<(), std::io::Error> {
    if let Some(parent) = path.as_ref().parent() {
      create_dir_all(parent)?;
    }

    let file = File::create(path)?;
    let mut writer = BufWriter::new(file);

    let cell_size = 30;
    let svg_width = self.width * cell_size + 100;
    let svg_height = self.height * cell_size + 100;

    // SVG header
    writeln!(writer, r#"<?xml version="1.0" encoding="UTF-8"?>"#)?;
    writeln!(writer, r#"<svg width="{}" height="{}" xmlns="http://www.w3.org/2000/svg">"#, 
      svg_width, svg_height)?;

    // Background
    writeln!(writer, r#"<rect width="100%" height="100%" fill="white"/>"#)?;

    // Grid lines
    self.render_svg_grid(&mut writer, cell_size)?;

    // Highlights
    self.render_svg_highlights(&mut writer, cell_size)?;

    // Markers
    self.render_svg_markers(&mut writer, cell_size)?;

    // Annotations
    self.render_svg_annotations(&mut writer, cell_size)?;

    // SVG footer
    writeln!(writer, "</svg>")?;
    writer.flush()?;

    Ok(())
  }

  fn render_svg_grid(&self, writer: &mut BufWriter<File>, cell_size: usize) -> Result<(), std::io::Error> {
    let offset = 50;
    
    match self.style {
      GridStyle::Square4 | GridStyle::Square8 => {
        // Vertical lines
        for x in 0..=self.width {
          let x_pos = offset + x * cell_size;
          writeln!(writer, r#"<line x1="{}" y1="{}" x2="{}" y2="{}" stroke="lightgray" stroke-width="1"/>"#,
            x_pos, offset, x_pos, offset + self.height * cell_size)?;
        }

        // Horizontal lines
        for y in 0..=self.height {
          let y_pos = offset + y * cell_size;
          writeln!(writer, r#"<line x1="{}" y1="{}" x2="{}" y2="{}" stroke="lightgray" stroke-width="1"/>"#,
            offset, y_pos, offset + self.width * cell_size, y_pos)?;
        }
      },
      GridStyle::Hexagonal => {
        // Simplified hexagonal grid (would need proper hexagon math for production)
        for y in 0..self.height {
          for x in 0..self.width {
            let x_offset = if y % 2 == 1 { cell_size / 2 } else { 0 };
            let center_x = offset + x * cell_size + x_offset + cell_size / 2;
            let center_y = offset + y * cell_size * 3 / 4 + cell_size / 2;
            
            writeln!(writer, r#"<polygon points="{},{} {},{} {},{} {},{} {},{} {},{}" fill="none" stroke="lightgray" stroke-width="1"/>"#,
              center_x, center_y - cell_size/3,
              center_x + cell_size/3, center_y - cell_size/6,
              center_x + cell_size/3, center_y + cell_size/6,
              center_x, center_y + cell_size/3,
              center_x - cell_size/3, center_y + cell_size/6,
              center_x - cell_size/3, center_y - cell_size/6)?;
          }
        }
      },
      _ => {
        // Default to square grid for other styles
        self.render_svg_grid(writer, cell_size)?;
      }
    }

    Ok(())
  }

  fn render_svg_highlights(&self, writer: &mut BufWriter<File>, cell_size: usize) -> Result<(), std::io::Error> {
    let offset = 50;
    
    for highlight in &self.highlights {
      let color = self.color_to_svg(highlight.color);
      
      for &(x, y) in &highlight.coordinates {
        if x >= 0 && x < self.width as i32 && y >= 0 && y < self.height as i32 {
          let x_pos = offset + x as usize * cell_size;
          let y_pos = offset + y as usize * cell_size;
          
          match highlight.style {
            HighlightStyle::Fill => {
              writeln!(writer, r#"<rect x="{}" y="{}" width="{}" height="{}" fill="{}" opacity="0.3"/>"#,
                x_pos, y_pos, cell_size, cell_size, color)?;
            },
            HighlightStyle::Outline => {
              writeln!(writer, r#"<rect x="{}" y="{}" width="{}" height="{}" fill="none" stroke="{}" stroke-width="2"/>"#,
                x_pos, y_pos, cell_size, cell_size, color)?;
            },
            HighlightStyle::Dotted => {
              writeln!(writer, r#"<rect x="{}" y="{}" width="{}" height="{}" fill="none" stroke="{}" stroke-width="2" stroke-dasharray="5,5"/>"#,
                x_pos, y_pos, cell_size, cell_size, color)?;
            },
            HighlightStyle::Animated => {
              writeln!(writer, r#"<rect x="{}" y="{}" width="{}" height="{}" fill="{}" opacity="0.5"><animate attributeName="opacity" values="0.2;0.8;0.2" dur="2s" repeatCount="indefinite"/></rect>"#,
                x_pos, y_pos, cell_size, cell_size, color)?;
            },
          }
        }
      }
    }

    Ok(())
  }

  fn render_svg_markers(&self, writer: &mut BufWriter<File>, cell_size: usize) -> Result<(), std::io::Error> {
    let offset = 50;
    
    for (&(x, y), marker) in &self.markers {
      if x >= 0 && x < self.width as i32 && y >= 0 && y < self.height as i32 {
        let x_pos = offset + x as usize * cell_size + cell_size / 2;
        let y_pos = offset + y as usize * cell_size + cell_size / 2 + 5; // Offset for text baseline
        let color = self.color_to_svg(marker.color);
        
        writeln!(writer, r#"<text x="{}" y="{}" text-anchor="middle" fill="{}" font-family="monospace" font-size="16" font-weight="bold">{}</text>"#,
          x_pos, y_pos, color, marker.symbol)?;
        
        // Add tooltip
        writeln!(writer, r#"<title>{}</title>"#, marker.description)?;
      }
    }

    Ok(())
  }

  fn render_svg_annotations(&self, writer: &mut BufWriter<File>, cell_size: usize) -> Result<(), std::io::Error> {
    let offset = 50;
    
    for annotation in &self.annotations {
      let (x, y) = annotation.position;
      if x >= 0 && x < self.width as i32 && y >= 0 && y < self.height as i32 {
        let x_pos = offset + x as usize * cell_size + cell_size / 2 + annotation.offset.0 as usize;
        let y_pos = offset + y as usize * cell_size + cell_size / 4 + annotation.offset.1 as usize;
        let color = self.color_to_svg(annotation.color);
        
        writeln!(writer, r#"<text x="{}" y="{}" fill="{}" font-family="sans-serif" font-size="12">{}</text>"#,
          x_pos, y_pos, color, annotation.text)?;
      }
    }

    Ok(())
  }

  fn color_to_svg(&self, color: DebugColor) -> &'static str {
    match color {
      DebugColor::Default => "black",
      DebugColor::Red => "red",
      DebugColor::Green => "green",
      DebugColor::Blue => "blue",
      DebugColor::Yellow => "orange",
      DebugColor::Purple => "purple",
      DebugColor::Orange => "darkorange",
      DebugColor::Gray => "gray",
    }
  }

  /// Clears all debug information.
  pub fn clear(&mut self) {
    self.markers.clear();
    self.highlights.clear();
    self.annotations.clear();
  }
}

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

/// Pathfinding debug visualizer.
pub struct PathfindingDebugger {
  grid_renderer: GridRenderer,
  path_costs: HashMap<(i32, i32), u32>,
  visited_nodes: Vec<(i32, i32)>,
  open_nodes: Vec<(i32, i32)>,
  obstacles: Vec<(i32, i32)>,
}

impl PathfindingDebugger {
  /// Creates a new pathfinding debugger.
  pub fn new(width: usize, height: usize) -> Self {
    Self {
      grid_renderer: GridRenderer::new().with_size(width, height),
      path_costs: HashMap::new(),
      visited_nodes: Vec::new(),
      open_nodes: Vec::new(),
      obstacles: Vec::new(),
    }
  }

  /// Adds an obstacle at the specified coordinate.
  pub fn add_obstacle<C>(&mut self, coord: C)
  where
    C: Into<(i32, i32)>,
  {
    let pos = coord.into();
    self.obstacles.push(pos);
    self.grid_renderer.add_colored_marker(pos, "X", "Obstacle", DebugColor::Red, 10);
  }

  /// Sets the start position.
  pub fn set_start<C>(&mut self, coord: C)
  where
    C: Into<(i32, i32)>,
  {
    let pos = coord.into();
    self.grid_renderer.add_colored_marker(pos, "S", "Start", DebugColor::Green, 20);
  }

  /// Sets the goal position.
  pub fn set_goal<C>(&mut self, coord: C)
  where
    C: Into<(i32, i32)>,
  {
    let pos = coord.into();
    self.grid_renderer.add_colored_marker(pos, "G", "Goal", DebugColor::Blue, 20);
  }

  /// Adds a path to visualize.
  pub fn add_path<C>(&mut self, path: Vec<C>, label: &str)
  where
    C: Into<(i32, i32)>,
  {
    let path_coords: Vec<(i32, i32)> = path.into_iter().map(|c| c.into()).collect();
    
    // Add path markers
    for (i, &coord) in path_coords.iter().enumerate() {
      if i > 0 && i < path_coords.len() - 1 {
        self.grid_renderer.add_colored_marker(coord, "·", "Path point", DebugColor::Yellow, 5);
      }
    }

    self.grid_renderer.add_path(path_coords, label, DebugColor::Yellow);
  }

  /// Adds visited nodes from pathfinding algorithm.
  pub fn add_visited_nodes<C>(&mut self, nodes: Vec<C>)
  where
    C: Into<(i32, i32)>,
  {
    self.visited_nodes = nodes.into_iter().map(|c| c.into()).collect();
    self.grid_renderer.add_area(self.visited_nodes.clone(), "Visited", DebugColor::Gray, HighlightStyle::Fill);
  }

  /// Adds open nodes from pathfinding algorithm.
  pub fn add_open_nodes<C>(&mut self, nodes: Vec<C>)
  where
    C: Into<(i32, i32)>,
  {
    self.open_nodes = nodes.into_iter().map(|c| c.into()).collect();
    self.grid_renderer.add_area(self.open_nodes.clone(), "Open", DebugColor::Orange, HighlightStyle::Dotted);
  }

  /// Sets cost information for pathfinding visualization.
  pub fn set_costs(&mut self, costs: HashMap<(i32, i32), u32>) {
    self.path_costs = costs;
    
    // Add cost annotations
    for (&coord, &cost) in &self.path_costs {
      if cost > 1 && !self.obstacles.contains(&coord) {
        self.grid_renderer.add_annotation(coord, &cost.to_string(), DebugColor::Purple);
      }
    }
  }

  /// Renders the pathfinding debug view as ASCII.
  pub fn render_ascii(&self) -> String {
    self.grid_renderer.render_ascii()
  }

  /// Exports the pathfinding debug view as SVG.
  pub fn export_svg<P: AsRef<Path>>(&self, path: P) -> Result<(), std::io::Error> {
    self.grid_renderer.export_svg(path)
  }
}

/// ECS component inspector for debugging entity data.
pub struct ECSInspector {
  entity_data: HashMap<u32, EntityDebugInfo>,
  component_counts: HashMap<String, usize>,
  system_timings: HashMap<String, Duration>,
}

/// Debug information for a single entity.
#[derive(Debug, Clone)]
pub struct EntityDebugInfo {
  /// Entity ID
  pub id: u32,
  /// Component type names
  pub components: Vec<String>,
  /// Current position (if applicable)
  pub position: Option<(i32, i32)>,
  /// Custom debug data
  pub data: HashMap<String, String>,
}

impl ECSInspector {
  /// Creates a new ECS inspector.
  pub fn new() -> Self {
    Self {
      entity_data: HashMap::new(),
      component_counts: HashMap::new(),
      system_timings: HashMap::new(),
    }
  }

  /// Records entity information.
  pub fn record_entity(&mut self, entity: EntityDebugInfo) {
    for component in &entity.components {
      *self.component_counts.entry(component.clone()).or_insert(0) += 1;
    }
    self.entity_data.insert(entity.id, entity);
  }

  /// Records system execution time.
  pub fn record_system_timing(&mut self, system_name: String, duration: Duration) {
    self.system_timings.insert(system_name, duration);
  }

  /// Gets the number of entities currently tracked.
  pub fn entity_count(&self) -> usize {
    self.entity_data.len()
  }

  /// Gets entity information by ID.
  pub fn get_entity(&self, id: u32) -> Option<&EntityDebugInfo> {
    self.entity_data.get(&id)
  }

  /// Gets all entity IDs.
  pub fn entity_ids(&self) -> Vec<u32> {
    self.entity_data.keys().copied().collect()
  }

  /// Generates a debug report.
  pub fn generate_report(&self) -> String {
    let mut report = String::new();
    
    report.push_str("ECS Inspector Report\n");
    report.push_str("===================\n\n");

    // Entity summary
    report.push_str(&format!("Total Entities: {}\n", self.entity_data.len()));
    
    // Component statistics
    report.push_str("\nComponent Statistics:\n");
    let mut components: Vec<_> = self.component_counts.iter().collect();
    components.sort_by_key(|(_, count)| *count);
    for (component, count) in components.iter().rev() {
      report.push_str(&format!("  {}: {} entities\n", component, count));
    }

    // System timings
    if !self.system_timings.is_empty() {
      report.push_str("\nSystem Performance:\n");
      let mut timings: Vec<_> = self.system_timings.iter().collect();
      timings.sort_by_key(|(_, duration)| *duration);
      for (system, duration) in timings.iter().rev() {
        report.push_str(&format!("  {}: {:.2}ms\n", system, duration.as_secs_f64() * 1000.0));
      }
    }

    // Detailed entity information
    report.push_str("\nDetailed Entity Information:\n");
    let mut entities: Vec<_> = self.entity_data.values().collect();
    entities.sort_by_key(|e| e.id);
    
    for entity in entities.iter().take(10) { // Limit to first 10 for readability
      report.push_str(&format!("\nEntity {}:\n", entity.id));
      report.push_str(&format!("  Components: {}\n", entity.components.join(", ")));
      if let Some(pos) = entity.position {
        report.push_str(&format!("  Position: ({}, {})\n", pos.0, pos.1));
      }
      for (key, value) in &entity.data {
        report.push_str(&format!("  {}: {}\n", key, value));
      }
    }

    if self.entity_data.len() > 10 {
      report.push_str(&format!("\n... and {} more entities\n", self.entity_data.len() - 10));
    }

    report
  }

  /// Exports entity data as JSON.
  pub fn export_json(&self) -> String {
    // Simplified JSON export (in real implementation would use serde_json)
    let mut json = String::from("{\n");
    json.push_str(&format!("  \"total_entities\": {},\n", self.entity_data.len()));
    
    json.push_str("  \"component_counts\": {\n");
    let component_entries: Vec<String> = self.component_counts.iter()
      .map(|(name, count)| format!("    \"{}\": {}", name, count))
      .collect();
    json.push_str(&component_entries.join(",\n"));
    json.push_str("\n  },\n");
    
    json.push_str("  \"system_timings\": {\n");
    let timing_entries: Vec<String> = self.system_timings.iter()
      .map(|(name, duration)| format!("    \"{}\": {:.2}", name, duration.as_secs_f64() * 1000.0))
      .collect();
    json.push_str(&timing_entries.join(",\n"));
    json.push_str("\n  }\n");
    
    json.push('}');
    json
  }
}

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

/// Performance profiler for tracking frame times and bottlenecks.
pub struct PerformanceProfiler {
  frame_times: VecDeque<Duration>,
  system_times: HashMap<String, VecDeque<Duration>>,
  memory_samples: VecDeque<MemorySample>,
  start_time: Instant,
  frame_count: u64,
}

/// Memory usage sample.
#[derive(Debug, Clone, Copy)]
pub struct MemorySample {
  /// Timestamp of the sample
  pub timestamp: Duration,
  /// Estimated memory usage in bytes
  pub memory_usage: u64,
  /// Number of active entities
  pub entity_count: u32,
}

impl PerformanceProfiler {
  /// Creates a new performance profiler.
  pub fn new() -> Self {
    Self {
      frame_times: VecDeque::with_capacity(1000),
      system_times: HashMap::new(),
      memory_samples: VecDeque::with_capacity(1000),
      start_time: Instant::now(),
      frame_count: 0,
    }
  }

  /// Records a frame time.
  pub fn record_frame_time(&mut self, duration: Duration) {
    self.frame_times.push_back(duration);
    if self.frame_times.len() > 1000 {
      self.frame_times.pop_front();
    }
    self.frame_count += 1;
  }

  /// Records system execution time.
  pub fn record_system_time(&mut self, system_name: String, duration: Duration) {
    let times = self.system_times.entry(system_name).or_insert_with(|| VecDeque::with_capacity(100));
    times.push_back(duration);
    if times.len() > 100 {
      times.pop_front();
    }
  }

  /// Records memory usage sample.
  pub fn record_memory_sample(&mut self, memory_usage: u64, entity_count: u32) {
    let sample = MemorySample {
      timestamp: self.start_time.elapsed(),
      memory_usage,
      entity_count,
    };
    self.memory_samples.push_back(sample);
    if self.memory_samples.len() > 1000 {
      self.memory_samples.pop_front();
    }
  }

  /// Gets current performance statistics.
  pub fn get_stats(&self) -> PerformanceStats {
    let avg_frame_time = if !self.frame_times.is_empty() {
      self.frame_times.iter().sum::<Duration>() / self.frame_times.len() as u32
    } else {
      Duration::ZERO
    };

    let min_frame_time = self.frame_times.iter().min().copied().unwrap_or(Duration::ZERO);
    let max_frame_time = self.frame_times.iter().max().copied().unwrap_or(Duration::ZERO);

    let fps = if avg_frame_time.as_secs_f64() > 0.0 {
      1.0 / avg_frame_time.as_secs_f64()
    } else {
      0.0
    };

    let current_memory = self.memory_samples.back().map(|s| s.memory_usage).unwrap_or(0);
    let current_entities = self.memory_samples.back().map(|s| s.entity_count).unwrap_or(0);

    PerformanceStats {
      avg_frame_time,
      min_frame_time,
      max_frame_time,
      fps,
      frame_count: self.frame_count,
      current_memory,
      current_entities,
      uptime: self.start_time.elapsed(),
    }
  }

  /// Generates a performance report.
  pub fn generate_report(&self) -> String {
    let stats = self.get_stats();
    let mut report = String::new();

    report.push_str("Performance Profile Report\n");
    report.push_str("=========================\n\n");

    report.push_str(&format!("Uptime: {:.1}s\n", stats.uptime.as_secs_f64()));
    report.push_str(&format!("Frame Count: {}\n", stats.frame_count));
    report.push_str(&format!("Average FPS: {:.1}\n", stats.fps));
    report.push_str(&format!("Frame Time: {:.2}ms (avg), {:.2}ms (min), {:.2}ms (max)\n",
      stats.avg_frame_time.as_secs_f64() * 1000.0,
      stats.min_frame_time.as_secs_f64() * 1000.0,
      stats.max_frame_time.as_secs_f64() * 1000.0));
    
    report.push_str(&format!("Memory Usage: {} KB\n", stats.current_memory / 1024));
    report.push_str(&format!("Active Entities: {}\n", stats.current_entities));

    if !self.system_times.is_empty() {
      report.push_str("\nSystem Performance:\n");
      for (system, times) in &self.system_times {
        if !times.is_empty() {
          let avg = times.iter().sum::<Duration>() / times.len() as u32;
          let max = times.iter().max().copied().unwrap_or(Duration::ZERO);
          report.push_str(&format!("  {}: {:.2}ms avg, {:.2}ms max\n",
            system, 
            avg.as_secs_f64() * 1000.0,
            max.as_secs_f64() * 1000.0));
        }
      }
    }

    report
  }

  /// Exports performance data as CSV for analysis.
  pub fn export_csv<P: AsRef<Path>>(&self, path: P) -> Result<(), std::io::Error> {
    let file = File::create(path)?;
    let mut writer = BufWriter::new(file);

    // Header
    writeln!(writer, "timestamp_ms,frame_time_ms,memory_kb,entity_count")?;

    // Data
    for (i, frame_time) in self.frame_times.iter().enumerate() {
      let timestamp_ms = i as f64 * 16.67; // Approximate 60 FPS timing
      let memory_sample = self.memory_samples.get(i).copied().unwrap_or(MemorySample {
        timestamp: Duration::from_millis(timestamp_ms as u64),
        memory_usage: 0,
        entity_count: 0,
      });
      
      writeln!(writer, "{:.2},{:.2},{},{}", 
        timestamp_ms,
        frame_time.as_secs_f64() * 1000.0,
        memory_sample.memory_usage / 1024,
        memory_sample.entity_count)?;
    }

    writer.flush()?;
    Ok(())
  }
}

/// Performance statistics snapshot.
#[derive(Debug, Clone)]
pub struct PerformanceStats {
  /// Average frame time
  pub avg_frame_time: Duration,
  /// Minimum frame time recorded
  pub min_frame_time: Duration,
  /// Maximum frame time recorded
  pub max_frame_time: Duration,
  /// Current frames per second
  pub fps: f64,
  /// Total frame count
  pub frame_count: u64,
  /// Current memory usage in bytes
  pub current_memory: u64,
  /// Current number of entities
  pub current_entities: u32,
  /// Total uptime
  pub uptime: Duration,
}

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

/// Coordinate conversion trait for debug rendering.
pub trait IntoDebugCoord {
  /// Converts the coordinate to a debug-friendly (i32, i32) tuple.
  fn into_debug_coord(self) -> (i32, i32);
}

// Implement for common coordinate types
impl IntoDebugCoord for (i32, i32) {
  fn into_debug_coord(self) -> (i32, i32) {
    self
  }
}

impl IntoDebugCoord for (f32, f32) {
  fn into_debug_coord(self) -> (i32, i32) {
    (self.0 as i32, self.1 as i32)
  }
}

impl IntoDebugCoord for (usize, usize) {
  fn into_debug_coord(self) -> (i32, i32) {
    (self.0 as i32, self.1 as i32)
  }
}

/// Utility functions for debugging.
pub mod utils {
  use super::*;

  /// Creates a simple ASCII art representation of a 2D boolean array.
  pub fn render_bool_grid(grid: &[Vec<bool>], true_char: char, false_char: char) -> String {
    let mut output = String::new();
    for row in grid {
      for &cell in row {
        output.push(if cell { true_char } else { false_char });
        output.push(' ');
      }
      output.push('\n');
    }
    output
  }

  /// Formats a duration for human-readable display.
  pub fn format_duration(duration: Duration) -> String {
    let micros = duration.as_micros();
    if micros < 1000 {
      format!("{}μs", micros)
    } else if micros < 1_000_000 {
      format!("{:.1}ms", duration.as_secs_f64() * 1000.0)
    } else {
      format!("{:.2}s", duration.as_secs_f64())
    }
  }

  /// Formats memory usage for human-readable display.
  pub fn format_memory(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

    if bytes >= GB {
      format!("{:.2} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
      format!("{:.2} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
      format!("{:.1} KB", bytes as f64 / KB as f64)
    } else {
      format!("{} B", bytes)
    }
  }
}

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

  #[test]
  fn test_grid_renderer_creation() {
    let renderer = GridRenderer::new()
      .with_size(10, 8)
      .with_style(GridStyle::Hexagonal);
    
    assert_eq!(renderer.width, 10);
    assert_eq!(renderer.height, 8);
    assert!(matches!(renderer.style, GridStyle::Hexagonal));
  }

  #[test]
  fn test_grid_renderer_markers() {
    let mut renderer = GridRenderer::new();
    renderer.add_marker((5, 3), "S", "Start position");
    renderer.add_colored_marker((8, 6), "G", "Goal", DebugColor::Blue, 10);
    
    assert_eq!(renderer.markers.len(), 2);
    assert!(renderer.markers.contains_key(&(5, 3)));
    assert!(renderer.markers.contains_key(&(8, 6)));
  }

  #[test]
  fn test_pathfinding_debugger() {
    let mut debugger = PathfindingDebugger::new(10, 10);
    
    debugger.set_start((0, 0));
    debugger.set_goal((9, 9));
    debugger.add_obstacle((5, 5));
    debugger.add_path(vec![(0, 0), (1, 1), (2, 2), (3, 3)], "Test Path");
    
    let output = debugger.render_ascii();
    assert!(output.contains("Start"));
    assert!(output.contains("Goal"));
    assert!(output.contains("Obstacle"));
  }

  #[test]
  fn test_ecs_inspector() {
    let mut inspector = ECSInspector::new();
    
    let entity = EntityDebugInfo {
      id: 42,
      components: vec!["Position".to_string(), "Health".to_string()],
      position: Some((10, 20)),
      data: vec![("level".to_string(), "5".to_string())].into_iter().collect(),
    };
    
    inspector.record_entity(entity);
    inspector.record_system_timing("MovementSystem".to_string(), Duration::from_millis(5));
    
    let report = inspector.generate_report();
    assert!(report.contains("Entity 42"));
    assert!(report.contains("Position"));
    assert!(report.contains("MovementSystem"));
  }

  #[test]
  fn test_performance_profiler() {
    let mut profiler = PerformanceProfiler::new();
    
    profiler.record_frame_time(Duration::from_millis(16));
    profiler.record_frame_time(Duration::from_millis(18));
    profiler.record_system_time("RenderSystem".to_string(), Duration::from_millis(8));
    profiler.record_memory_sample(1024 * 1024, 100); // 1MB, 100 entities
    
    let stats = profiler.get_stats();
    assert_eq!(stats.frame_count, 2);
    assert!(stats.fps > 0.0);
    assert_eq!(stats.current_memory, 1024 * 1024);
    assert_eq!(stats.current_entities, 100);
  }

  #[test]
  fn test_debug_utilities() {
    let grid = vec![
      vec![true, false, true],
      vec![false, true, false],
      vec![true, true, false],
    ];
    
    let output = utils::render_bool_grid(&grid, '#', '.');
    assert!(output.contains('#'));
    assert!(output.contains('.'));
    
    let duration = Duration::from_micros(1500);
    let formatted = utils::format_duration(duration);
    assert!(formatted.contains("1.5ms"));
    
    let memory = utils::format_memory(1536 * 1024); // 1.5 MB
    assert!(memory.contains("1.5") && memory.contains("MB"));
  }

  #[test]
  fn test_coordinate_conversion() {
    let int_coord = (5, 10);
    let float_coord = (5.7, 10.3);
    let usize_coord = (5usize, 10usize);
    
    assert_eq!(int_coord.into_debug_coord(), (5, 10));
    assert_eq!(float_coord.into_debug_coord(), (5, 10));
    assert_eq!(usize_coord.into_debug_coord(), (5, 10));
  }
}