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
//! implementation of blob's gene and builder that can build blob base on an given genotype

use std::f32::consts::PI;
use std::fmt::{self, Debug};

use bevy::prelude::*;
use rand::prelude::*;
use serde::{Serialize, Deserialize};

use crate::blob::block::NeuronId;
use crate::brain::neuron::GenericNN;
use crate::consts::*;

use super::blob_builder::BlobBuilder;
use super::block::PhysiBlockBundle;

/// Generate Blob according to Genotype
/// Wrapper around BlobBuilder
pub struct GenoBlobBuilder<'a> {
    builder: BlobBuilder<'a>,
}

impl<'a> GenoBlobBuilder<'a> {
    pub fn from_commands(commands: Commands<'a, 'a>, nnvec: &'a mut Vec<GenericNN>) -> Self {
        Self {
            builder: BlobBuilder::from_commands(commands, nnvec),
        }
    }

    /// generate blob according to its genotype
    pub fn build(&mut self, geno: &mut BlobGeno, center: [f32; 2]) {

        // create first
        let builder = &mut self.builder;

        if let Some(nn_id) = geno.get_first().unwrap().nn_id {
            // if the geno is exported geno or mutated geno (with nn inside)
            let root_nn = NeuronId::new(nn_id,None);
            
            builder.create_first(
                geno.get_first()
                .unwrap()
                .to_bundle(center),
                root_nn);
            
            // start recursion
            build_node_with_nn(builder, &mut geno.vec_tree, 0, nn_id)

        } else {
            // if the geno is new rand geno (without nn inside)
            geno.assign_nn_id_to_root(
                builder.create_first(
                geno.get_first()
                    .unwrap()
                    .to_bundle(center)
                    .with_color(Color::BLUE),
                (),).unwrap()
            );

            // start recursion
            build_node(&mut self.builder, &mut geno.vec_tree, 0);
        }

        // save geno to blob
        self.builder.update_geno(geno.clone());

        // reset builder
        self.builder.clean();
    }
}

// Lambda function to use in child extraction
fn lambda(node: &mut Option<GenericGenoNode>) -> Option<&mut GenoNode> {
    node.as_mut().and_then(|node| match node {
        GenericGenoNode::Parent => None,
        GenericGenoNode::Child(child) => Some(child),
    })
}

fn build_node(
    builder: &mut BlobBuilder, 
    tree: &mut QuadTree<GenericGenoNode>, 
    index: usize, 
) {
    if let Some(Some(_)) = tree.nodes.get_mut(index) {
        let children = tree.children(index);
        // let (top_child, bottom_child, left_child, right_child) = (
        //     tree.nodes.get(children[0]).and_then(lambda),
        //     tree.nodes.get(children[1]).and_then(lambda),
        //     tree.nodes.get(children[2]).and_then(lambda),
        //     tree.nodes.get(children[3]).and_then(lambda),
        // );

        // top
        if let Some(mut node) = tree.nodes.get_mut(children[0]).and_then(lambda) {

            let nn_id = builder.add_to_top(
                node.size[0],
                node.size[1],
                None,
                Some(node.joint_limits),
                (),
            );

            // don't overwrite nn_id if it is not None
            // which means they have already had bounded NN
            if node.nn_id.is_none() {
                node.nn_id = nn_id
            }
            
            build_node(builder, tree, children[0]);
            builder.bottom();
        }

        // bottom
        if let Some(mut node) = tree.nodes.get_mut(children[1]).and_then(lambda) {
            let nn_id = builder.add_to_bottom(
                node.size[0],
                node.size[1],
                None,
                Some(node.joint_limits),
                (),
            );

            if node.nn_id.is_none() {
                node.nn_id = nn_id
            }

            build_node(builder, tree, children[1]);
            builder.top();
        }

        // left
        if let Some(node) = tree.nodes.get_mut(children[2]).and_then(lambda) {
            let nn_id = builder.add_to_left(
                node.size[0],
                node.size[1],
                None,
                Some(node.joint_limits),
                (),
            );

            if node.nn_id.is_none() {
                node.nn_id = nn_id
            }

            build_node(builder, tree, children[2]);
            builder.right();
        }

        // right
        if let Some(node) = tree.nodes.get_mut(children[3]).and_then(lambda) {
            let nn_id = builder.add_to_right(
                node.size[0],
                node.size[1],
                None,
                Some(node.joint_limits),
                (),
            );

            if node.nn_id.is_none() {
                node.nn_id = nn_id
            }

            build_node(builder, tree, children[3]);
            builder.left();
        }
    }
}


/// build node that inherit `NeuronId` from geno
fn build_node_with_nn(
    builder: &mut BlobBuilder, 
    tree: &mut QuadTree<GenericGenoNode>, 
    index: usize,
    parent_nn_id: usize
) {
    if let Some(Some(_)) = tree.nodes.get_mut(index) {
        let children = tree.children(index);

        // top
        if let Some(node) = tree.nodes.get_mut(children[0]).and_then(lambda) {

            let nn_id = node.nn_id.unwrap();
            let neuron_id = NeuronId::new(nn_id,Some(parent_nn_id));

            builder.add_to_top(
                node.size[0],
                node.size[1],
                None,
                Some(node.joint_limits),
                neuron_id,
            );
            
            build_node_with_nn(builder, tree, children[0],nn_id);
            builder.bottom();
        }

        // bottom
        if let Some(node) = tree.nodes.get_mut(children[1]).and_then(lambda) {
            let nn_id = node.nn_id.unwrap();
            let neuron_id = NeuronId::new(nn_id,Some(parent_nn_id));
            
            builder.add_to_bottom(
                node.size[0],
                node.size[1],
                None,
                Some(node.joint_limits),
                neuron_id,
            );

            build_node_with_nn(builder, tree, children[1],nn_id);
            builder.top();
        }

        // left
        if let Some(node) = tree.nodes.get_mut(children[2]).and_then(lambda) {
            let nn_id = node.nn_id.unwrap();
            let neuron_id = NeuronId::new(nn_id,Some(parent_nn_id));

            builder.add_to_left(
                node.size[0],
                node.size[1],
                None,
                Some(node.joint_limits),
                neuron_id,
            );

            build_node_with_nn(builder, tree, children[2], nn_id);
            builder.right();
        }

        // right
        if let Some(node) = tree.nodes.get_mut(children[3]).and_then(lambda) {
            let nn_id = node.nn_id.unwrap();
            let neuron_id = NeuronId::new(nn_id,Some(parent_nn_id));

            builder.add_to_right(
                node.size[0],
                node.size[1],
                None,
                Some(node.joint_limits),
                neuron_id,
            );

            build_node_with_nn(builder, tree, children[3], nn_id);
            builder.left();
        }
    }
}

/// The Geno for morphyology of the blob.
/// 
/// The Geno is a QuadTree (it can be represented as TernaryTree as well).
/// 
/// index 0,1,2,3 means up,down,left,right (one of them can be ParentIndicator)
#[derive(Debug, Component, Clone, Serialize, Deserialize)]
pub struct BlobGeno {
    pub vec_tree: QuadTree<GenericGenoNode>,
}

impl Default for BlobGeno {
    fn default() -> Self {
        Self {
            vec_tree: QuadTree::<GenericGenoNode>::new(GENO_MAX_DEPTH),
        }
    }
}

impl BlobGeno {
    // TODO: Clean the code. Ugly long function
    /// generate a random GenoType that don't have conflict limbs
    pub fn new_rand() -> BlobGeno {
        // prevent tree-structural block conflict
        let mut occupied_region = Vec::<[f32; 4]>::new();

        fn is_overlapped(
            center: [f32; 2],
            size: [f32; 2],
            occupied_region: &mut Vec<[f32; 4]>,
        ) -> bool {
            let x_min = center[0] - size[0];
            let x_max = center[0] + size[0];
            let y_min = center[1] - size[1];
            let y_max = center[1] + size[1];

            for region in occupied_region.iter() {
                let x_overlap = x_min <= region[1] && x_max >= region[0];
                let y_overlap = y_min <= region[3] && y_max >= region[2];
                if x_overlap && y_overlap {
                    occupied_region.push([x_min, x_max, y_min, y_max]);
                    return true;
                }
            }
            occupied_region.push([x_min, x_max, y_min, y_max]);
            return false;
        }

        /// function to acquire a new rand node
        fn rand_nodes(
            parent: &GenoNode,
            direction: usize,
            occupied_region: &mut Vec<[f32; 4]>,
        ) -> Option<GenericGenoNode> {
            let mut rng = thread_rng();

            let parent_size = parent.size;
            let parent_center = parent.center;

            // set limitation
            // limitation can only avoid block conflict
            // it can not avoid conflict caused by tree structure
            let dx_dy_limits_top_bottom =
                [parent_size[0], DEFAULT_BLOCK_SIZE[0] * RAND_SIZE_SCALER[1]];
            let dx_dy_limits_left_right =
                [DEFAULT_BLOCK_SIZE[0] * RAND_SIZE_SCALER[1], parent_size[1]];

            if rng.gen_bool(RAND_NODE_NOT_NONE) {
                let joint_limits = [rng.gen_range(-PI * 0.9..0.0), rng.gen_range(0.0..PI * 0.9)];
                let mut size = [
                    rng.gen_range(
                        RAND_SIZE_SCALER[0] * DEFAULT_BLOCK_SIZE[0]..dx_dy_limits_top_bottom[0],
                    ),
                    rng.gen_range(
                        RAND_SIZE_SCALER[0] * DEFAULT_BLOCK_SIZE[1]..dx_dy_limits_top_bottom[1],
                    ),
                ];
                if direction == 2 || direction == 3 {
                    size = [
                        rng.gen_range(
                            RAND_SIZE_SCALER[0] * DEFAULT_BLOCK_SIZE[0]..dx_dy_limits_left_right[0],
                        ),
                        rng.gen_range(
                            RAND_SIZE_SCALER[0] * DEFAULT_BLOCK_SIZE[1]..dx_dy_limits_left_right[1],
                        ),
                    ];
                }

                // center
                let mut center = [
                    parent_center[0],
                    parent_center[1] + parent_size[1] + size[1],
                ];
                if direction == 1 {
                    center = [
                        parent_center[0],
                        parent_center[1] - parent_size[1] - size[1],
                    ];
                } else if direction == 2 {
                    center = [
                        parent_center[0] - parent_size[0] - size[0],
                        parent_center[1],
                    ];
                } else if direction == 3 {
                    center = [
                        parent_center[0] + parent_size[0] + size[0],
                        parent_center[1],
                    ]
                }
                if is_overlapped(center, size, occupied_region) {
                    return None;
                } else {
                    return Some(GenericGenoNode::Child(GenoNode {
                        joint_limits,
                        size,
                        center,
                        nn_id: None
                    }));
                }
            };
            return None;
        }

        /// recursive function
        fn build(
            tree: &mut QuadTree<GenericGenoNode>,
            index: usize,
            occupied_region: &mut Vec<[f32; 4]>,
        ) {
            let mut rng = thread_rng();

            let children = tree.children(index);

            // index and children index should in range
            if tree.nodes.get(children[3]).is_none() {
                return;
            }

            // random init four nodes, avoid self-conflict
            if let Some(GenericGenoNode::Child(node)) = tree.nodes[index].clone() {
                for (i, &child) in children.iter().enumerate() {
                    tree.nodes[child] = rand_nodes(&node, i, occupied_region)
                }

                // one parent indicator
                let parent_idx = *children.choose(&mut rng).unwrap();
                tree.nodes[parent_idx] = Some(GenericGenoNode::Parent);

                // keep recursion
                for &i in children.iter() {
                    if i != parent_idx {
                        build(tree, i, occupied_region);
                    }
                }
            }
        }

        // init tree
        let mut bg = BlobGeno::default();
        // root node
        bg.vec_tree.nodes[0] = Some(GenericGenoNode::Child(GenoNode::default()));
        build(&mut bg.vec_tree, 0, &mut occupied_region);
        bg
    }

    /// get the first GenoNode
    pub fn get_first(&self) -> Option<&GenoNode> {
        self.vec_tree.nodes[0].as_ref().and_then(|node| match node {
            GenericGenoNode::Parent => None,
            GenericGenoNode::Child(child) => Some(child),
        })
    }

    /// checker function to check the genotype is valid or not.
    /// 
    /// Not valid means self-conflit limbs
    pub fn is_valid(&self) -> bool {

        fn is_overlapped(
            center: [f32; 2],
            size: [f32; 2],
            occupied_region: &mut Vec<[f32; 4]>,
        ) -> bool {
            let x_min = center[0] - size[0];
            let x_max = center[0] + size[0];
            let y_min = center[1] - size[1];
            let y_max = center[1] + size[1];

            // println!("{},{},{},{}",x_min,x_max,y_min,y_max);

            for region in occupied_region.iter() {
                let x_overlap = x_min < region[1] - POSITION_EPSILON && x_max - POSITION_EPSILON > region[0];
                let y_overlap = y_min < region[3] - POSITION_EPSILON && y_max - POSITION_EPSILON > region[2];
                if x_overlap && y_overlap {
                    occupied_region.push([x_min, x_max, y_min, y_max]);
                    return true;
                }
            }
            occupied_region.push([x_min, x_max, y_min, y_max]);
            return false;
        }

        /// recursively add to `occupied_region`
        fn check (
            tree: &QuadTree<GenericGenoNode>,
            mut occupied_region: &mut Vec<[f32; 4]>,
            idx: usize
        ) -> bool {
            // println!("is_valid checking {}", idx);
            // println!("occupied_region {:?}", occupied_region);
            if let Some(Some(GenericGenoNode::Child(cur))) = tree.nodes.get(idx) {
                if !is_overlapped(cur.center, cur.size, &mut occupied_region) {
                    tree.children(idx).iter().all(|&i| check(tree, occupied_region, i))
                } else {
                    // println!("not valid {}", idx);
                    false
                }
            } else {
                true
            }
        }

        let mut occupied_region: Vec<[f32; 4]> = Vec::new();
        check(&self.vec_tree, &mut occupied_region, 0)

    }


    /// all nodes don't have child, used for mutate to lose limb
    /// 
    /// can not return root, can not return parent indicator
    pub fn leaf_nodes(&self) -> Vec<usize> {
        let mut result = Vec::new();
        for i in 1..self.vec_tree.nodes.len() {
            if let Some(GenericGenoNode::Parent) = self.vec_tree.nodes[i] {
                continue; // Skip if the node is of type GenericGenoNode::Parent
            }
            if self.vec_tree.nodes[i].is_some() && self.vec_tree.children(i).iter().all(
                |&child_idx| 
                child_idx >= self.vec_tree.nodes.len() || 
                self.vec_tree.nodes[child_idx].is_none() || 
                matches!(
                    self.vec_tree.nodes[child_idx], 
                    Some(GenericGenoNode::Parent)
                )
            ) {
                result.push(i);
            }
        }
        result
    }

    /// assign an nn_id to root (sometimes builder don't need new random geno)
    pub fn assign_nn_id_to_root(&mut self, id: usize) {
        if let Some(Some(GenericGenoNode::Child(node))) = self.vec_tree.nodes.get_mut(0) {
            if node.nn_id.is_none() {
                node.nn_id = Some(id);
            }
        } else {
            panic!()
        }
    }

    /// get reference for all nn_id, in usize rather than `Option<usize>`
    pub fn all_usize_nn_ids(&self) -> Vec<usize> {
        self.vec_tree.nodes.iter()
            .filter_map(|node_option|{
                match node_option {
                    Some(GenericGenoNode::Child(node)) => Some(node.nn_id.unwrap()),
                    _ => None
                }
            })
            .collect()
    }

    /// get mut reference for all nn_id in the geno
    pub fn all_nn_ids_mut(&mut self) -> Vec<&mut Option<usize>> {
        self.vec_tree.nodes.iter_mut()
            .filter_map(|node_option| {
                match node_option {
                    Some(GenericGenoNode::Child(child_node)) => Some(&mut child_node.nn_id),
                    _ => None,
                }
            })
            .collect()
    }

    pub fn all_nn_ids_indices(&self) -> Vec<usize> {
        self.vec_tree.nodes.iter().enumerate()
            .filter_map(|(idx, node_option)| {
                match node_option {
                    Some(GenericGenoNode::Child(_)) => Some(idx),
                    _ => None,
                }
            })
            .collect()
    }

    /// push all subtrees outside if the root block's size changed
    pub fn move_subtree_nodes_root(&mut self, old_size: [f32;2], new_size: [f32;2]) {
        let root_index: usize = 0;
        let xmove = new_size[0]-old_size[0];
        let ymove = new_size[1]-old_size[1];

        let children = self.vec_tree.children(root_index);
        for (direction, subtree_root_index) in children.iter().enumerate() {
            let subtree_indices: Vec<usize> = self.vec_tree.subtree_indices(*subtree_root_index);
            match direction {
                0 => {
                    for &i in &subtree_indices {
                        if let Some(Some(genericnode)) = self.vec_tree.nodes.get_mut(i) {
                            if let GenericGenoNode::Child(node) = genericnode{
                                node.center[1] += ymove;
                            }
                        } else {
                            panic!()
                        }
                    }
                },
                1 => {
                    for &i in &subtree_indices {
                        if let Some(Some(genericnode)) = self.vec_tree.nodes.get_mut(i) {
                            if let GenericGenoNode::Child(node) = genericnode{
                                node.center[1] -= ymove;
                            }
                        } else {
                            panic!()
                        }
                    }
                },
                2 => {
                    for &i in &subtree_indices {
                        if let Some(Some(genericnode)) = self.vec_tree.nodes.get_mut(i) {
                            if let GenericGenoNode::Child(node) = genericnode{
                                node.center[1] -= xmove;
                            }
                        } else {
                            panic!()
                        }
                    }
                },
                3 => {
                    for &i in &subtree_indices {
                        if let Some(Some(genericnode)) = self.vec_tree.nodes.get_mut(i) {
                            if let GenericGenoNode::Child(node) = genericnode{
                                node.center[1] += xmove;
                            }
                        } else {
                            panic!()
                        }
                    }
                }
                _ => {panic!()}
            }
        }
    }

    /// only used for morphyology mutation, update the `center` in each genonode, 
    /// so that the validation check can preform
    /// 
    /// this function only works for non-root block's mutation
    pub fn move_subtree_nodes(&mut self, root_index: usize, move_vec: ([f32;2],[f32;2],[f32;2])) {
        if root_index == 0 {
            panic!()
        }

        let forward = move_vec.0;
        let left_movec = move_vec.1;
        let right_movec = move_vec.2;

        let subtree_indices: Vec<usize> = self.vec_tree.subtree_indices(root_index);
    
        // move 1 units of all nodes
        for &i in &subtree_indices {
            if let Some(Some(genericnode)) = self.vec_tree.nodes.get_mut(i) {
                if let GenericGenoNode::Child(node) = genericnode{
                    node.center[0] += forward[0];
                    node.center[1] += forward[1];
                }
            } else {
                panic!()
            }
        }

        let direction = self.vec_tree.child_direction(root_index).unwrap();
        let children = self.vec_tree.children(root_index);

        // root of subtree that need to move 1 unit more
        let forward_root = children[direction];
        let subtree_indices: Vec<usize> = self.vec_tree.subtree_indices(forward_root);


        // move 1 more unit for selected nodes
        for &i in &subtree_indices {
            if let Some(Some(genericnode)) = self.vec_tree.nodes.get_mut(i) {
                if let GenericGenoNode::Child(node) = genericnode{
                    node.center[0] += forward[0];
                    node.center[1] += forward[1];
                }
            } else {
                panic!()
            }
        }

        // move left
        let left_root_index = children[get_left_right_direction(direction).0];
        let subtree_indices = self.vec_tree.subtree_indices(left_root_index);
        for &i in &subtree_indices {
            if let Some(Some(genericnode)) = self.vec_tree.nodes.get_mut(i) {
                if let GenericGenoNode::Child(node) = genericnode{
                    node.center[0] += left_movec[0];
                    node.center[1] += left_movec[1];
                }
            } else {
                panic!()
            }
        }

        // move right
        let right_root_index = children[get_left_right_direction(direction).1];
        let subtree_indices = self.vec_tree.subtree_indices(right_root_index);
        for &i in &subtree_indices {
            if let Some(Some(genericnode)) = self.vec_tree.nodes.get_mut(i) {
                if let GenericGenoNode::Child(node) = genericnode{
                    node.center[0] += right_movec[0];
                    node.center[1] += right_movec[1];
                }
            } else {
                panic!()
            }
        }

    }

    pub fn change_node_size(&mut self, index: usize, new_size: [f32;2]) {
        if let Some(Some(GenericGenoNode::Child(node))) = self.vec_tree.nodes.get_mut(index) {
            node.size = new_size;
        }
    }
    
}

/// GenericGenoNode is the Node in the BlobGeno QuadTree.
/// 
/// Representing morphyology of each block inside blob.
/// 
/// `Parent` is an indicator to show the parent direction of current node
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum GenericGenoNode {
    /// parent indicator
    Parent,
    Child(GenoNode),
}

/// minium signle geno node
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenoNode {
    pub joint_limits: [f32; 2],
    pub size: [f32; 2],
    pub center: [f32; 2],
    pub nn_id: Option<usize>,
}

impl Default for GenoNode {
    fn default() -> Self {
        Self {
            joint_limits: [-PI, PI],
            size: DEFAULT_BLOCK_SIZE,
            center: [0.0, 0.0],
            nn_id: None
        }
    }
}

impl GenoNode {
    pub fn from_nn_id(nn_id: usize) -> Self {
        Self {
            joint_limits: [-PI, PI],
            size: DEFAULT_BLOCK_SIZE,
            center: [0.0, 0.0],
            nn_id: Some(nn_id)
        }
    }
    /// generate `PhysiBlockBundle` from GenoNode
    fn to_bundle(&self, center: [f32; 2]) -> PhysiBlockBundle {
        PhysiBlockBundle::from_xy_dx_dy(center[0], center[1], self.size[0], self.size[1])
    }
}

/// QuadTree, Helper struct
#[derive(Clone, Serialize, Deserialize)]
pub struct QuadTree<T> {
    pub nodes: Vec<Option<T>>,
    pub max_depth: u32,
}

impl<T> QuadTree<T> {
    pub fn new(max_depth: u32) -> Self {
        let capacity = usize::pow(4, max_depth)+1;
        let nodes = (0..capacity).map(|_| None).collect();
        Self { max_depth, nodes }
    }

    pub fn parent(&self, index: usize) -> Option<usize> {
        if index == 0 {
            None
        } else {
            Some((index - 1) / 4)
        }
    }

    pub fn children(&self, index: usize) -> [usize; 4] {
        let base = 4 * index;
        [base + 1, base + 2, base + 3, base + 4]
    }

    pub fn depth(&self, index: usize) -> u32 {
        (index as f64).log(4.0).floor() as u32
    }

    pub fn is_leaf(&self, index: usize) -> bool {
        let children_indices = self.children(index);
        children_indices.iter().all(|&child_index| {
            child_index >= self.nodes.len() || self.nodes[child_index].is_none()
        })
    }

    pub fn clean_subtree(&mut self, index: usize) {
        self.nodes[index] = None;
        let child_indices = self.children(index);

        // For each child, if the child exists, clean it recursively
        for &child_index in &child_indices {
            if child_index < self.nodes.len() && self.nodes[child_index].is_some() {
                self.clean_subtree(child_index);
            }
        }
    }

    pub fn clean_subtree_without_self(&mut self, index: usize) {
        let child_indices = self.children(index);

        // For each child, if the child exists, clean it recursively
        for &child_index in &child_indices {
            if child_index < self.nodes.len() && self.nodes[child_index].is_some() {
                self.clean_subtree(child_index);
            }
        }
    }

    /// all nodes have at least one `none` child, using for mutate to gain limb
    pub fn branch_nodes(&self) -> Vec<usize> {
        let mut result = Vec::new();
        for i in 0..self.nodes.len() {
            if self.nodes[i].is_some() 
                && self.depth(i) < self.max_depth - 1 // Ensure the node is not at the last layer
                && self.children(i).iter().any(
                    |&child_idx| 
                    child_idx >= self.nodes.len() || self.nodes[child_idx].is_none()
                ) {
                result.push(i);
            }
        }
        result
    }

    /// all the not-None indices of the subtree
    pub fn subtree_indices(&self, index: usize) -> Vec<usize> {
        let mut result = Vec::new();

        // Recursive function to collect indices
        fn collect_indices<T>(quad_tree: &QuadTree<T>, index: usize, result: &mut Vec<usize>) {
            if quad_tree.nodes.get(index).is_some() && quad_tree.nodes[index].is_some() {
                result.push(index);

                for &child_index in &quad_tree.children(index) {
                    if child_index < quad_tree.nodes.len() {
                        collect_indices(quad_tree, child_index, result);
                    }
                }
            }
        }

        collect_indices(self, index, &mut result);
        result
    }

    pub fn child_direction(&self, index: usize) -> Option<usize>{
        if index == 0 || index > self.nodes.len() || self.nodes[index].is_none(){
            None
        } else {
            Some((index - 1) % 4)
        }
    }

    pub fn is_empty(&self, index: usize) -> bool {
        index < self.nodes.len() && self.nodes[index].is_none()
    }

    /// Calculates the Tree Edit Distance (TED) between two QuadTrees.
    ///
    /// The Tree Edit Distance is a measure of the similarity between two trees, defined as the minimum
    /// cost sequence of node deletions, insertions, and substitutions that transform one tree into the other.
    /// This function uses dynamic programming to efficiently compute the TED between the current tree and another.
    ///
    /// # Parameters
    ///
    /// * `other`: A reference to the other `QuadTree` with which the edit distance is to be calculated.
    ///
    /// # Returns
    ///
    /// Returns a `usize` representing the Tree Edit Distance between the two trees.
    ///
    /// # Examples
    ///
    /// ```
    /// let tree1 = QuadTree::new(...);
    /// let tree2 = QuadTree::new(...);
    /// let distance = tree1.tree_edit_distance(&tree2);
    /// ```
    pub fn tree_edit_distance(&self, other: &QuadTree<T>) -> usize {
        // Create a cache to store previous computed results
        let mut dp = vec![vec![None; other.nodes.len()]; self.nodes.len()];

        self._tree_edit_distance(0, 0, other, &mut dp)
    }

    fn _tree_edit_distance(
        &self, 
        i: usize, 
        j: usize, 
        other: &QuadTree<T>, 
        dp: &mut Vec<Vec<Option<usize>>>
    ) -> usize {
        if i >= self.nodes.len() && j >= other.nodes.len() {
            return 0;
        }
        if i >= self.nodes.len() {
            return 1 + other.children(j).iter().map(|&child_j| self._tree_edit_distance(i, child_j, other, dp)).sum::<usize>();
        }
        if j >= other.nodes.len() {
            return 1 + self.children(i).iter().map(|&child_i| self._tree_edit_distance(child_i, j, other, dp)).sum::<usize>();
        }
        if let Some(val) = dp[i][j] {
            return val;
        }

        let cost = if self.nodes[i].is_some() && other.nodes[j].is_some() {
            let children_i = self.children(i);
            let children_j = other.children(j);

            (0..4).map(|k| self._tree_edit_distance(children_i[k], children_j[k], other, dp)).sum::<usize>()
        } else if self.nodes[i].is_some() {
            1 + self.children(i).iter().map(|&child_i| self._tree_edit_distance(child_i, j, other, dp)).sum::<usize>()
        } else if other.nodes[j].is_some() {
            1 + other.children(j).iter().map(|&child_j| self._tree_edit_distance(i, child_j, other, dp)).sum::<usize>()
        } else {
            0
        };

        dp[i][j] = Some(cost);
        cost
    }
}

impl<T: Debug> Debug for QuadTree<T> {
    /// tree structure debug info
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fn print_node<T: Debug>(
            tree: &QuadTree<T>,
            index: usize,
            indent: &str,
            f: &mut fmt::Formatter<'_>,
        ) -> fmt::Result {
            match tree.nodes.get(index) {
                None | Some(None) => Ok(()), // skip empty nodes
                Some(Some(node)) => {
                    writeln!(f, "{}- Node {}: {:?}", indent, index, node)?;
                    let children = tree.children(index);
                    for &child_index in &children {
                        print_node(tree, child_index, &format!("{}  ", indent), f)?;
                    }
                    Ok(())
                }
            }
        }

        writeln!(f, "QuadTree {{")?;
        print_node(self, 0, "  ", f)?;
        writeln!(f, "}}")
    }
}

/// input the forward direction.
/// output the left and right direction in tuple
fn get_left_right_direction(direction:usize) -> (usize,usize) {
    match direction {
        0 => (2,3),
        1 => (3,2),
        2 => (1,0),
        3 => (0,1),
        _ => {panic!()}
    }
}


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

    #[test]
    fn test_geno_builder_validation() {
        for _ in 0..100 {
            let geno = BlobGeno::new_rand();
            assert!(geno.is_valid());
        }
    }
}