package sikd.sikd5.security;

import com.github.adminfaces.template.config.AdminConfig;
import com.github.adminfaces.template.session.AdminSession;

import sikd.sikd5.sikd5bean.tabel.mgr.TUserAccount;
import sikd.sikd5.sikd5bean.util.ServerUtil;
import sikd.sikd5.sikd5bean.util.SikdSessionUtil;
import sikd.sikd5.sikd5ejb.session.login.LoginSessionBeanRemote;

import org.omnifaces.util.Messages;

import static com.github.adminfaces.starter.util.Utils.addDetailMessage;

import java.io.IOException;
import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.naming.Context;

import org.omnifaces.util.Faces;

@SessionScoped
@Named("authorizer")
public class CustomAuthorizer implements Serializable {

    private String currentUser;
    @Inject
	private AdminConfig adminConfig;

    @Inject
    AdminSession adminSession;

    public void login(String username) throws IOException {
        currentUser = username;
        adminSession.setIsLoggedIn(true);
        Messages.addInfo(null,"Logged in sucessfully pake custom as <b>"+username+"</b>");
        Faces.redirect(adminConfig.getIndexPage());
//    	try {
//			Faces.getExternalContext().getFlash().setKeepMessages(true);
//			Context ctxOffice = ServerUtil.getOfficeContextV3();
//			LoginSessionBeanRemote sessionLogin = (LoginSessionBeanRemote) ctxOffice
//					.lookup(SikdSessionUtil.LOGIN_SESSION);
//			TUserAccount tuserAccount = sessionLogin.getUserAccountbyName(email);
//			if (tuserAccount != null) {
//				currentUser = email;
//				addDetailMessage("Logged in successfully as <b>" + email + "</b>");
//				
//				Faces.redirect(adminConfig.getIndexPage());
//			}else {
////				currentUser = email;
//				addDetailMessage("Logged in gagal as <b>" + email + "</b>");
//				
//				Faces.redirect(adminConfig.getLoginPage());
////				throw new Exception("User tidak terdaftar");
//				
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//			addDetailMessage("Logged in gagal : <b>" + e.getMessage()+ "</b>");
//			
//		}
    }

}
