package com.dragonsoft.tryapp.sysinterface;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import com.dragonsoft.tryapp.common.UserCredentials;
import com.dragonsoft.tryapp.common.exceptions.TryException;
import com.dragonsoft.tryapp.common.exceptions.UserAuthenticatorException;
import com.dragonsoft.tryapp.constants.TryConstants;
import com.dragonsoft.tryapp.ejb.session.interfaces.Login;
import com.dragonsoft.tryapp.ejb.session.interfaces.LoginHome;
import com.dragonsoft.tryapp.webapp.auth.WebAppUser;

/**
 * Used for login/logout operations. Designed differently from other "workers"
 * because a UserAuthenticator instance is needed before a UserCredentials
 * instance can be established.
 */
public class UserAuthenticator {
	/*
	 * The Cached home object
	 */
	private LoginHome loginHome;
	
    /**
     * Creates a user authenticator. Should always be able to create an
     * instance.
     */
    public UserAuthenticator() {

        init();
    }

    /**
     * Called to log in a user. Updates the WebAppUser session object if login
     * is successful.
     * 
     * @param strTryUsername
     *            the username
     * @param strTryPassword
     *            the password
     * @param usrObj
     *            the WebAppUser session object
     */
    public UserCredentials login(String strTryUsername, String strTryPassword,
            com.dragonsoft.tryapp.webapp.auth.WebAppUser usrObj)
            throws TryException {

        UserCredentials loginResult = null; 
         try {
			Login login = loginHome.create();
			loginResult = login.login(strTryUsername,strTryPassword,null);
			login.remove();
		} catch (RemoteException e) {
			init();
			throw new TryException("There was an error talking to the remote Objects",e);
		} catch (CreateException e) {
			throw new TryException("The creation to the Login Interface Failed.", e);
		} catch (RemoveException e) {
			throw new TryException("The handle to the remote objects was not removed cleanly.",e);
		} catch (FinderException e) {
			throw new UserAuthenticatorException("Bad Username or Password");
		}	
         // for now, second parameter is student level, get appropriate level
         // in the future
		// TODO Fix this when more is known.
		if(loginResult == null){
			throw new UserAuthenticatorException("Invalid Username and Password Combination");
		}
         usrObj.updateStatus(strTryUsername, TryConstants.TRY_USR_LVL_STU);
         

        return (loginResult);
    }

    /**
     * Called to log out a user.
     * 
     * @param usrObj
     *            the TryUser session object
     */
    public void logout(WebAppUser usrObj) {

        String tryUsername = usrObj.getUsername();

        usrObj.logout();
    }
    
    private void init(){
		try {
		Context context = new InitialContext();
		Object obj = context.lookup(LoginHome.JNDI_NAME);
        loginHome = (LoginHome) PortableRemoteObject.narrow(obj,
        LoginHome.class);
		} catch (NamingException e) {
			e.printStackTrace();
		}


    }

} // UserAuthenticator