package szakdolgozat.client.ui;

import szakdolgozat.client.UI;
import szakdolgozat.shared.MD5;
import szakdolgozat.shared.User;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RegisterWindow extends Composite {
	private TextBox newUserNameTextBox;
	private PasswordTextBox newPasswordTextBox;

	public RegisterWindow() {

		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("440px", "200px");

		Label lblRegisztráció = new Label("Regisztráció");
		lblRegisztráció.setStyleName("gwt-blueLabel");
		verticalPanel.add(lblRegisztráció);
		verticalPanel.setCellVerticalAlignment(lblRegisztráció,
				HasVerticalAlignment.ALIGN_MIDDLE);

		FlexTable flexTable = new FlexTable();
		verticalPanel.add(flexTable);
		flexTable.setSize("391px", "159px");

		Label lblÚjFelhasználónév = new Label("Új felhasználónév");
		lblÚjFelhasználónév.setStyleName("gwt-blueLabel");
		flexTable.setWidget(0, 0, lblÚjFelhasználónév);

		newUserNameTextBox = new TextBox();
		flexTable.setWidget(0, 1, newUserNameTextBox);

		Label lbljJelsz = new Label("Használni kívánt jelszó");
		lbljJelsz.setStyleName("gwt-blueLabel");
		flexTable.setWidget(1, 0, lbljJelsz);

		newPasswordTextBox = new PasswordTextBox();
		newPasswordTextBox.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					registerNewUser(newUserNameTextBox.getText(),
							newPasswordTextBox.getText());
				}
			}
		});
		flexTable.setWidget(1, 1, newPasswordTextBox);

		Label lblA = new Label(
				"A jelszavakat a szerver oldalon kódolva tároljuk, ha elfelejti nem áll módunkban azt újraküldeni!");
		lblA.setStyleName("serverResponseLabelError");
		flexTable.setWidget(2, 0, lblA);
		flexTable.getFlexCellFormatter().setColSpan(2, 0, 2);

		Button registerButton = new Button("New button");
		registerButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				registerNewUser(newUserNameTextBox.getText(),
						newPasswordTextBox.getText());
			}
		});
		registerButton.setText("Regisztráció");
		flexTable.setWidget(3, 0, registerButton);
		flexTable.getFlexCellFormatter().setColSpan(3, 0, 2);
		flexTable.getCellFormatter().setHorizontalAlignment(3, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setHorizontalAlignment(2, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
	}

	protected void registerNewUser(String username, String password) {
		//ServicesAsync services = (ServicesAsync) GWT.create(Services.class);
		//ServiceDefTarget target = (ServiceDefTarget) services;
		//String relativeUrl = GWT.getModuleBaseURL() + "Services";
		//target.setServiceEntryPoint(relativeUrl);
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setAnimationEnabled(true);
		final HTML serverResponseLabel = new HTML();
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<br><b>Server válasza:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		User newUser = new User();
		newUser.setUsername(username);
		newUser.setPassword(MD5.getMD5(password));

		UI.servicesHandler.registerNewUser(newUser, new AsyncCallback<Boolean>() {

			@Override
			public void onSuccess(Boolean result) {
				if (result == true) {
					serverResponseLabel
							.removeStyleName("serverResponseLabelError");
					dialogBox.setText("Sikeres regisztráció");
					serverResponseLabel
							.setHTML("Az új felhasználónevet sikerült rögzíteni");
				} else {
					dialogBox.setText("Sikertelen regisztráció");
					serverResponseLabel
							.setHTML("A felhasználónév már létezik!");
				}

				dialogBox.center();
				closeButton.setFocus(true);
			}

			@Override
			public void onFailure(Throwable caught) {
				dialogBox.setText("Remote Procedure Call - Failure");
				serverResponseLabel.addStyleName("serverResponseLabelError");
				serverResponseLabel.setHTML("HIBA \n" + caught.getMessage());
				dialogBox.center();
				closeButton.setFocus(true);

			}
		});

	}
}
