package kr.co.i4way.genesys.config;


import com.genesyslab.platform.applicationblocks.com.IConfService;
import com.genesyslab.platform.applicationblocks.com.ConfServiceFactory;
import com.genesyslab.platform.applicationblocks.com.ConfigException;

import com.genesyslab.platform.configuration.protocol.types.CfgAppType;
import com.genesyslab.platform.configuration.protocol.ConfServerProtocol;
import com.genesyslab.platform.commons.connection.Connection;
import com.genesyslab.platform.commons.connection.configuration.PropertyConfiguration;
import com.genesyslab.platform.commons.protocol.ChannelState;
import com.genesyslab.platform.commons.protocol.Endpoint;
import com.genesyslab.platform.commons.protocol.ProtocolException;


/**
 * Sample methods for initialization of configuration service instance.
 * 
 * See http://docs.genesys.com/Documentation/PSDK/latest/Developer/UsingtheCOMAB
 *
 */
public class InitializationConfig {

    /**
     * Sample Configuration service initialization function example.
     * 
     *
     * @param cfgsrvEndpointName name for the server connection endpoint
     * @param cfgsrvHost configuration server host name
     * @param cfgsrvPort configuration server port
     * @param username configuration server login username
     * @param password configuration server login password
     * @return initialized configuration service
     * @throws ConfigException in case of exception while service or configuration protocol initialization
     * @throws InterruptedException if process was interrupted
     * @throws ProtocolException exception on protocol connection opening
     */
    public IConfService initializeConfigService(
                final String     cfgsrvEndpointName,
                final String     cfgsrvHost,
                final int        cfgsrvPort,
                final String     username,
                final String     password,
                final String     charset)
            throws ConfigException, InterruptedException, ProtocolException {

        CfgAppType clientType = CfgAppType.CFGSCE;
        String     clientName = "default";

        return initializeConfigService(cfgsrvEndpointName,
                cfgsrvHost, cfgsrvPort,
                clientType, clientName,
                username, password, charset);
    }

    /**
     * Sample Configuration service initialization function example.
     *
     * @param cfgsrvEndpointName name for the server connection endpoint
     * @param cfgsrvHost configuration server host name
     * @param cfgsrvPort configuration server port
     * @param clientType configuration server client application name
     * @param clientName configuration server client application type
     * @param username configuration server login user name
     * @param password configuration server login password
     * @return initialized configuration service
     * @throws ConfigException in case of exception while service or configuration protocol initialization
     * @throws InterruptedException if process was interrupted
     * @throws ProtocolException exception on protocol connection opening
     */
    public IConfService initializeConfigService(
                final String     cfgsrvEndpointName,
                final String     cfgsrvHost,
                final int        cfgsrvPort,
                final CfgAppType clientType,
                final String     clientName,
                final String     username,
                final String     password,
                final String     charset)
            throws ConfigException, InterruptedException, ProtocolException {

    	PropertyConfiguration config = new PropertyConfiguration();
        config.setUseAddp(true);
        config.setAddpClientTimeout(10);
        config.setAddpServerTimeout(10);
        config.setOption(Connection.STR_ATTR_ENCODING_NAME_KEY, charset);
    	
        Endpoint cfgServerEndpoint =
                new Endpoint(cfgsrvEndpointName, cfgsrvHost, cfgsrvPort, config);

        ConfServerProtocol protocol = new ConfServerProtocol(cfgServerEndpoint);
        protocol.setClientName(clientName);
        protocol.setClientApplicationType(clientType.ordinal());
        protocol.setUserName(username);
        protocol.setUserPassword(password);

        IConfService service = ConfServiceFactory.createConfService(protocol);

        protocol.open();
        
        Integer cfgServerEncoding = protocol.getServerContext().getServerEncoding();
        if (cfgServerEncoding != null && cfgServerEncoding.intValue() == 1) {
       	 //System.out.println("UTF-8로 변경");
           protocol.close();
           config.setStringsEncoding("UTF-8");
           protocol.setEndpoint(new Endpoint(cfgsrvEndpointName, cfgsrvHost, cfgsrvPort, config));
           protocol.open();
         } else{
       	  //System.out.println("변경안함");
         }

        return service;
    }

    /**
     * Release ConfService instance created with
     * {@link #initializeConfigService(String, String, int, CfgAppType, String, String, String)}.
     *
     * @param service configuration service reference
     * @throws InterruptedException 
     * @throws IllegalStateException 
     * @throws ProtocolException 
     */
    public void uninitializeConfigService(
                final IConfService service) throws ProtocolException, IllegalStateException, InterruptedException {
        if (service.getProtocol().getState() != ChannelState.Closed) {
            service.getProtocol().close();
        }
        ConfServiceFactory.releaseConfService(service);
    }
}
