use crate::system::*;

use winit::{
    event::{Event, WindowEvent},
    event_loop::{ControlFlow, EventLoop},
    window::Window,
    application::ApplicationHandler,
};

mod handler; use handler::Handler;

pub struct Internal {
    supervisor:Sender<Query>,
}
impl Internal {
    pub fn thread(supervisor:Sender<Query>, tx:Sender<Query>, rx:Receiver<Query>)
    {
        let _sv = Internal {
            supervisor,
        };

        // Initialize event loop.
        let channel = tx.clone();
        std::thread::spawn(move || {
            if let Ok(event_loop) = EventLoop::new() {
                let mut handler = Handler::new(channel);
                event_loop.set_control_flow(ControlFlow::Wait);
                event_loop.run_app(&mut handler).ok();
            }
        });

        // Handle messages.
        while let Ok(msg) = rx.recv() {
            match msg.data {
                QueryData::Supervisor(request) => match request {
                    SupervisorQuery::Stop {
                        system: _,
                    } => {

                        // Perform shutdown operations and terminate thread.
                        return;
                    }

                    _ => { }
                }

                QueryData::Window(request) => match request {
                    WindowQuery::Create {

                    } => {
                        
                    }

                    _ => { }
                }

                _ => { }
            }
        }
    }
}
