package xyz.thastertyn.Login;

import java.util.Arrays;
import java.util.Optional;

import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.CheckBox;
import com.googlecode.lanterna.gui2.EmptySpace;
import com.googlecode.lanterna.gui2.GridLayout;
import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.LocalizedString;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.TextBox;
import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.GridLayout.Alignment;
import com.googlecode.lanterna.gui2.dialogs.DialogWindow;
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton;

import xyz.thastertyn.Types.InputtedCredentials;

/**
 * A Dialog window for inputing username and password.
 */
public class CredentialsInput extends DialogWindow {

	private TextBox username;
	private TextBox password;
	private String user;
	private String pass;

	private boolean wasCanceled = false;

	private CheckBox remember;
	
	public CredentialsInput()
	{
		super("Prihlaseni");
		this.user = null;
		this.pass = null;
		this.username = new TextBox();
		this.password = new TextBox().setMask('*');
		this.remember = new CheckBox();

		Panel buttonPanel = new Panel(new GridLayout(2).setHorizontalSpacing(1))
			.addComponent(
				new Button(LocalizedString.OK.toString(), this::onOK)
					.setLayoutData(GridLayout.createLayoutData(
						GridLayout.Alignment.CENTER,
						GridLayout.Alignment.CENTER,
						true,
						false)))
			.addComponent(
				new Button(LocalizedString.Cancel.toString(), this::onCancel));

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

		mainPanel.addComponent(new Label("Zadej sve jmeno a heslo"));

		mainPanel.addComponent(new EmptySpace(TerminalSize.ONE));

		Panel userPanel = new Panel(new GridLayout(3))
			.setLayoutData(
				GridLayout.createLayoutData(
					GridLayout.Alignment.FILL,
					Alignment.CENTER,
					true,
					false));

		this.username.setLayoutData(
			GridLayout.createLayoutData(
				GridLayout.Alignment.FILL,
				GridLayout.Alignment.CENTER,
				true,
				false));

		userPanel.addComponent(new Label("Jmeno: "))
				.addComponent(username)
				.addTo(mainPanel);

		this.password
			.setLayoutData(
				GridLayout.createLayoutData(GridLayout.Alignment.FILL,
					GridLayout.Alignment.CENTER,
					true,
					false))
			.addTo(mainPanel);

		Panel passPanel = new Panel(new GridLayout(3))
			.setLayoutData(GridLayout.createLayoutData(
				GridLayout.Alignment.FILL,
				Alignment.CENTER,
				true,
				false));

		passPanel.addComponent(new Label("Heslo: "))
			.addComponent(password)
			.addTo(mainPanel);

		if(!xyz.thastertyn.Config.Config.getInstance().checkIfCredentialsExist())
		{
			Panel rememberPanel = new Panel(new GridLayout(3))
				.setLayoutData(GridLayout.createLayoutData(
					GridLayout.Alignment.FILL,
					Alignment.CENTER,
					true,
					false));
	
			rememberPanel.addComponent(new Label("Zapamatovat?"))
				.addComponent(remember)
				.addTo(mainPanel);
		}
			
		mainPanel.addComponent(new EmptySpace(TerminalSize.ONE));

		buttonPanel.setLayoutData(
			GridLayout.createLayoutData(
				Alignment.END,
				Alignment.CENTER,
				false, false))
			.addTo(mainPanel);

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

	/**
	 * User pressed ok, check if anything is actually typed and if so, pack and return it
	 */
	public void onOK()
	{
		this.user = username.getText();
		this.pass = password.getText();

		if (user.isEmpty() || pass.isEmpty())
		{
			MessageDialog.showMessageDialog(getTextGUI(), getTitle(), "Uzivatelske jmeno a heslo nemohou byt prazdne",
					MessageDialogButton.OK);
			return;
		}

		close();
	}

	public void onCancel() {
		close();
		wasCanceled = true;
	}

	/**
	 * returns the inputted username, password and if user chose to keep credentials stored
	 * @return {@link InputtedCredentials} with username first, password second and whether to store the credentials third
	 */
	@Override
	public Optional<InputtedCredentials> showDialog(WindowBasedTextGUI textGUI) {
		super.showDialog(textGUI);

		if(wasCanceled)
		{
			return Optional.empty();
		}

		return Optional.of(new InputtedCredentials(user, pass, remember.isChecked()));
	}
}
