package com.evolute.siprp.client.panels.index.recover_pwd;

import com.evolute.siprp.client.panels.utils.navigation.NavigationConstants;
import com.evolute.siprp.client.panels.utils.navigation.PageNavigation;
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.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class RecoverPwdPanel extends Composite 
{

	private static RecoverPwdPanelUiBinder uiBinder = GWT.create( RecoverPwdPanelUiBinder.class );
	interface RecoverPwdPanelUiBinder extends UiBinder< Widget, RecoverPwdPanel > {  }

	private RecoverServiceAsync recoverService;
	
	@UiField protected Button btnOK;
	@UiField protected Button btnCancel;
	@UiField protected TextBox txtUsername;
	@UiField protected TextBox txtEmail;

	
	public RecoverPwdPanel() 
	{
		initWidget( uiBinder.createAndBindUi( this ) );

		this.recoverService = ( RecoverServiceAsync ) GWT.create( RecoverService.class );
		ServiceDefTarget serviceDef = ( ServiceDefTarget ) this.recoverService;
		serviceDef.setServiceEntryPoint( GWT.getModuleBaseURL() + "recoverService" );
		
		btnOK.setText( "OK" );
		btnCancel.setText( "Cancelar" );
	}

	@UiHandler( {"btnOK", "btnCancel"} )
	protected void onClick( ClickEvent e ) 
	{
		if ( e.getSource().equals( btnOK ) )
		{
			String userName = txtUsername.getText().trim();
			String eMail = txtEmail.getText().trim();
			
			this.recoverService.recoverPWD( userName, eMail, new AsyncCallback< String >() {
				@Override
				public void onFailure( Throwable caught ) 
				{
					Window.alert( "Oops !" );
				}

				@Override
				public void onSuccess( String result ) 
				{
					Window.alert( result );
				}
			});
		}
		else if ( e.getSource().equals( btnCancel ) )
		{
			PageNavigation.goPage( NavigationConstants.LOGIN_PANEL );
		}
	}

}
