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

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

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.eactive.eai.rms.onl.common.service.OnlAgentUtilServiceImpl;

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1EndpointAddress;
import io.kubernetes.client.openapi.models.V1EndpointSubset;
import io.kubernetes.client.openapi.models.V1Endpoints;
import io.kubernetes.client.util.ClientBuilder;

public class K8sUtil {
   private static final Logger logger = Logger.getLogger(K8sUtil.class);
   
    public static List<Map<String, String>> getServerInfoByK8sUrl(String namespace, String serviceName, int port, String extUrl) throws Exception {
       
       List<Map<String, String>> resultList = getServerInfoByK8sApi(namespace,serviceName);
       
       for (Map<String,String> map : resultList) {
          String EAISEVRINSTNCNAME = map.get("EAISEVRINSTNCNAME");
          String EAISEVRIP = map.get("EAISEVRIP"); 
          Map<String, String> serverInfo = new HashMap<>();
          
         serverInfo.put(OnlAgentUtilServiceImpl.FIELD_URL, new StringBuilder().append("http://" + EAISEVRIP + ":"+String.valueOf(port) + extUrl).toString());
         serverInfo.put(OnlAgentUtilServiceImpl.FIELD_INST_NAME, EAISEVRINSTNCNAME);
       }
       
       return resultList;
    }
    public static List<Map<String, String>> getServerInfoByK8sApi(String namespace, String serviceName) throws Exception {
       
       logger.info("K8sAgentUtilService getServerInfoByK8sApi start.");
       
       List<Map<String, String>> resultList = new ArrayList<>();
       
       try {
          ApiClient client = ClientBuilder.cluster().build();
          Configuration.setDefaultApiClient(client);
          
          if (StringUtils.isBlank(namespace)) {
             new RuntimeException("Can't find kubernetes_namespace in Engine Property");
          }
          if (StringUtils.isBlank(serviceName)) {
             new RuntimeException("Can't find kubernetes_servicename in Engine Property");
          }
          
          logger.info("getServerInfoByK8sApi Codes [ namespace: " + namespace + ", serviceName:" + serviceName + " ]");
          
          V1Endpoints endpoints = new CoreV1Api().readNamespacedEndpoints(serviceName, namespace, null);
          
          for (V1EndpointSubset subset : endpoints.getSubsets()) {
             for (V1EndpointAddress address : subset.getAddresses()) {
                Map<String, String> serverInfo = new HashMap<>();
                serverInfo.put("EAISEVRINSTNCNAME", address.getTargetRef().getName());
                serverInfo.put("EAISEVRIP", address.getIp());
                resultList.add(serverInfo);
             }
          }
          
          logger.info("getServerInfoByK8sApi resultList [ " + resultList.toString() + " ]");
          
       } catch (Exception e) {
          logger.error(e);
          throw new RuntimeException("K8S Mode로 AgentUtilService를 기동중 실패하였습니다.", e);
       }
       
       return resultList;
    }
    
}