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

import com.evolute.siprp.client.panels.utils.dialog.CDialogBox;
import com.evolute.siprp.client.panels.utils.navigation.NavigationConstants;
import com.evolute.siprp.client.panels.utils.navigation.PageNavigation;
import com.evolute.siprp.client.vo.Utilizador;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
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.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LoginPanel extends Composite 
{

	private static LoginPanelUiBinder uiBinder = GWT.create( LoginPanelUiBinder.class );
	interface LoginPanelUiBinder extends UiBinder< Widget, LoginPanel > {  }
	
	@UiField protected TextBox txtUsername;
	@UiField protected PasswordTextBox txtPassword;
	@UiField protected Anchor lnkRecoverPwd;
	@UiField protected Button btnLogin;

	private LoginServiceAsync loginService;
	
	
	public LoginPanel() 
	{
		initWidget( uiBinder.createAndBindUi( this ) );
		
		this.loginService = ( LoginServiceAsync ) GWT.create( LoginService.class );
		ServiceDefTarget serviceDef = ( ServiceDefTarget ) this.loginService;
		serviceDef.setServiceEntryPoint( GWT.getModuleBaseURL() + "loginService" );
		
		lnkRecoverPwd.setText( "Recuperar palavra-chave" );
		btnLogin.setText( "Entrar" );
	}
	
	@UiHandler( {"btnLogin", "lnkRecoverPwd"} ) 
	protected void onClick( ClickEvent e ) 
	{
		if ( e.getSource().equals( btnLogin ) )
		{
			doLogin();
		}
		else if ( e.getSource().equals( lnkRecoverPwd ) )
		{
			gotoRecoverPwd();
		}
	}

	@UiHandler( {"txtPassword", "txtUsername"} )
	protected void onKeyUp( KeyUpEvent e )
	{
		if ( e.getNativeKeyCode() == KeyCodes.KEY_ENTER ) 
		{
			doLogin();
		}
	}
		
	private void doLogin()
	{
		String userName = txtUsername.getText().trim();
		String passwd = txtPassword.getText().trim();
		
		if ( ! userName.equals( "" ) )
		{
			if ( ! passwd.equals( "" ) )
			{
				loginService.doLogin( txtUsername.getText(), txtPassword.getText(), new AsyncCallback< Utilizador >() {
					@Override
					public void onFailure( Throwable caught ) 
					{
						Window.alert( "Oops ! Unable to perform login." );
					}
					
					@Override
					public void onSuccess( Utilizador result ) 
					{
						if ( result == null )
						{
							CDialogBox box = new CDialogBox( "Dados inv\u00E1lidos", new String[] { "Utilizador e/ou palavra-chave incorrectos." } );
							box.center();
						}
						else
						{
							PageNavigation.getProvider().setUserLogged( result );
							PageNavigation.goPage( NavigationConstants.APP_MAIN_PANEL );
						}
					}
				});
			}
			else
			{
				CDialogBox box = new CDialogBox( "Erro ...", new String[] { "Tem de introduzir uma palavra-chave." } );
				box.center();
			}
		}
		else
		{
			CDialogBox box = new CDialogBox( "Erro ...", new String[] { "Tem de introduzir um nome de utilizador." } );
			box.center();
		}
	}
	
	
	private void gotoRecoverPwd()
	{
		PageNavigation.goPage( NavigationConstants.RECOVER_PWD_PANEL );
	}
	
}
