/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sikd.sikd5.sikd5bean.util;

import java.io.File;
import java.io.Serializable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;

/**
 *
 * @author agus
 */
public class ServerUtil implements Serializable {

    static String proploginCtx = "aasurl";
    static String propOfficeCtx = "officeurl";
    static String proploginCtxV3 = "aasurl_v3_wf";
   static String propOfficeCtxV3 = "officeurl_v3_wf";

    public ServerUtil() {
        if (SIKDUtil.SERVER.equals("Wildfly")) {
            proploginCtxV3 = "aasurl_v3_wf";
            propOfficeCtxV3 = "officeurl_v3_wf";
        } else if (SIKDUtil.SERVER.equals("Glassfish")) {
            proploginCtxV3 = "aasurl_v3_gf";
            propOfficeCtxV3 = "officeurl_v3_gf";
        }
    }

    public static Context getLoginContext() throws Exception {
        Properties pr = PropertiesLoader.loadProperties("sikd.properties");
        Properties prop = new Properties();
        if (pr.getProperty(proploginCtx) != null && !pr.getProperty(proploginCtx).trim().equals("")) {
            prop.put(InitialContext.PROVIDER_URL, pr.getProperty(proploginCtx));
        }
        Context ctx = new InitialContext(prop);
        return ctx;
    }

    public static Context getOfficeContext() throws Exception {
        Properties pr = PropertiesLoader.loadProperties("sikd.properties");
        Properties prop = new Properties();
//        prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
        if (pr.getProperty(propOfficeCtx) != null && !pr.getProperty(propOfficeCtx).trim().equals("")) {
            prop.put(InitialContext.PROVIDER_URL, pr.getProperty(propOfficeCtx));
        }
        Context ctx = new InitialContext(prop);
//        InitialContext ctx = new InitialContext();
//        NamingEnumeration<NameClassPair> list = ctx.list("");
//        while (list.hasMore()) {
//            System.out.println(list.next().getName());
//        }
        return ctx;
    }

    public static Context getLoginContextV3() throws Exception {
        Properties pr = PropertiesLoader.loadProperties("sikd.properties");
        Properties prop = new Properties();
        if (pr.getProperty(proploginCtxV3) != null && !pr.getProperty(proploginCtxV3).trim().equals("")) {
            prop.put(InitialContext.PROVIDER_URL, pr.getProperty(proploginCtxV3));
        }
        Context ctx = new InitialContext(prop);
        return ctx;
    }

    public static Context getOfficeContextV3() throws Exception {
        Properties pr = PropertiesLoader.loadProperties("sikd.properties");
        Properties prop = new Properties();
//        prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
        if (pr.getProperty(propOfficeCtxV3) != null && !pr.getProperty(propOfficeCtxV3).trim().equals("")) {
            prop.put(InitialContext.PROVIDER_URL, pr.getProperty(propOfficeCtxV3));
        }
        System.out.println("Property context : " + pr.getProperty(propOfficeCtxV3));
        Context ctx = new InitialContext(prop);
//        InitialContext ctx = new InitialContext();
//        NamingEnumeration<NameClassPair> list = ctx.list("");
//        while (list.hasMore()) {
//            System.out.println(list.next().getName());
//        }
        return ctx;
    }

    public static String getDomain() {
        try {
            Properties pr = PropertiesLoader.loadPropertiesWildfly("sikd.properties");

            return pr.getProperty("sikddomain");
        } catch (Exception ex) {
            Logger.getLogger(ServerUtil.class.getName()).log(Level.SEVERE, null, ex);
            return "";
        }
    }

//    public static boolean cekSession(){
//        FacesContext fc1 = FacesContext.getCurrentInstance();
//            ExternalContext excontext1 = fc1.getExternalContext();
//        if (excontext1.getSession(false) == null ) {
//            return false;
//        }
//        return true;
//    }    
//    public static LoginMBean getLoginMBean(){
//        FacesContext facesContext = FacesContext.getCurrentInstance();
//            ValueExpression valExp = facesContext.getApplication().getExpressionFactory().createValueExpression(facesContext.getELContext(), "#{userBean}", LoginMBean.class);
//            LoginMBean userbean = (LoginMBean) valExp.getValue(facesContext.getELContext());
//            return userbean;
//    }
}
