package gui.menu;

import gui.constants.Images;
import gui.constants.Options;
import gui.constants.Symbols;
import imgui.ImGui;
import imgui.extension.imguifiledialog.ImGuiFileDialog;
import imgui.extension.imguifiledialog.flag.ImGuiFileDialogFlags;
import imgui.type.ImInt;

public class OptionsMenu extends BaseView {

	private ImInt currentValue = new ImInt();
	private float backgroundSpeed[] = new float[] { Options.BackgroundSpeed };

	public OptionsMenu(StateMachine stateMachine) {
		super(stateMachine);
	}

	private void renderImageSelectDialog() {
		if (ImGuiFileDialog.display("browse-img", ImGuiFileDialogFlags.None)) {
			if (ImGuiFileDialog.isOk()) {
				var selection = ImGuiFileDialog.getSelection();
				for (var entry : selection.entrySet()) {
					try {
						String filePath = entry.getValue();
						Options.BackgroundPath = filePath;
						Images.reloadImages();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			ImGuiFileDialog.close();
		}
	}

	private void renderImageSelectButton() {
		if (ImGui.button("Changer de fond d'écran"))
			ImGuiFileDialog.openDialog("browse-img", "Choisissez un fichier", ".png,.jpg,.jpeg", ".");
		renderImageSelectDialog();
	}

	@Override
	public void render() {
		ImGui.text("Options");
		if (ImGui.combo("Jeu de symboles", currentValue, Symbols.getSymbolsNames())) {
			Options.Symboles = Symbols.values()[currentValue.get()];
		}
		if (ImGui.sliderFloat("Vitesse d'animation de l'arrière plan", backgroundSpeed, 0.0f, 10.0f)) {
			Options.BackgroundSpeed = backgroundSpeed[0];
		}
		renderImageSelectButton();
		renderReturnButton();
	}

}
