use strafesnet_graphics::setup;

fn create_window(title:&str,event_loop:&winit::event_loop::EventLoop<()>)->Result<winit::window::Window,winit::error::OsError>{
	let mut attr=winit::window::WindowAttributes::default();
	attr=attr.with_title(title);
	event_loop.create_window(attr)
}

pub async fn setup_and_start(title:&str){
	let event_loop=winit::event_loop::EventLoop::new().unwrap();

	let window=create_window(title,&event_loop).unwrap();

	println!("Initializing the surface...");

	let instance=setup::step1::create_instance();

	let surface=setup::step2::create_surface(&instance,&window).unwrap();

	let adapter=setup::step3::pick_adapter(&instance,&surface).await.expect("No suitable GPU adapters found on the system!");

	let (device,queue)=setup::step4::request_device(&adapter).await;

	let size=window.inner_size();
	let config=setup::step5::configure_surface(&adapter,&device,&surface,(size.width,size.height));

	//dedicated thread to ping request redraw back and resize the window doesn't seem logical

	//the thread that spawns the physics thread
	let mut window_thread=crate::window::worker(
		&window,
		device,
		queue,
		surface,
		config,
	);

	for arg in std::env::args().skip(1){
		window_thread.send(strafesnet_common::instruction::TimedInstruction{
			time:strafesnet_common::integer::Time::ZERO,
			instruction:crate::window::Instruction::WindowEvent(winit::event::WindowEvent::DroppedFile(arg.into())),
		}).unwrap();
	};

	println!("Entering event loop...");
	let mut app=crate::app::App::new(
		std::time::Instant::now(),
		window_thread
	);
	event_loop.run_app(&mut app).unwrap();
}
