package com.eactive.eai.rms.onl.common.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.rms.common.util.CommonUtil;
import com.eactive.eai.rms.onl.server.repository.Repository;
import com.eactive.eai.rms.onl.vo.JmsMonitorVo;

@Service
@Qualifier("jmsQueueMonitorService")
public class JmsQueueMonitorService {

    @Autowired
    @Qualifier("repository")
    private Repository repository;

    /** The eai server info service. */
    @Autowired
    @Qualifier("eaiServerInfoService")
    private EaiServerInfoService eaiServerInfoService;
    
    
    /**
     * 주어진 호스트명, 인스턴스명에 해당하는 JmsQueue 정보 HashMap 을 리턴한다.
     * @param hostName
     * @param instName
     * @return
     */
    public HashMap getDataByHost( String hostName, String instName ) {
        JmsMonitorVo vo = (JmsMonitorVo)repository.getMonitorData(hostName, instName, JmsMonitorVo.class.getName());
        if( vo == null ) {
            return null ;
        } else {
            return vo.getData();
        }
    }
    
    /**
     * 주어진 인스턴스명 에 해당하는 JmsQueue 정보 HashMap 을 리턴한다.
     * @param instName
     * @return
     */
    public HashMap getDataByInstance( String instName ) {
        //DataSourceType service = DataSourceContextHolder.getDataSourceType();
        List<Map<String, String>> list = eaiServerInfoService.getEaiServerIpListDB();
        for( Map<String,String> map : list ) {
            String inst = map.get("EAISVRINSTNM");
            String host = map.get("HOSTNAME");
            
            if( instName.equals(inst) ) {
                return getDataByHost(host, inst);
            }            
        }

        return null ;        
    }
    
    /**
     * 주어진 QueueName 에 해당하는 JmsQueue 배열정보를 리턴한다.
     * 0:Name
     * 1:Type
     * 2,3,4:Consumer(Current,High,Total)
     * 5,6,7,8:Message(Current,High,Pending,Received)
     * @param queueName
     * @return
     */
    public String[] getArrayByQueueName( String queueName ) {
    	//DataSourceType service = DataSourceContextHolder.getDataSourceType();
        List<Map<String, String>> list = eaiServerInfoService.getEaiServerIpListDB();
        for( Map<String,String> map : list ) {
            String inst = map.get("EAISVRINSTNM");
            String host = map.get("HOSTNAME");
            
            HashMap dataMap = getDataByHost(host, inst);
            if( dataMap != null ) {
            	Object arrObj = dataMap.get(queueName);
	            if( arrObj != null ) {
	                return (String[]) arrObj;
	            }            
            }
        }

        return null ;        
    }
    
    
    
    /**
     * 주어진 QueueName ,instncname 에 해당하는 JmsQueue 배열정보를 리턴한다.
     * 0:Name
     * 1:Type
     * 2,3,4:Consumer(Current,High,Total)
     * 5,6,7,8:Message(Current,High,Pending,Received)
     * jun's ALL 추가
     * ALL 이 추가 된건 Kubernetes 에서 ActiveMQ를 사용하기 때문에 ALL로 설정
     * TSEAIQM01,TSEAIQM02 에 값을 넣어야됨 instance에 ALL 
     * @param queueName
     * @return
     */
    public String[] getArrayByInstQueueName( String queueName , String instncname  ) {
        //DataSourceType service = DataSourceContextHolder.getDataSourceType();
         List<Map<String, String>> list = eaiServerInfoService.getEaiServerIpListDB();
         int currentCnt =0;
         for( Map<String,String> map : list ) {
             String inst = map.get("EAISVRINSTNM");
             String host = map.get("HOSTNAME");
             
             if(!"ALL".equals(instncname) && !inst.equals(instncname) ) continue;
             
             HashMap dataMap = getDataByHost(host, inst);
             if( dataMap != null ) {
                Object arrObj = dataMap.get(queueName);
                if( arrObj != null ) {
                   if ("ALL".equals(instncname)) {
                      String[] d = (String[]) arrObj;
                      if (d.length>5) {
                         currentCnt = currentCnt + CommonUtil.getInt(d[5]);
                      }
                   }else {
                       return (String[]) arrObj;
                   }
                }
             }
         }
         if ("ALL".equals(instncname) && currentCnt > 0) {
            try {
             return getCount(queueName,currentCnt);
          } catch (Exception e) {
             // TODO Auto-generated catch block
             return null;
          }
         }

         return null ;        
     }
    private String[] getCount(String queueName, long depth) throws Exception{
       String[] info = new String[9];
       info[0] = queueName;
       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(depth);
       info[6] = "0";//jmsDestinationRuntimes[j].getMessagesHighCount();
       info[7] = "0";//jmsDestinationRuntimes[j].getMessagesPendingCount();
       info[8] = "0";//jmsDestinationRuntimes[j].getMessagesReceivedCount();
       return info;
    }
}
