package com.eactive.eai.rms.client.service.websphere;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Properties;

import com.eactive.eai.rms.client.service.JMXMonitoring;

//import java.security.PrivilegedAction;
//import java.util.Collections;
//import java.util.Comparator;
//import java.util.HashMap;
//import java.util.Iterator;
//import java.util.LinkedHashMap;
//import java.util.List;
//import java.util.Properties;
//import java.util.Set;
//import java.util.stream.Collectors;
//
//import javax.management.Attribute;
//import javax.management.AttributeList;
//import javax.management.ObjectName;
//import javax.security.auth.Subject;
//import javax.security.auth.callback.CallbackHandler;
//import javax.security.auth.login.LoginContext;
//
//import com.ibm.websphere.management.AdminClient;
//import com.ibm.websphere.management.AdminClientFactory;
//import com.ibm.websphere.security.auth.WSSubject;
//import com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl;
//
public class SingleQueueMonitoringForWebSphere implements JMXMonitoring{
//	public static AdminClient ac = null;
//	
//	private static Boolean isSubjectLogin ; 
//
//
//	private static Properties properties ;
	private static SingleQueueMonitoringForWebSphere monitoring;
	public static SingleQueueMonitoringForWebSphere getInstance(){
        if(monitoring == null) {
            synchronized(SingleQueueMonitoringForWebSphere.class) {
                if(monitoring == null) {
                	monitoring = new SingleQueueMonitoringForWebSphere();   
                }   
            }
        }
        return monitoring;
	}
	
	private SingleQueueMonitoringForWebSphere() {
	}

	public void init(Properties properties) throws Exception {
//		if (ac == null) {
//			synchronized(SingleQueueMonitoring.class){
//				this.properties = properties;
//				Object obj = properties.get("isSubjectLogin");
//				if (obj == null){
//					this.isSubjectLogin = false;
//				}else {
//					if (obj instanceof String){
//						this.isSubjectLogin = Boolean.parseBoolean((String)obj);
//					}
//				}
//				if (ac== null){
//					ac = AdminClientFactory.createAdminClient(properties);
//				}
//			}
//		}
	}
//	private static Object doAsAdmin(PrivilegedAction action) throws Exception {
//		return WSSubject.doAs(getLoginSubject(), action);
//	}
//
//	private static Subject getLoginSubject() throws Exception {
//		CallbackHandler callbackHandler = new WSCallbackHandlerImpl(properties.getProperty(AdminClient.USERNAME), null,
//				properties.getProperty(AdminClient.PASSWORD));
//		LoginContext loginContext;
//		try {
//			loginContext = new LoginContext("WSLogin", callbackHandler);
//			loginContext.login();
//		} catch (Exception e) {
//			throw new RuntimeException(e.getMessage());
//		}
//		return loginContext.getSubject();
//	}
//	private Set<ObjectName> getQueueListObjectNames(String serverName) throws Exception {
//		final ObjectName objectName = new ObjectName("WebSphere:type=SIBQueuePoint,process="+serverName+",*");
////		final QueryExp queryExp = Query.eq(Query.attr("process"), Query.value(serverName));
//		
//		PrivilegedAction action = new PrivilegedAction(){
//			@Override
//			public Object run() {
//				try {
//					return ac.queryNames(objectName, null);
//				} catch (Exception e) {
//					//e.printStackTrace();
//				}
//				return null;
//			}
//		};
//		
//		Set<ObjectName> objectNameSet = null;
//		if(isSubjectLogin){
//			objectNameSet = (Set) doAsAdmin(action);
//		}else{
//			objectNameSet = (Set) action.run();
//		}
//		
//		return objectNameSet;
//	}
//	private HashMap<String,String[]> getCount(ObjectName objectName) throws Exception{
//		String process = objectName.getKeyProperty("process");
//		String name    = objectName.getKeyProperty("name").replaceAll("."+process, "");
//		
//		HashMap<String,String[]> result = new HashMap<String,String[]>();
//		PrivilegedAction action = new PrivilegedAction(){
//			@Override
//			public Object run() {
//				try {
//					return ac.getAttributes(objectName, new String[] { "depth" });
//				} catch (Exception e) {
//					//e.printStackTrace();
//				}
//				return null;
//			}
//		};
//		
//		AttributeList attrList = null;
//		if(isSubjectLogin){
//			attrList = (AttributeList) doAsAdmin(action);
//		}else{
//			attrList = (AttributeList) action.run();
//		}
//		if (attrList == null){
//			return result;
//		}
//		Iterator it = attrList.iterator();
//		while(it.hasNext()){
//			Attribute a  = (Attribute)it.next();
//			if ("depth".equals(a.getName())){
//				if (a.getValue() instanceof Long ){
//					Long l = (Long)a.getValue();
//					String[] info = new String[9];
//					info[0] = name;
//					info[1] = "queue";//jmsDestinationRuntimes[j].getDestinationType();
//					info[2] = "0";//jmsDestinationRuntimes[j].getConsumersCurrentCount();
//					info[3] = "0";//jmsDestinationRuntimes[j].getConsumersHighCount();
//					info[4] = "0";//jmsDestinationRuntimes[j].getConsumersTotalCount();
//					info[5] = Long.toString(l);
//					info[6] = "0";//jmsDestinationRuntimes[j].getMessagesHighCount();
//					info[7] = "0";//jmsDestinationRuntimes[j].getMessagesPendingCount();
//					info[8] = "0";//jmsDestinationRuntimes[j].getMessagesReceivedCount();
//					result.put(name, info);
//				}
//			}		
//		}
//		return result;
//	}
//	
	public Object getState(String serverName) throws Exception {
		LinkedHashMap<String,String[]> result = new LinkedHashMap<String,String[]>();
//		Set<ObjectName> objectNames = getQueueListObjectNames(serverName);
//		if (objectNames == null){
//			return result;
//		}
//		
//		List<ObjectName> list = objectNames.stream().collect(Collectors.toList());
//		Collections.sort(list, new Comparator<ObjectName>() {
//			@Override
//			public int compare(ObjectName o1, ObjectName o2) {
//				String s1 = (String) o1.getKeyProperty("name");
//				String s2 = (String) o2.getKeyProperty("name");
//				return s1.compareTo(s2);
//			}
//		});
//		
//		for(ObjectName objectName : list){
//			String name = objectName.getKeyProperty("name");
//			if (name.startsWith("_")){
//				continue;
//			}
//			HashMap<String,String[]> m = getCount(objectName);
//			result.putAll(m);
//		}
		return result;
	}


}
