/*
 * 文 件 名:  JsonObj
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  <描述>
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/9/28
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.huashe.common.domain;

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

import java.util.Map;

/**
 * JsonEntity
 * <功能详细描述>
 *
 * @author lvwenbin
 * @version [版本号, 2025/9/28]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class JsonEntity<T> {

    private final T jsonObj;

    // 私有构造方法，只能通过Builder创建对象
    private JsonEntity(JsonEntity.ObjBuilder builder) {
        this.jsonObj = (T) builder.jsonObject;
    }

    private JsonEntity(JsonEntity.ArrayBuilder builder) {
        this.jsonObj = (T) builder.jsonArray;
    }

    public T getJsonObj() {
        return jsonObj;
    }

    // Builder类
    public static class ObjBuilder {
        private JSONObject jsonObject = new JSONObject();

        public ObjBuilder() {
        }

        public JsonEntity.ObjBuilder putKv(String key, Object value) {
            this.jsonObject.put(key, value);
            return this;
        }

        public JsonEntity.ObjBuilder putSubKv(String key, String subKey, Object value) {
            JSONObject tmpObj = this.jsonObject.getJSONObject(key);
            tmpObj.put(subKey, value);
            return this;
        }

        public JsonEntity.ObjBuilder putMap(Map<? extends String, ?> map) {
            jsonObject.putAll(map);
            return this;
        }

        public JsonEntity.ObjBuilder addKeyArray(String key, Object value) {
            JSONArray tmpArr = this.jsonObject.getJSONArray(key);

            if (tmpArr == null) {
                tmpArr = new JSONArray();
                this.jsonObject.put(key, tmpArr);
            }

            tmpArr.add(value);
            return this;
        }

        /**
         * 设置resPayload
         */
        public JsonEntity.ObjBuilder set(JSONObject jsonObject) {
            this.jsonObject = jsonObject;
            return this;
        }

        /**
         * 构建CallData对象
         */
        public JsonEntity<JSONObject> build() {
            return new JsonEntity<>(this);
        }
    }

    // Builder类
    public static class ArrayBuilder {
        private JSONArray jsonArray = new JSONArray();

        public ArrayBuilder() {
        }

        public JsonEntity.ArrayBuilder addKv(int index, String key, Object value) {
            JSONObject jsonObject = this.jsonArray.getJSONObject(index);
            jsonObject.put(key, value);
            return this;
        }

        public JsonEntity.ArrayBuilder addKv(String matchKey, String matchVal, String setKey, Object setValue) {
            jsonArray.forEach(obj -> {
                JSONObject tmpObj = (JSONObject) obj;
                if (tmpObj.getString(matchKey).equals(matchVal)) {
                    tmpObj.put(setKey, setValue);
                }
            });
            return this;
        }

        /**
         * 构建CallData对象
         */
        public JsonEntity<JSONArray> build() {
            return new JsonEntity<>(this);
        }
    }

    // 用于获取Builder实例的静态方法
    public static JsonEntity.ObjBuilder objBuilder() {
        return new JsonEntity.ObjBuilder();
    }

    public static JsonEntity.ArrayBuilder arraybuilder() {
        return new JsonEntity.ArrayBuilder();
    }
}