use core::sync::atomic::{AtomicBool, Ordering};

use specs::{Entity, VecStorage, Component};


pub struct Child {
    parent: Entity,
    dirty: AtomicBool,
}

impl Component for Child {
    type Storage = VecStorage<Child>;
}

impl Child {

    #[inline(always)]
    pub fn new(entity: Entity) -> Child {
        Child {
            parent: entity,
            dirty: AtomicBool::new(true),
        }
    }

    #[inline(always)]
    pub fn parent(&self) -> Entity {
        self.parent
    }

    #[inline]
    pub fn set_parent(&mut self, entity: Entity) {
        self.parent = entity;
        self.flag(true);
    }

    #[inline(always)]
    pub fn flag(&self, dirty: bool) {
        self.dirty.store(dirty, Ordering::SeqCst);
    }

    #[inline(always)]
    pub fn is_dirty(&self) -> bool {
        self.dirty.load(Ordering::SeqCst)
    }
}
