package szakdolgozat.client.ui;

import java.util.Date;

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.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
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 LoginWindow extends Composite {
	public TextBox userNameTextBox;
	private PasswordTextBox passwordTextBox;
	private Label errorLabel;

	public LoginWindow() {

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

		Label lblBelépés = new Label("Belépés a rendszerbe");
		lblBelépés.setStyleName("gwt-blueLabel");
		verticalPanel.add(lblBelépés);
		verticalPanel.setCellVerticalAlignment(lblBelépés,
				HasVerticalAlignment.ALIGN_MIDDLE);

		FlexTable flexTable = new FlexTable();
		verticalPanel.add(flexTable);
		flexTable.setSize("380px", "150px");

		Label lblFelhasználónév = new Label("Felhasználónév");
		lblFelhasználónév.setStyleName("gwt-blueLabel");
		flexTable.setWidget(0, 0, lblFelhasználónév);

		userNameTextBox = new TextBox();
		flexTable.setWidget(0, 1, userNameTextBox);
		userNameTextBox.setFocus(true);

		Label lblJelszó = new Label("Jelszó");
		lblJelszó.setStyleName("gwt-blueLabel");
		flexTable.setWidget(1, 0, lblJelszó);

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

		CheckBox chckbxAutoLogout = new CheckBox(
				"Automatikus kiléptetés egy idő után");
		flexTable.setWidget(2, 1, chckbxAutoLogout);

		Button loginButton = new Button("New button");
		loginButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				loginUser(userNameTextBox.getText(), passwordTextBox.getText());
			}
		});
		loginButton.setText("Belépés");
		flexTable.setWidget(3, 1, loginButton);
		flexTable.getCellFormatter().setHorizontalAlignment(3, 1,
				HasHorizontalAlignment.ALIGN_LEFT);
		errorLabel = new Label();
		verticalPanel.add(errorLabel);
		verticalPanel.setCellVerticalAlignment(errorLabel,
				HasVerticalAlignment.ALIGN_MIDDLE);
		
	}

	protected void loginUser(String username, String password) {
		final User user = new User();
		user.setUsername(username);
		user.setPassword(MD5.getMD5(password));
		user.setLoginDate(new Date());

		// services		
		UI.servicesHandler.isUserPasswordValid(user,
				new AsyncCallback<Integer>() {

					@Override
					public void onFailure(Throwable caught) {
						errorLabel
								.setText("Sikertelen bejelentkezés! Rossz felhasználónév / jelszó.");
						userNameTextBox.setText("");
						passwordTextBox.setText("");

					}

					@Override
					public void onSuccess(Integer result) {
						if (result >= 0) {
							// serverResponseLabel
							// .removeStyleName("serverResponseLabelError");
							// dialogBox.setText("Sikeres bejelentkezés");
							// serverResponseLabel.setHTML("Belépve");
							user.setId(result);
							user.setPassword("&");

							UI.currentUser = user;
							// UI.initMainMenuUI();
							History.newItem("main");
							// Window.Location.assign(Window.Location.getPath()
							// + Window.Location.getQueryString() + "#main");

						} else if (result == -2) {
							errorLabel
									.setText("Sikertelen bejelentkezés! Felhasználó már be van jelentkezve.");							
						} else
							errorLabel
									.setText("Sikertelen bejelentkezés! Rossz felhasználónév / jelszó.");

						userNameTextBox.setText("");
						passwordTextBox.setText("");

					}
				});
	}
}
