//
//  ResetPasswordPage.java
//  blogwt
//
//  Created by William Shakour (billy1380) on 13 May 2015.
//  Copyright © 2015 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.client.page.user;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.willshex.blogwt.client.Resources;
import com.willshex.blogwt.client.controller.UserController;
import com.willshex.blogwt.client.helper.UiHelper;
import com.willshex.blogwt.client.page.Page;
import com.willshex.blogwt.client.page.PageType;
import com.willshex.blogwt.client.wizard.WizardDialog;

/**
 * @author William Shakour (billy1380)
 *
 */
public class ResetPasswordPage extends Page {

	private static ResetPasswordPageUiBinder uiBinder = GWT
			.create(ResetPasswordPageUiBinder.class);

	interface ResetPasswordPageUiBinder extends
			UiBinder<Widget, ResetPasswordPage> {}

	@UiField FormPanel frmReset;
	@UiField TextBox txtEmail;
	@UiField HTMLPanel pnlEmailNote;
	@UiField HTMLPanel pnlEmail;

	@UiField Button btnReset;

	public ResetPasswordPage () {
		super(PageType.ResetPasswordPageType);
		initWidget(uiBinder.createAndBindUi(this));

		UiHelper.addPlaceholder(txtEmail, "E-mail");
		UiHelper.autoFocus(txtEmail);
	}

	/* (non-Javadoc)
	 * 
	 * @see com.willshex.blogwt.client.page.Page#onAttach() */
	@Override
	protected void onAttach () {
		super.onAttach();

		ready();
	}

	@UiHandler("btnReset")
	void onResetClicked (ClickEvent ce) {
		if (isValid()) {
			loading();

			UserController.resetPassword(txtEmail.getText());
		} else {
			showErrors();
		}
	}

	/**
	 * @return
	 */
	private boolean isValid () {
		// do client validation
		return true;
	}

	/**
	 * 
	 */
	private void showErrors () {

	}

	private void ready () {
		btnReset.getElement()
				.setInnerSafeHtml(
						WizardDialog.WizardDialogTemplates.INSTANCE
								.nextButton("Reset"));

		btnReset.setEnabled(true);
		txtEmail.setEnabled(true);
	}

	private void loading () {
		btnReset.getElement().setInnerSafeHtml(
				WizardDialog.WizardDialogTemplates.INSTANCE.loadingButton(
						"Resetting... ", Resources.RES.primaryLoader()
								.getSafeUri()));

		btnReset.setEnabled(false);
		txtEmail.setEnabled(false);

		pnlEmail.removeStyleName("has-error");
		pnlEmailNote.setVisible(false);
	}
}

@@ -672,16 +672,67 @@
 Widget;%0A
+import com.willshex.blogwt.client.DefaultEventBus;%0A
 import c
@@ -1028,16 +1028,238 @@
 dDialog;
+%0Aimport com.willshex.blogwt.shared.api.user.call.ResetPasswordRequest;%0Aimport com.willshex.blogwt.shared.api.user.call.ResetPasswordResponse;%0Aimport com.willshex.blogwt.shared.api.user.call.event.ResetPasswordEventHandler;
 %0A%0A/**%0A *
@@ -1346,16 +1346,55 @@
 ds Page 
+implements%0A%09%09ResetPasswordEventHandler 
 %7B%0A%0A%09priv
@@ -2077,16 +2077,136 @@
 ach();%0A%0A
+%09%09register(DefaultEventBus.get().addHandlerToSource(%0A%09%09%09%09ResetPasswordEventHandler.TYPE, UserController.get(), this));%0A%0A
 %09%09ready(
@@ -2327,16 +2327,22 @@
 troller.
+get().
 resetPas
@@ -3116,11 +3116,828 @@
 lse);%0A%09%7D
+%0A%0A%09/* (non-Javadoc)%0A%09 * %0A%09 * @see%0A%09 * com.willshex.blogwt.shared.api.user.call.event.ResetPasswordEventHandler%0A%09 * #resetPasswordSuccess%0A%09 * (com.willshex.blogwt.shared.api.user.call.ResetPasswordRequest,%0A%09 * com.willshex.blogwt.shared.api.user.call.ResetPasswordResponse) */%0A%09@Override%0A%09public void resetPasswordSuccess (ResetPasswordRequest input,%0A%09%09%09ResetPasswordResponse output) %7B%0A%09%09// probably show a message about expecting an email %0A%09%7D%0A%0A%09/* (non-Javadoc)%0A%09 * %0A%09 * @see%0A%09 * com.willshex.blogwt.shared.api.user.call.event.ResetPasswordEventHandler%0A%09 * #resetPasswordFailure%0A%09 * (com.willshex.blogwt.shared.api.user.call.ResetPasswordRequest,%0A%09 * java.lang.Throwable) */%0A%09@Override%0A%09public void resetPasswordFailure (ResetPasswordRequest input,%0A%09%09%09Throwable caught) %7B%0A%09%09GWT.log(%22resetPasswordFailure%22, caught);%0A%09%7D
 %0A%7D%0A

call user controller reset password with email