package gui.menu;

import game.Player;
import gui.ColorGenerator;
import gui.widget.SudokuRenderer;
import imgui.ImGui;
import imgui.ImVec4;
import sudoku.structure.MultiDoku;

public class EndGameView extends BaseView {

	private final Player winner;
	private float time = 0;

	private static final ImVec4 YELLOW = new ImVec4(1, 1, 0, 1);

	private final SudokuRenderer sudokuRenderer;

	public EndGameView(StateMachine stateMachine, MultiDoku resolved, Player winner) {
		super(stateMachine);
		this.winner = winner;
		this.sudokuRenderer = new SudokuRenderer(resolved);
	}

	private ImVec4 getPseudoColor() {
		time += ImGui.getIO().getDeltaTime();
		float factor = (float) Math.cos(time);
		var color = ColorGenerator.hslToRgb(factor * factor, 0.9f, 0.4f);
		return new ImVec4(color.r, color.g, color.b, 1.0f);
	}

	private void renderWinText() {
		String winText = " a gagné !";
		String text = winner.getPseudo() + winText;
		float textWidth = ImGui.calcTextSizeX(text);
		ImGui.setCursorPosX(ImGui.getIO().getDisplaySizeX() / 2.0f - textWidth / 2.0f);
		ImGui.textColored(getPseudoColor(), winner.getPseudo());
		ImGui.sameLine();
		ImGui.textColored(YELLOW, winText);
	}

	@Override
	public void render() {
		renderWinText();
		this.sudokuRenderer.render();
		renderReturnButton();
	}

}
