package sikd.sikd5.security;

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

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

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

import org.omnifaces.util.Faces;

import com.github.adminfaces.template.config.AdminConfig;
import com.github.adminfaces.template.session.AdminSession;
import sikd.ikonsol.model.User;

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

/**
 * Created by rmpestano on 12/20/14.
 *
 * This is just a login example.
 *
 * AdminSession uses isLoggedIn to determine if user must be redirect to login
 * page or not. By default AdminSession isLoggedIn always resolves to true so it
 * will not try to redirect user.
 *
 * If you already have your authorization mechanism which controls when user
 * must be redirect to initial page or logon you can skip this class.
 */
@Named
@SessionScoped
@Specializes
public class LoginMB extends AdminSession implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String currentUser;
	private String email;
	private String password;
	private boolean remember;
	private TUserAccount tuserAccount;
        private User user;
	@Inject
	private AdminConfig adminConfig;
	LoginSessionBeanRemote sessionLogin;

	public void login() throws Exception {
//		
//	    currentUser = email;
//        addDetailMessage("Logged in successfully as <b>" + email + "</b>");
//        Faces.getExternalContext().getFlash().setKeepMessages(true);
//        Faces.redirect(adminConfig.getIndexPage());

		try {
			Faces.getExternalContext().getFlash().setKeepMessages(true);
			Context ctxOffice = ServerUtil.getOfficeContextV3();
			sessionLogin = (LoginSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.LOGIN_SESSION);
			//tuserAccount = sessionLogin.getUserAccountbyName(email);
                        tuserAccount = sessionLogin.getUserAccountbyName(email);
			if (tuserAccount == null) {
				throw new Exception("User tidak terdaftar");
			}
                        if (tuserAccount.isActives() == false) {
				throw new Exception("User tidak aktif");
			}
			byte[] p = new byte[password.trim().length()];
			for (int i = 0; i < p.length; i++) {
				p[i] = (byte) password.trim().charAt(i);
			}
			byte[] epass = Crypto.encrypt(p);
			if (!Arrays.equals(epass, tuserAccount.getPassword())) {
				throw new Exception("Kata Sandi tidak Sesuai");
			}
			currentUser = email;
			addDetailMessage("Login successfully as <b>" + email + "</b>");
			Faces.redirect(adminConfig.getIndexPage());
		} catch (Exception e) {
                        e.printStackTrace();
			addDetailMessage("Login gagal : <i>" + e.toString() + "</i>");
			Faces.redirect(adminConfig.getLoginPage());

		}
	}

	public boolean ambilData() {
//	   1,2,42,50,51,22,54
	   boolean hasil = false;
	   if (tuserAccount.getGroup().getId()==1 ||
	         tuserAccount.getGroup().getId()==2||
	         tuserAccount.getGroup().getId()==42||
	         tuserAccount.getGroup().getId()==50||
	         tuserAccount.getGroup().getId()==51||
	         tuserAccount.getGroup().getId()==22||
	         tuserAccount.getGroup().getId()==54
	         ) {
	      hasil = true;
	   }
	   return hasil;
	}
	public List<Pemda> getPemdaByTahuns(short tahun) throws Exception {
		return sessionLogin.getPemdaByTahuns(tahun);
	}

	@Override
	public boolean isLoggedIn() {
		return currentUser != null;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

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

	public boolean isRemember() {
		return remember;
	}

	public void setRemember(boolean remember) {
		this.remember = remember;
	}

	public String getCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(String currentUser) {
		this.currentUser = currentUser;
	}

	public TUserAccount getTuserAccount() {
		return tuserAccount;
	}
        
        
        /* batas User role */

    private boolean userOperatorRole;

    public boolean isUserOperatorRole() {
        return this.tuserAccount.getGroup().getId()==6;
    }

    public void setUserOperatorRole(boolean userOperatorRole) {
        this.userOperatorRole = userOperatorRole;
    }

 private boolean userAdminRole;

    public boolean isUserAdminRole() {
        return this.tuserAccount.getGroup().getId()==1;
    }

    public void setUserAdminRole(boolean userAdminRole) {
        this.userAdminRole = userAdminRole;
    }   


}
