package xyz.thastertyn.UserInterface.Dialogs;

import java.util.Arrays;

import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.Direction;
import com.googlecode.lanterna.gui2.GridLayout;
import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.DialogWindow;

import xyz.thastertyn.Config.Config;
import xyz.thastertyn.Login.LoginController;
import xyz.thastertyn.UserInterface.Listeners.ContentResetListener;

public class EscapeDialog extends DialogWindow {

	private WindowBasedTextGUI textGUI;
	private ContentResetListener resetListener;

	public EscapeDialog(WindowBasedTextGUI textGUI, ContentResetListener resetListener)
	{
		super("Escape Menu");
		this.textGUI = textGUI;
		this.resetListener = resetListener;

		Panel mainPanel = new Panel(
			new GridLayout(1)
				.setLeftMarginSize(1)
				.setRightMarginSize(1));

		new Panel(new LinearLayout(Direction.VERTICAL))
			.addComponent(new Button("Zpet", this::onReturn))
			.addComponent(new Button("Odhlasit", this::onLogout))
			.addComponent(new Button("Smazat ulozene udaje", this::onDeleteLogin))
			.addComponent(new Button("Ukoncit", this::onExit))
			.addTo(mainPanel);

		setHints(Arrays.asList(Window.Hint.CENTERED));
		setComponent(mainPanel);
	}

	public void onReturn()
	{
		close();
	}

	public void onDeleteLogin()
	{
		close();
		Config.getInstance()
			.removeCredentials()
			.writeConfig();
		LoginController controller = new LoginController(textGUI);
		controller.loginUsingGui();
		resetListener.reset();
	}

	public void onLogout()
	{
		close();
		LoginController controller = new LoginController(textGUI);
		controller.login(true);
		resetListener.reset();
	}

	public void onExit()
	{
		close();
		System.exit(0);
	}

	@Override
	public Object showDialog(WindowBasedTextGUI textGUI) {
		return super.showDialog(textGUI);
	}
}
