use std::mem::take;

use crate::{game::ZLayer, State};
use comfy::{
	draw_rect, egui,
	egui::{Align, Layout},
	is_key_pressed, main_camera, Color, EngineContext, WHITE
};
use egui::{Align2, RichText};
use log::info;

pub fn update(state: &mut State, _engine: &mut EngineContext<'_>) {
	if is_key_pressed(comfy::KeyCode::Return) {
		info!("Restart game");
		take(state); //reset to default
	}
}

pub fn draw(state: &State, _engine: &EngineContext<'_>) {
	let cam = main_camera();
	draw_rect(
		cam.center,
		cam.world_viewport() * 1.2,
		Color::new(0.0, 0.0, 0.0, 0.5),
		ZLayer::GameOver.into()
	);

	egui::Area::new("game_over")
		.anchor(Align2::CENTER_CENTER, egui::vec2(0.0, 0.0))
		.show(egui(), |ui| {
			ui.with_layout(Layout::top_down_justified(Align::Center), |ui| {
				ui.heading(
					RichText::new(
						"The poor spirit lost all their energy and starve to death"
					)
					.color(WHITE)
					.strong()
					.size(50.0)
				);
				ui.label(
					RichText::new(format!("\nYour Score:\n{:.0}", state.score))
						.color(WHITE)
						.size(30.0)
				);
				ui.label(
					RichText::new("\n\n\npress enter for restart")
						.color(WHITE)
						.size(30.0)
				);
			})
		});
}
