package com.hzs.common.core.utils;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.*;

/**
 * description: 将json串转化为map类型
 *
 * @author: sui q
 * @time: 2023/4/3 14:17
 * @classname: StringJsonUtils
 * @package_name: com.hzs.common.core.utils
 * version 1.0.0
 */
public class StringJsonUtils {

    public static Map<String, Object> jsonStringToMap(String jsonString) {
        Map<String, Object> map = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        for (Object k : jsonObject.keySet()) {
            Object o = jsonObject.get(k);
            if (o instanceof JSONArray) {
                List<Map<String, Object>> list = new ArrayList<>();
                for (Object obj : (JSONArray) o) {
                    list.add(jsonStringToMap(obj.toString()));
                }
                map.put(k.toString(), list);
            } else if (o instanceof JSONObject) {
                // 如果内层是json对象的话，继续解析
                map.put(k.toString(), jsonStringToMap(o.toString()));
            } else {
                // 如果内层是普通对象的话，直接放入map中
                // map.put(k.toString(), o.toString().trim());
                if (o instanceof String) {
                    map.put(k.toString(), o.toString().trim());
                } else {
                    map.put(k.toString(), o);
                }
            }
        }
        return map;
    }

    public static JSONArray jsonArrayStrTrim(String jsonStr) {
        JSONArray jsonArray = JSONArray.parseArray(jsonStr);
        if (jsonArray != null && jsonArray.size() > 0) {
            for (int j = 0; j < jsonArray.size(); j++) {
//        JSONObject reagobj = JSONObject.parseObject(jsonStr);
                Object object = jsonArray.get(j);
                if(object instanceof String){
                    jsonArray.set(j, jsonArray.get(j).toString().trim());
                }else{
                    if(jsonArray.get(j) instanceof JSONObject){
                        JSONObject reagobj = (JSONObject) jsonArray.get(j);
                        handleJsonObject(reagobj);
                        jsonArray.set(j, reagobj);
                    }
                }
            }
        }
        return jsonArray;
    }

    public static JSONObject jsonObjectStrTrim(String jsonStr) {
        JSONObject reagobj = JSONObject.parseObject(jsonStr);
        handleJsonObject(reagobj);
        return reagobj;
    }

    private static void handleJsonObject(JSONObject reagobj) {
        Set<String> keySet = reagobj.keySet();
        Iterator<String> itt = keySet.iterator();
        while (itt.hasNext()) {
            String key = itt.next();
            Object obj = reagobj.get(key);
            if (obj instanceof JSONObject) {
                Set<String> keySets = ((JSONObject) obj).keySet();
                Iterator<String> iterator = keySets.iterator();
                while (iterator.hasNext()) {
                    String key1 = iterator.next();
                    Object o = ((JSONObject) obj).get(key1);
                    if (o instanceof String) {
                        if (StringUtils.isNotEmpty(o.toString().trim())) {
                            ((JSONObject) obj).put(key1, o.toString().trim());
                        }
                    }
                }
                reagobj.put(key, obj);
            } else if (obj instanceof JSONArray) {
                for (int i = 0; i < ((JSONArray) obj).size(); i++) {
                    Object o = ((JSONArray) obj).get(i);
                    if (o instanceof JSONObject) {
                        Set<String> keySets = ((JSONObject) o).keySet();
                        Iterator<String> iterator = keySets.iterator();
                        while (iterator.hasNext()) {
                            String key1 = iterator.next();
                            Object o1 = ((JSONObject) o).get(key1);
                            if (o1 instanceof String) {
                                if (StringUtils.isNotEmpty(o.toString().trim())) {
                                    ((JSONObject) o).put(key1, o1.toString().trim());
                                }
                            }
                        }
                    } else if (o instanceof String) {
                        if (StringUtils.isNotEmpty(o.toString().trim())) {
                            ((JSONArray) obj).set(i, ((String) o).trim());
                        }
                    }
                }
                reagobj.put(key, obj);
            } else if (obj instanceof String) {
                if (StringUtils.isNotEmpty(obj.toString().trim())) {
                    reagobj.put(key, obj.toString().trim());
                }
            }
        }
    }
}
