package com.ruoyi.web.controller.tool;


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

import java.util.List;

public class FormWidgetProcessor {
    // 支持禁用的组件类型列表
    private static final String[] DISABLED_TYPES = {
            "input", "select", "textarea", "radio-group",
            "checkbox-group", "date-picker", "time-picker",
            "upload", "picture-upload", "cascader"
    };
    public static void main(String[] args) {
        // 模拟构造包含表单数据的 parameters 对象，实际使用时替换为真实业务数据
        JSONObject parameters = new JSONObject();
        String formJsonStr = "{\"widgetList\":[{\"type\":\"grid\",\"category\":\"container\",\"icon\":\"grid\",\"cols\":[{\"type\":\"grid-col\",\"category\":\"container\",\"icon\":\"grid-col\",\"internal\":true,\"widgetList\":[{\"type\":\"input\",\"icon\":\"text-field\",\"formItemFlag\":true,\"options\":{\"name\":\"input37742\",\"label\":\"input1\",\"disabled\":false}},{\"type\":\"select\",\"icon\":\"select-field\",\"formItemFlag\":true,\"options\":{\"name\":\"select123\",\"label\":\"select示例\",\"disabled\":false}}]}]}]}";
        /*parameters.put("formJson", JSON.parseObject(formJsonStr));

        // 解析获取 oldVariables 和 oldFields
        JSONObject oldVariables = JSONObject.parseObject(JSON.toJSONString(parameters.get("formJson")));
        List<com.alibaba.fastjson2.JSONObject> oldFields = JSON.parseObject(JSON.toJSONString(oldVariables.get("widgetList")), new TypeReference<List<JSONObject>>() {});
       */
        /*JSONObject oldVariables = JSONObject.parseObject(formJsonStr);
        List<JSONObject> oldFields = JSON.parseObject(
                JSON.toJSONString(oldVariables.get("widgetList")),
                new TypeReference<List<JSONObject>>() {}
        );*/
        parameters.put("formJson", JSON.parseObject(formJsonStr));

        // 获取原始对象引用（关键修改点）
        JSONObject oldVariables = parameters.getJSONObject("formJson");
        JSONArray widgetList = oldVariables.getJSONArray("widgetList");
        // 直接处理原始 JSONArray
        processWidgetList(widgetList);

        // 输出处理后的原始对象（关键修改点）
        //System.out.println("处理后的结果: " + JSON.toJSONString(oldVariables, true));
        // 处理设置禁用状态
        //processWidgets(oldFields);
        System.out.print(widgetList);
        // 输出处理后的结果，实际可根据业务需求写回原数据结构或其他操作
        //System.out.println("处理后的 oldFields: " + JSON.toJSONString(oldFields, true));
    }

    // 递归处理 JSONArray（关键修改点：参数类型改为 JSONArray）
    private static void processWidgetList(JSONArray widgetList) {
        if (widgetList == null) return;

        for (Object obj : widgetList) {
            if (obj instanceof JSONObject) {
                JSONObject widget = (JSONObject) obj;
                String type = widget.getString("type");

                if ("grid".equals(type)) {
                    JSONArray cols = widget.getJSONArray("cols");
                    processWidgetList(cols);
                } else if ("grid-col".equals(type)) {
                    JSONArray innerWidgets = widget.getJSONArray("widgetList");
                    processWidgetList(innerWidgets);
                } else if ("input".equals(type) || "select".equals(type)) {
                    setDisabled(widget);
                }
            }
        }
    }

    private static void setDisabled(JSONObject component) {
        JSONObject options = component.getJSONObject("options");
        if (options == null) {
            options = new JSONObject();
            component.put("options", options);
        }
        options.put("disabled", true);
    }
}
