/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package admin;

import entities.Users;
import helper.DatabaseException;
import java.util.Map;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import manager.UserSessionBeanRemote;

/**
 *
 * @author Szilárd
 */
@ManagedBean(name="Login")
@RequestScoped
public class Login {
    @EJB
    private UserSessionBeanRemote userSessionBean;

    Users user;
    String userName, password;
    /** Creates a new instance of Login */
    public Login() {
        user = new Users();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String enterAction(){
        Users u;
        try {
            u = this.userSessionBean.login(userName, password);
        } catch (DatabaseException ex) {
            addMessage(ex.getMessage());
            return null;
        }
        Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        map.put("felhasznalo", u);
        String url = "";
        switch(u.getUserRole()){
            case STUDENT : 
                url = "menus";
                break;
            case TEACHER : 
                url = "menut";
                break;
            case ADMIN : 
                url = "menua";
                break;
            
        }
        return url;
    }

    public String createAction(){
        return "register.jsp";
    }

    protected void addMessage(String message){
        FacesMessage m = new FacesMessage(message);
        FacesContext.getCurrentInstance().addMessage(null, m);
    }

}
