package com.palacesun.masking.tools;

import com.palacesun.engine.common.ClientUtil;
import com.palacesun.engine.gateway.EncryptionGatewayManager;
import com.alibaba.fastjson.JSON;
import com.palacesun.masking.vo.Columninfo;
import java.util.HashMap;
import java.util.Map;

public class RemoteUtil {
   static String REMOTE_URI;
   static boolean PUBLISH = true;
   static String PROJECT_ID;

   public static String encryptEcb(String encryptKey, String content, String isLike, String keepfirst, String encdigit, String twoindex, String type, Columninfo info, String mode) throws Exception {
      return invoke(true, encryptKey, content, isLike, keepfirst, encdigit, twoindex, type, info, mode);
   }

   public static String decryptEcb(String decryptKey, String content, String isLike, String keepfirst, String encdigit, String twoindex, String type, Columninfo info, String mode) throws Exception {
      return invoke(false, decryptKey, content, isLike, keepfirst, encdigit, twoindex, type, info, mode);
   }

   public static String invoke(boolean isEnc, String key, String content, String isLike, String keepfirst, String encdigit, String twoindex, String type, Columninfo info, String mode) throws Exception {
      Map<String, String> params = new HashMap();
      params.put("api", isEnc ? "enc" : "dec");
      params.put("key", key);
      params.put("content", content);
      params.put("isLike", isLike);
      params.put("type", type);
      params.put("mode", mode);
      params.put("project", PROJECT_ID);
      params.put("keepfirst", keepfirst);
      params.put("encdigit", encdigit);
      params.put("twoindex", twoindex);
      if (info != null) {
         params.put("fid", info.getId());
      }

      String result = invoke(params);
      if (result != null && result.startsWith("{{ERR:")) {
         throw new Exception(result.substring("{{ERR:".length()));
      } else {
         return result;
      }
   }

   static String invoke(Map<String, String> params) throws Exception {
      String data = JSON.toJSONString(params);
      String result = ClientUtil.doPost(REMOTE_URI, data);
      if (result != null && result.startsWith("{{ERR:")) {
         throw new Exception(result.substring("{{ERR:".length()));
      } else {
         return result;
      }
   }

   public static void main(String[] args) throws Exception {
      PUBLISH = false;
      REMOTE_URI = "https://localhost:8181/data/api/remote/invoke";
      String text = "hello,世界！1";
      String[] types = new String[]{"SM4"};
      String[] keys = new String[]{"E20D0F3FBF8DCAB88C6A335CA944CD65"};

      for(int i = 0; i < types.length; ++i) {
         String type = types[i];
         String key = keys[i];
         String enc = invoke(true, key, text, "", "0", "2", "0", type, (Columninfo)null, null);
         System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
         System.out.println(String.format("[][%s]%s->%s", type, text, enc));
         String dec = invoke(false, key, enc, "", "0", "2", "0", type, (Columninfo)null, null);
         System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
         System.out.println(String.format("[][%s]%s->%s", type, enc, dec));
         enc = invoke(true, key, text, "1", type, "0", "2", "0", (Columninfo)null, null);
         System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
         System.out.println(String.format("[1][%s]%s->%s", type, text, enc));
         dec = invoke(false, key, enc, "1", type, "0", "2", "0", (Columninfo)null, null);
         System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
         System.out.println(String.format("[1][%s]%s->%s", type, enc, dec));
      }

   }

   static String queryCcoreSDFKey(Map<String, String> params) throws Exception {
      String data = JSON.toJSONString(params);
      String result = ClientUtil.doPost("/data/api/remote/queryCcoreSDFKey", data);
      if (result != null && result.startsWith("{{ERR:")) {
         throw new Exception(result.substring("{{ERR:".length()));
      } else {
         return result;
      }
   }

   static {
      try {
         PROJECT_ID = EncryptionGatewayManager.getParameterValue("projectid");
      } catch (Exception e) {
         e.printStackTrace();
      }

      if (PUBLISH) {
         try {
            REMOTE_URI = EncryptionGatewayManager.getParameterValue("gatewayurl");
            if (!REMOTE_URI.endsWith("/")) {
               REMOTE_URI = REMOTE_URI + "/data/api/remote/invoke";
            } else {
               REMOTE_URI = REMOTE_URI + "data/api/remote/invoke";
            }
         } catch (Exception e) {
            e.printStackTrace();
         }
      }

   }
}
