package authoritymanager.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;

public class SignupPage extends Page {
	private DialogBox dlgSignup ;
	private TextBox txtUsername ;
	private PasswordTextBox txtPassword ;
	
	public SignupPage(AuthorityManagerView authorityManagerView) {
		super(authorityManagerView) ;
		
		dlgSignup = new DialogBox();
		dlgSignup.setText("Register...");
		dlgSignup.setTitle("Register...");
		dlgSignup.setWidth("250px");
		dlgSignup.setHeight("200px");
		FlexTable pnlSignup = new FlexTable();
		pnlSignup.setWidth("290px");
		pnlSignup.setHeight("170px");
		pnlSignup.setCellSpacing(2);
		dlgSignup.add(pnlSignup);

		Label lblUsername = new Label("Username: ");
		txtUsername = new TextBox();
		Label lblPassword = new Label("Password: ");
		txtPassword = new PasswordTextBox();
		Label lblAccess = new Label("Access: ");
		final ListBox lstAccess = new ListBox();
		lstAccess.addItem("Normal");
		lstAccess.addItem("Administrator");
		pnlSignup.setWidget(0, 0, lblUsername);
		pnlSignup.setWidget(0, 1, txtUsername);
		pnlSignup.setWidget(1, 0, lblPassword);
		pnlSignup.setWidget(1, 1, txtPassword);
		pnlSignup.setWidget(2, 0, lblAccess);
		pnlSignup.setWidget(2, 1, lstAccess);

		Button btnRegister = new Button("Sign up", new ClickHandler() {
			public void onClick(ClickEvent event) {
				String username = txtUsername.getValue();
				//String password = BCrypt.hashpw(txtPassword.getValue(), BCrypt.gensalt()) ;
				String password = txtPassword.getValue() ;
				String access = lstAccess.getItemText(lstAccess
						.getSelectedIndex());
				AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
					public void onFailure(Throwable caught) {
					}

					public void onSuccess(Boolean result) {
						if (result.booleanValue()) {
							hide() ;
							
							PopupPanel pnlResult = new PopupPanel(true);
							Label lblResult = new Label(
									"Registered successfully!");							
							pnlResult.add(lblResult);
							pnlResult.center();
							pnlResult.show();							
							pnlResult.addCloseHandler(new CloseHandler<PopupPanel>() {
								public void onClose(CloseEvent<PopupPanel> event) {
									view.showLoginPage();
								}
							}) ;
						} else {
							PopupPanel pnlResult = new PopupPanel(true);
							Label lblResult = new Label(
									"Registration failed [Username exists or empty password]!");
							pnlResult.add(lblResult);
							pnlResult.center();
							pnlResult.show();
						}
					}
				};
				view.getController().register(username, password, access, callback);
			}
		});
		Button btnCancel = new Button("Cancel", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide() ;
				view.showLoginPage() ;
			}
		}) ;
		pnlSignup.setWidget(3, 0, btnRegister);
		pnlSignup.setWidget(3, 2, btnCancel);		
	}
	
	public void show() {
		txtUsername.setText("");
		txtPassword.setText("") ;
		dlgSignup.center() ;
		dlgSignup.show() ;
	}
	
	public void hide() {
		dlgSignup.hide() ;
		
	}
}
