package xyz.thastertyn.ContentDisplay;

import java.util.Arrays;

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.Tuples.Triplet;

public class CredentialsInput extends DialogWindow {

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

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

		Panel buttonPanel = new Panel();
		buttonPanel
			.setLayoutManager(
				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()
			.setLayoutManager(new GridLayout(1)
				.setLeftMarginSize(1)
				.setRightMarginSize(1));

		mainPanel.addComponent(new Label("Enter your username and password"));

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

		Panel userPanel = new Panel()
			.setLayoutManager(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("Username: "))
				.addComponent(username)
				.addTo(mainPanel);

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

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

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

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

		rememberPanel.addComponent(new Label("Rembember?"))
			.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);
	}

	public void onOK()
	{
		this.user = username.getText();
		this.pass = password.getText();

		if (user.isEmpty() || pass.isEmpty())
		{
			MessageDialog.showMessageDialog(getTextGUI(), getTitle(), "Username and password cannot be blank",
					MessageDialogButton.OK);
			return;
		}

		close();
	}

	public void onCancel() {
		close();
	}

	@Override
	public Triplet<String, String, Boolean> showDialog(WindowBasedTextGUI textGUI) {
		super.showDialog(textGUI);
		return new Triplet<String,String,Boolean>(user, pass, remember.isChecked());
	}
}
