package com.inspect.ivs.util;

import com.alibaba.fastjson.JSONObject;
import com.inspect.ivs.constant.IvsConst;
import com.inspect.ivs.vo.IvsDevChanSnapVo;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class UriUtils {
   public static String parse(String uri, Object params) {
      JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(params));
      StringBuilder sb = new StringBuilder("?");

      String key;
      for(Iterator<String> iterator = json.keySet().iterator(); iterator.hasNext(); sb.append(key).append("=").append(json.get(key)).append("&")) {
         key = iterator.next();
         if(uri.contains(key)) {
            uri = uri.replace("{" + key + "}", json.getString(key));
         }
      }

      String newUri = uri + sb.substring(0, sb.length() - 1);
      //System.out.println("================uri:" + newUri);
      return newUri;
   }

    public static String parseIgnored(String uri, Object params, Set<String> ignoredKeys) {
        JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(params));
        StringBuilder sb = new StringBuilder("?");
        String key;
        for (String s : json.keySet()) {
            key = s;
            // 如果 URI 模板中包含 {key}，替换它
            if (uri.contains("{" + key + "}")) {
                uri = uri.replace("{" + key + "}", json.getString(key));
            }
            // 如果是需要忽略的 key，则跳过后续拼接
            else if (!ignoredKeys.contains(key)) {
                sb.append(key).append("=").append(json.get(key)).append("&");
            }
        }

        // 如果没有 query 参数，则直接返回替换后的 uri
        if (sb.length() == 1) {
            return uri;
        }

        String newUri = uri + sb.substring(0, sb.length() - 1); // 去掉末尾的 "&"
        return newUri;
    }


    public static String parseForReplace(String uri, Object params) {
      JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(params));
      StringBuilder sb = new StringBuilder("?");
       for (String key : json.keySet()) {
           if (uri.contains(key)) {
               uri = uri.replace("{" + key + "}", json.getString(key));
           }
       }

      String newUri = uri + sb.substring(0, sb.length() - 1);
      //System.out.println("================uri:" + newUri);
      return newUri;
   }

   public static void main(String[] args) {

       IvsDevChanSnapVo ivsDevChanSnapVo = new IvsDevChanSnapVo();
       ivsDevChanSnapVo.setId("123456");
       ivsDevChanSnapVo.setName("ssssss");
       ivsDevChanSnapVo.setUUID("d9a1cad491f94756877cc2d3088c0138");
       ivsDevChanSnapVo.setCameraCode("01969538356559120106");
       ivsDevChanSnapVo.setDomainCode("425b88eb55ab4bcfae2c4efe1bd6df88");
       ivsDevChanSnapVo.setRealtime(true);
       ivsDevChanSnapVo.setCode("01969538356559120106#425b88eb55ab4bcfae2c4efe1bd6df88");
       ivsDevChanSnapVo.setSerial("c5486fffb1b64e53978bd6d82d96109f");
       ivsDevChanSnapVo.setTimeout(false);
       ivsDevChanSnapVo.setCheck_outputs(false);

       Set<String> ignoredKeys = new HashSet<>();
       ignoredKeys.add("UUID");
       ignoredKeys.add("uUID");
       ignoredKeys.add("presetName");
       ignoredKeys.add("patrolPointId");
       final String uri = UriUtils.parseIgnored(IvsConst.URI_PLATFORM_SNAPSHOT, ivsDevChanSnapVo, ignoredKeys);
       System.out.println("uri: " + uri);
   }
}
