package com.eactive.eai.adapter.http;

import com.eactive.eai.adapter.*;
import com.eactive.eai.common.property.PropManager;
import com.eactive.eai.common.testcall.TestCallException;
import com.eactive.eai.common.testcall.TestCallManager;
import com.eactive.eai.common.testcall.TestCallSupport;
import com.eactive.eai.common.util.Logger;

import java.util.Properties;

public class HttpAdapterTestCall extends TestCallSupport
{ 
    public static final String MESSAGE = "EAI TestCall Message";
    static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
    
    public boolean isAlive() throws TestCallException
    {
//        int nConnected  = 0 ;
        try {
	        ElinkAdapterFactory factory = ElinkAdapterFactory.newInstance();
	        ElinkAdapter adapter = factory.getElinkAdapter(Keys.TYPE_HTTP);
	        AdapterVO vo = this.getAdapterInfo();
	        String pgoName = vo.getPropGroupName();
	        //PropManager manager = PropManager.getInstance();
	        AdapterPropManager manager = AdapterPropManager.getInstance();
	        Properties prop = (Properties)(manager.getProperties(pgoName)).clone();
	        
	        String testCallUrl = (String)prop.get("TEST_CALL_URL");
	        
	        // MFC_FAILOVER : FAILOVER = Y 일 경우 무조건 2센터로 테스트콜을 처리한다 (2020-10-16)
            PropManager pmanager = PropManager.getInstance();
            String mfcFailOver = pmanager.getProperty("MFC_FAILOVER", "FAILOVER");
            
            if(mfcFailOver != null && "Y".equals(mfcFailOver.toUpperCase())) {
            	String testCallUrl2Grp = (String)prop.get("TEST_CALL_URL_2GRP");
            	if (testCallUrl2Grp != null && testCallUrl2Grp.trim().length() > 0) {
            		testCallUrl = testCallUrl2Grp;
            		if (logger.isWarn()) logger.warn(pgoName + " - USE TEST_CALL_URL_2GRP - " + testCallUrl );
            	}
            }
            // MFC_FAILOVER : FAILOVER = Y 일 경우 무조건 2센터로 테스트콜을 처리한다 (2020-10-16)
            
            if (testCallUrl != null && testCallUrl.trim().length() > 0) {
	        	prop.put("URL", testCallUrl);
	        }
	       
	        Properties testCallProp  = PropManager.getInstance().getProperties(TestCallManager.PROP_GROUP);
	        String responseType      = testCallProp.getProperty(TestCallManager.RESPONSE_TYPE, "SYNC");
	        String httpTimeout       = testCallProp.getProperty(TestCallManager.HTTP_TIME_OUT, "5000");
	        String connectionTimeout = testCallProp.getProperty(TestCallManager.CONNECTION_TIMEOUT, "3000");
	        
	        prop.setProperty(TestCallManager.RESPONSE_TYPE,responseType);
	        prop.setProperty(TestCallManager.HTTP_TIME_OUT, httpTimeout);
	        prop.setProperty(TestCallManager.CONNECTION_TIMEOUT, connectionTimeout);
	        Properties tempProp = new Properties();
	        tempProp.setProperty(TestCallManager.TESTCALL_YN, "Y");
            
	        if(logger.isWarn()) {
            	logger.warn("HttpAdapterTestCall] TestCall ["+vo.getAdapterGroupName()+"] URL = " + prop.getProperty("URL") );
            	logger.warn("HttpAdapterTestCall] TestCall ["+vo.getAdapterGroupName()+"] " + TestCallManager.RESPONSE_TYPE +" = " + prop.getProperty(TestCallManager.RESPONSE_TYPE) );
            	logger.warn("HttpAdapterTestCall] TestCall ["+vo.getAdapterGroupName()+"] " + TestCallManager.HTTP_TIME_OUT +" = " + prop.getProperty(TestCallManager.HTTP_TIME_OUT) );
            	logger.warn("HttpAdapterTestCall] TestCall ["+vo.getAdapterGroupName()+"] " + TestCallManager.CONNECTION_TIMEOUT +" = " + prop.getProperty(TestCallManager.CONNECTION_TIMEOUT) );
            	logger.warn("HttpAdapterTestCall] TestCall ["+vo.getAdapterGroupName()+"] " + TestCallManager.TESTCALL_YN +" = " + tempProp.getProperty(TestCallManager.TESTCALL_YN) );

            }
	        
            Object result = adapter.callService(vo.getAdapterGroupName(),prop,MESSAGE.getBytes(), tempProp);
            if(logger.isWarn()) {
            	logger.warn("HttpAdapterTestCall] TestCall["+vo.getAdapterGroupName()+"], result ="+ result );
            }
            return true;
        } catch(HttpStatusException hse) {
        	logger.warn("HttpAdapterTestCall] Error - "+hse.getClass().getName()+" : "+hse.getMessage());
        	return true;
        } catch(Exception e) {
            //e.printStackTrace();
            logger.warn("HttpAdapterTestCall] Error - "+e.getClass().getName()+" : "+e.getMessage());
            return false;      
        }
    }
} 
