use super::*;

mod windowhandle; pub use windowhandle::*;

mod query; pub use query::*;
mod config; pub use config::WindowConfig;
mod layout; pub use layout::*;
mod winproc;
mod display;
mod internal; use internal::Internal;

pub struct WindowManager {
    channel:Sender<Query>,
}

impl WindowManager {
    pub fn create(&self, config:WindowConfig) -> Result<(),()>
    {
        self.channel.send(Query::new(
            Some(self.channel.clone()),
            QueryData::Window(WindowQuery::Create {
                config,
            }
        ))).map_err(|_|())
    }
}

impl Subsystem for WindowManager {
    fn start(supervisor:Option<Sender<Query>>) -> Result<Sender<Query>, crate::Error>
    {
        if let Some(supervisor) = supervisor {
            let (tx, rx) = channel::bounded::<Query>(24);

            let sys_tx = tx.clone();
            std::thread::spawn(move || {
                Internal::thread(supervisor, sys_tx, rx);
            });

            Ok(tx)
        } else {
            Err(Error::new())
        }
    }
}

impl From<Sender<Query>> for WindowManager {
    fn from(channel:Sender<Query>) -> Self {
        Self { channel }
    }
}
