use ratatui::{
	Frame,
	layout::{Constraint, Flex, Layout},
	style::{Color, Style},
	text::Line,
};
use tui_big_text::{BigText, PixelSize};

use super::{
	menu::menu,
	utils::{Popup, rounded_block},
};
use crate::state::game_over_menu::GameOverMenu;

pub fn game_over_menu(f: &mut Frame, game_over_menu: &GameOverMenu) {
	let new_score = game_over_menu.new_score.clone();

	let offest = if new_score.is_some() {
		8
	} else {
		0
	};

	let popup = Popup::new(48 + offest, 24 + offest).render(f);

	let mut constraints = vec![Constraint::Length(4), Constraint::Length(16)];

	if new_score.is_some() {
		constraints.insert(1, Constraint::Length(6));
	}

	let chunk = Layout::vertical(constraints).spacing(2).split(popup);

	if let Some(score) = new_score {
		let new_score_block = rounded_block().title("NEW SCORE");
		let new_score_block_inner = new_score_block.inner(chunk[1]);
		let score = BigText::builder()
			.pixel_size(PixelSize::Quadrant)
			.lines([Line::raw(score)])
			.style(Style::new().fg(Color::Green))
			.build()
			.unwrap();

		f.render_widget(new_score_block, chunk[1]);
		f.render_widget(score, new_score_block_inner);
	}

	let title = BigText::builder()
		.pixel_size(PixelSize::Quadrant)
		.lines([Line::raw("GAME OVER")])
		.style(Style::new().fg(Color::Red))
		.build()
		.unwrap();

	let title_area = Layout::horizontal([Constraint::Length(36)])
		.flex(Flex::Center)
		.areas::<1>(chunk[0])[0];

	f.render_widget(title, title_area);

	menu(
		f,
		if chunk.len() == 3 {
			chunk[2]
		} else {
			chunk[1]
		},
		game_over_menu,
	);
}
