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
//! Mixing hecs entity builder with legion intoComponentSource
//! ```
//! let mut world = legion::World::new(Default::default());
//! let mut components = EntityBuilder::new();
//! components.add(42i32);
//! components.add(true);
//! components.add(vec![0, 1, 2, 3]);
//! components.add("hello");
//! components.add(0u64);
//! let components = components.build();
//! let entity = world.extend(components)[0];
//! assert_eq!(Ok(&42), world.entry(entity).unwrap().get_component::<i32>());
//! assert_eq!(Ok(&vec![0, 1, 2, 3]), world.entry(entity).unwrap().get_component::<Vec<i32>>());
//!
//! ```

// WIP
// TODO try to use it in generators to facilitate adding new metadata
//! # Possible build facilities
//! ```
//! struct Builder<T, S> {
//!     inner: BuiltEntity,
//!     phantom: PhantomData<T,S>
//! }
//! fn new<T:TypeTrait, H:NodeHashs>(t:T, h:H) -> Builder<T,Typed> {
//!     let mut inner = BuiltEntity::default();
//!     inner.add(t);
//!     inner.add(h);
//!     Builder {
//!         inner,
//!         phantom: PhantomData
//!     }
//! }
//! 
//! trait Final {}
//! struct Typed;
//! struct Keyword;
//! impl Final for Keyword {}
//! struct Labeled;
//! impl Final for Labeled {}
//! struct WithChildren;
//! impl Final for WithChildren {}
//! 
//! // use a bound on T to know if it can have a label ?
//! impl<T> Builder<T,Typed> {
//!     pub fn label(self, l: LabelIdentifier) -> Builder<T, Labeled> {
//!         let mut inner = self.inner;
//!         inner.add(l);
//!         Builder {
//!             inner,
//!             phantom: PhantomData
//!         }
//!     }
//!     pub fn children(self, cs: Children) -> Builder<T, WithChildren> {
//!         let mut inner = self.inner;
//!         inner.add(cs);
//!         Builder {
//!             inner,
//!             phantom: PhantomData
//!         }
//!     }
//!     pub fn add_metadata(self, md: MD) -> Builder<T, Keyword> {
//!         let mut inner = self.inner;
//!         inner.add(md);
//!         Builder {
//!             inner,
//!             phantom: PhantomData
//!         }
//!     }
//! }
//! 
//! impl<T, S:Final> Builder<T,S> {
//!     pub fn add_metadata(self, md: MD) -> Builder<T, S> {
//!         let mut inner = self.inner;
//!         inner.add(md);
//!         Builder {
//!             inner,
//!             phantom: PhantomData
//!         }
//!     }
//!     pub fn build(self) -> BuiltEntity {
//!         self.inner.build()
//!     }
//! }
//! 
//! ```

use std::{
    alloc::{alloc, dealloc, Layout},
    any::TypeId,
    collections::HashMap,
    hash::{BuildHasher, BuildHasherDefault, Hasher},
    ptr::NonNull,
};

use legion::{
    query::{FilterResult, LayoutFilter},
    storage::{
        ArchetypeSource, ArchetypeWriter, ComponentSource, ComponentTypeId, EntityLayout,
        UnknownComponentStorage,
    },
    Entity,
};

use super::*;

/// A builder of entities for a archetypal store, here legion.
pub struct BuiltEntity {
    inner: Common<fn() -> Box<dyn UnknownComponentStorage>>,
}

#[derive(Default)]
pub struct EntityBuilder {
    pub inner: Common<fn() -> Box<dyn UnknownComponentStorage>>,
}

impl EntityBuilder {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn build(self) -> BuiltEntity {
        BuiltEntity {
            inner: self.inner,
        }
    }

    /// Add `component` to the entity.
    ///
    /// If the bundle already contains a component of type `T`, it will be dropped and replaced with
    /// the most recently added one.
    pub fn add<T: Component>(&mut self, mut component: T) -> &mut Self {
        unsafe {
            self.inner.add(
                (&mut component as *mut T).cast(),
                TypeInfo::of::<T>(),
                || Box::new(T::Storage::default()), //DynamicClone::new::<T>(),
            );
        }
        core::mem::forget(component);
        self
    }
}

// impl Iterator for BuiltEntity {
//     type Item = Self;

//     fn next(&mut self) -> Option<Self::Item> {
//         let r = todo!();

//         self.i += 1;
//         Some(r)
//     }
// }

impl IntoComponentSource for BuiltEntity
// where
//     I: IntoIterator,
//     DynBuiltEntity<I::Item, I::IntoIter>: ComponentSource,
{
    type Source = BuiltEntity;

    fn into(self) -> Self::Source {
        self
        // <Self::Source>::new(self.into_iter())
    }
}

impl IntoComponentSource for EntityBuilder
// where
//     I: IntoIterator,
//     DynBuiltEntity<I::Item, I::IntoIter>: ComponentSource,
{
    type Source = BuiltEntity;

    fn into(self) -> Self::Source {
        self.build()
        // <Self::Source>::new(self.into_iter())
    }
}

/// A layout filter used to select the appropriate archetype for inserting
/// entities from a component source into a world.
pub struct ComponentSourceFilter(Vec<ComponentTypeId>);

// impl Default for ComponentSourceFilter {
//     fn default() -> Self {
//         ComponentSourceFilter(PhantomData)
//     }
// }

impl LayoutFilter for ComponentSourceFilter {
    fn matches_layout(&self, components: &[ComponentTypeId]) -> FilterResult {
        // FilterResult::Match(components.is_empty())
        // TODO check if inverted
        FilterResult::Match(
            components.len() == self.0.len() && components.iter().all(|x| self.0.contains(x)),
        )
    }
}

impl ArchetypeSource for BuiltEntity {
    type Filter = ComponentSourceFilter;

    fn filter(&self) -> Self::Filter {
        let v = self.inner.info.iter().map(|x| x.0.id()).collect();
        ComponentSourceFilter(v)
    }

    fn layout(&mut self) -> EntityLayout {
        let mut layout = EntityLayout::default();

        for (tid, _offset, meta) in &self.inner.info {
            unsafe {
                layout.register_component_raw(tid.id(), meta.clone());
            }
        }

        layout
    }
}

impl ComponentSource for BuiltEntity {
    fn push_components<'a>(
        &mut self,
        writer: &mut ArchetypeWriter<'a>,
        mut entities: impl Iterator<Item = Entity>,
    ) {
        let entity = entities.next().unwrap();
        writer.push(entity);

        let v = unsafe { Vec::from_raw_parts(self.inner.storage.as_ptr(), self.inner.cursor, 4) };
        dbg!(&v);
        std::mem::forget(v);

        for (ty, offset, _) in &mut self.inner.info {
            let mut target = writer.claim_components_unknown(ty.id());
            let ptr = unsafe { self.inner.storage.as_ptr().add(*offset) };
            let len = ty.layout().size();

            println!();
            println!("store:  {:?}", self.inner.storage.as_ptr());
            println!("ptr:    {:p}", ptr);
            println!("off:    {:?}", offset);
            println!("cursor: {:?}", self.inner.cursor);
            println!("len:    {:?}", len);
            if ty.id().type_id() == TypeId::of::<(Vec<usize>,)>() {
                let aaa = ptr as *mut (Vec<usize>,);
                dbg!(unsafe { aaa.as_ref() });
            } else if ty.id().type_id() == TypeId::of::<(Box<[u32]>,)>() {
                let aaa = ptr as *mut (Box<[u32]>,);
                dbg!(unsafe { aaa.as_ref() });
            } else if ty.id().type_id() == TypeId::of::<Vec<u64>>() {
                let aaa = ptr as *mut Vec<u64>;
                dbg!(unsafe { aaa.as_ref() });
            }
            let len = 1;
            unsafe { target.extend_memcopy_raw(ptr, len) };
            if ty.id().type_id() == TypeId::of::<(Vec<usize>,)>() {
                let aaa = ptr as *mut (Vec<usize>,);
                dbg!(unsafe { aaa.as_ref() });
            } else if ty.id().type_id() == TypeId::of::<(Box<[u32]>,)>() {
                let aaa = ptr as *mut (Box<[u32]>,);
                dbg!(unsafe { aaa.as_ref() });
            } else if ty.id().type_id() == TypeId::of::<Vec<u64>>() {
                let aaa = ptr as *mut Vec<u64>;
                dbg!(unsafe { aaa.as_ref() });
            }
        }
    }
}

// impl legion::internals::insert::KnownLength for DynBuiltEntity<(), Iter>
// where
//     Iter: ExactSizeIterator,
// {
//     fn len(&self) -> usize {
//         self.iter.len()
//     }
// }

/// A hasher optimized for hashing a single TypeId.
///
/// TypeId is already thoroughly hashed, so there's no reason to hash it again.
/// Just leave the bits unchanged.
#[derive(Default)]
pub(crate) struct TypeIdHasher {
    hash: u64,
}

impl Hasher for TypeIdHasher {
    fn write_u64(&mut self, n: u64) {
        // Only a single value can be hashed, so the old hash should be zero.
        debug_assert_eq!(self.hash, 0);
        self.hash = n;
    }

    // Tolerate TypeId being either u64 or u128.
    fn write_u128(&mut self, n: u128) {
        debug_assert_eq!(self.hash, 0);
        self.hash = n as u64;
    }

    fn write(&mut self, bytes: &[u8]) {
        debug_assert_eq!(self.hash, 0);

        // This will only be called if TypeId is neither u64 nor u128, which is not anticipated.
        // In that case we'll just fall back to using a different hash implementation.
        let mut hasher = <DefaultHashBuilder as BuildHasher>::Hasher::default();
        hasher.write(bytes);
        self.hash = hasher.finish();
    }

    fn finish(&self) -> u64 {
        self.hash
    }
}

/// A HashMap with TypeId keys
///
/// Because TypeId is already a fully-hashed u64 (including data in the high seven bits,
/// which hashbrown needs), there is no need to hash it again. Instead, this uses the much
/// faster no-op hash.
pub(crate) type TypeIdMap<V> = HashMap<TypeId, V, BuildHasherDefault<TypeIdHasher>>;

/// Metadata required to store a component.
///
/// All told, this means a [`TypeId`], to be able to dynamically name/check the component type; a
/// [`Layout`], so that we know how to allocate memory for this component type; and a drop function
/// which internally calls [`core::ptr::drop_in_place`] with the correct type parameter.
#[derive(Debug, Copy, Clone)]
pub struct TypeInfo {
    id: ComponentTypeId,
    layout: Layout,
    drop: unsafe fn(*mut u8),
    // #[cfg(debug_assertions)]
    // type_name: &'static str,
}

impl TypeInfo {
    /// Construct a `TypeInfo` directly from the static type.
    pub fn of<T: 'static + Send + Sync>() -> Self {
        unsafe fn drop_ptr<T>(x: *mut u8) {
            x.cast::<T>().drop_in_place()
        }

        Self {
            id: ComponentTypeId::of::<T>(),
            layout: Layout::new::<T>(),
            drop: drop_ptr::<T>,
            // #[cfg(debug_assertions)]
            // type_name: core::any::type_name::<T>(),
        }
    }

    // /// Construct a `TypeInfo` from its components. This is useful in the rare case that you have
    // /// some kind of pointer to raw bytes/erased memory holding a component type, coming from a
    // /// source unrelated to hecs, and you want to treat it as an insertable component by
    // /// implementing the `DynamicBundle` API.
    // pub fn from_parts(id: ComponentTypeId, layout: Layout, drop: unsafe fn(*mut u8)) -> Self {
    //     Self {
    //         id,
    //         layout,
    //         drop,
    //         // #[cfg(debug_assertions)]
    //         // type_name: "<unknown> (TypeInfo constructed from parts)",
    //     }
    // }

    /// Access the `TypeId` for this component type.
    pub fn id(&self) -> ComponentTypeId {
        self.id
    }

    /// Access the `Layout` of this component type.
    pub fn layout(&self) -> Layout {
        self.layout
    }

    /// Directly call the destructor on a pointer to data of this component type.
    ///
    /// # Safety
    ///
    /// All of the caveats of [`core::ptr::drop_in_place`] apply, with the additional requirement
    /// that this method is being called on a pointer to an object of the correct component type.
    pub unsafe fn drop(&self, data: *mut u8) {
        (self.drop)(data)
    }

    /// Get the function pointer encoding the destructor for the component type this `TypeInfo`
    /// represents.
    pub fn drop_shim(&self) -> unsafe fn(*mut u8) {
        self.drop
    }
}

pub struct Common<M> {
    storage: NonNull<u8>,
    layout: Layout,
    cursor: usize,
    info: Vec<(TypeInfo, usize, M)>,
    ids: Vec<TypeId>,
    indices: TypeIdMap<usize>,
}

impl<M> Common<M> {
    fn has<T: Component>(&self) -> bool {
        self.indices.contains_key(&TypeId::of::<T>())
    }

    fn get_by_tid<'a, T>(&'a self, tid: &TypeId) -> Option<T> {
        let index = self.indices.get(tid)?;
        let (_, offset, _) = self.info[*index];
        unsafe {
            let storage = self.storage.as_ptr().add(offset).cast::<T>();
            // Some(T::from_raw(storage))
            Some(todo!())
        }
    }

    // fn get<'a, T: ComponentRefShared<'a>>(&'a self) -> Option<T> {
    //     let index = self.indices.get(&TypeId::of::<T::Component>())?;
    //     let (_, offset, _) = self.info[*index];
    //     unsafe {
    //         let storage = self.storage.as_ptr().add(offset).cast::<T::Component>();
    //         Some(T::from_raw(storage))
    //     }
    // }

    // fn get_mut<'a, T: ComponentRef<'a>>(&'a self) -> Option<T> {
    //     let index = self.indices.get(&TypeId::of::<T::Component>())?;
    //     let (_, offset, _) = self.info[*index];
    //     unsafe {
    //         let storage = self.storage.as_ptr().add(offset).cast::<T::Component>();
    //         Some(T::from_raw(storage))
    //     }
    // }

    fn component_types(&self) -> impl Iterator<Item = ComponentTypeId> + '_ {
        self.info.iter().map(|(info, _, _)| info.id())
    }

    unsafe fn grow(
        min_size: usize,
        cursor: usize,
        align: usize,
        storage: NonNull<u8>,
    ) -> (NonNull<u8>, Layout) {
        let layout = Layout::from_size_align(min_size.next_power_of_two().max(64), align).unwrap();
        let new_storage = NonNull::new_unchecked(alloc(layout));
        std::ptr::copy_nonoverlapping(storage.as_ptr(), new_storage.as_ptr(), cursor);
        (new_storage, layout)
    }

    fn clear(&mut self) {
        self.ids.clear();
        self.indices.clear();
        self.cursor = 0;
        // NOTE we do not clone stuff and use everything, thus we do not need to drop things pointed by structures in storage
        // unsafe {
        //     for (ty, offset, _) in self.info.drain(..) {
        //         ty.drop(self.storage.as_ptr().add(offset));
        //     }
        // }
    }

    unsafe fn add(&mut self, ptr: *mut u8, ty: TypeInfo, meta: M) {
        use std::collections::hash_map::Entry;
        use std::ptr;
        match self.indices.entry(ty.id().type_id()) {
            Entry::Occupied(occupied) => {
                let index = *occupied.get();
                let (ty, offset, _) = self.info[index];
                let storage = self.storage.as_ptr().add(offset);

                // Drop the existing value
                ty.drop(storage);

                // Overwrite the old value with our new one.
                ptr::copy_nonoverlapping(ptr, storage, ty.layout().size());
            }
            Entry::Vacant(vacant) => {
                let offset = align(self.cursor, ty.layout().align());
                let end = offset + ty.layout().size();
                if end > self.layout.size() || ty.layout().align() > self.layout.align() {
                    let new_align = self.layout.align().max(ty.layout().align());
                    let (new_storage, new_layout) =
                        Self::grow(end, self.cursor, new_align, self.storage);
                    if self.layout.size() != 0 {
                        dealloc(self.storage.as_ptr(), self.layout);
                    }
                    self.storage = new_storage;
                    self.layout = new_layout;
                }

                if ty.id().type_id() == TypeId::of::<(Vec<usize>,)>() {
                    let aaa = ptr as *mut (Vec<usize>,);
                    dbg!(aaa.as_ref());
                    // let v = unsafe { Vec::<usize>::from_raw_parts(ptr as *mut usize, 4, 4) };
                    // dbg!(&v);
                    // std::mem::forget(v);
                }

                if ty.id().type_id() == TypeId::of::<(Box<[u32]>,)>() {
                    let aaa = ptr as *mut (Box<[u32]>,);
                    dbg!(aaa.as_ref());
                    // let v = unsafe { Vec::<usize>::from_raw_parts(ptr as *mut usize, 4, 4) };
                    // dbg!(&v);
                    // std::mem::forget(v);
                }

                let addr = self.storage.as_ptr().add(offset);
                ptr::copy_nonoverlapping(ptr, addr, ty.layout().size());

                vacant.insert(self.info.len());
                self.info.push((ty, offset, meta));
                self.cursor = end;

                if ty.id().type_id() == TypeId::of::<(Box<[u32]>,)>() {
                    let aaa = ptr as *mut (Box<[u32]>,);
                    dbg!(aaa.as_ref());
                    // let v = unsafe { Vec::<usize>::from_raw_parts(ptr as *mut usize, 4, 4) };
                    // dbg!(&v);
                    // std::mem::forget(v);
                }
            }
        }
    }
}
fn align(x: usize, alignment: usize) -> usize {
    debug_assert!(alignment.is_power_of_two());
    (x + alignment - 1) & (!alignment + 1)
}
unsafe impl<M> Send for Common<M> {}
unsafe impl<M> Sync for Common<M> {}

impl<M> Drop for Common<M> {
    fn drop(&mut self) {
        // Ensure buffered components aren't leaked
        self.clear();
        if self.layout.size() != 0 {
            unsafe {
                dealloc(self.storage.as_ptr(), self.layout);
            }
        }
    }
}

impl<M> Default for Common<M> {
    /// Create a builder representing an entity with no components
    fn default() -> Self {
        Self {
            storage: NonNull::dangling(),
            layout: Layout::from_size_align(0, 8).unwrap(),
            cursor: 0,
            info: Vec::new(),
            ids: Vec::new(),
            indices: Default::default(),
        }
    }
}

#[test]
fn example() {
    let mut world = legion::World::new(Default::default());
    let mut components = EntityBuilder::new();
    components.add(42i32);
    components.add(true);
    components.add(vec![0, 1, 2, 3]);
    components.add("hello");
    components.add(0u64);
    let components = components.build();
    let entity = world.extend(components)[0];
    assert_eq!(Ok(&42), world.entry(entity).unwrap().get_component::<i32>());
    assert_eq!(
        Ok(&vec![0, 1, 2, 3]),
        world.entry(entity).unwrap().get_component::<Vec<i32>>()
    );
}

#[test]
fn simple() {
    let mut world = legion::World::new(Default::default());
    let mut components = EntityBuilder::new();
    let mut comp0: (Box<[u32]>,) = (vec![0,0,0,0,0,1,4100177920].into_boxed_slice(),);//0, 14, 43, 10, 876, 7, 1065, 35
    let mut comp0_saved = comp0.clone();
    let comp0_ptr  = (&mut comp0) as *mut (Box<[u32]>,);
    components.add(comp0);
    unsafe{(*comp0_ptr).0[4] = 42};
    comp0_saved.0[4] = 42;
    let comp1: i32 = 0;
    components.add(comp1);
    let comp2: bool = true;
    components.add(comp2);
    let mut comp3: Vec<u64> = vec![0, 1, 2, 3];
    let comp3_saved = comp3.clone();
    let comp3_ptr = (&mut comp3) as *mut Vec<u64>;
    components.add(comp3);
    let comp4: String = "ewgwgwsegwesf".into();
    components.add(comp4.clone());
    let comp5: u64 = 0;
    components.add(comp5);
    let components = components.build();
    dbg!(unsafe { comp0_ptr.as_ref() });
    let entity = world.extend(components)[0];
    assert_eq!(
        Some(&comp0_saved),
        unsafe { comp0_ptr.as_ref() },
        "slice should not have changed"
    );
    assert_eq!(
        Some(&comp3_saved),
        unsafe { comp3_ptr.as_ref() },
        "vec should not have changed"
    );

    if let Some(entry) = world.entry(entity) {
        unsafe{(*comp0_ptr).0[5] += 1};
        comp0_saved.0[5] += 1;
        dbg!(unsafe{comp0_ptr.as_ref()});
        assert_eq!(Ok(&comp0_saved), entry.get_component::<(Box<[u32]>,)>());
        assert_eq!(Ok(&comp1), entry.get_component::<i32>());
        assert_eq!(Ok(&comp2), entry.get_component::<bool>());
        assert_eq!(Ok(&comp3_saved), entry.get_component::<Vec<u64>>());
        assert_eq!(Ok(&comp4), entry.get_component::<String>());
    }
}