package cn.fateverse.workflow.process;

import cn.fateverse.workflow.entity.bpmn.FormItem;
import cn.fateverse.workflow.enums.FormItemEnum;
import cn.fateverse.workflow.enums.FormPermEnum;
import cn.fateverse.workflow.entity.FormPerm;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 表单工具类
 *
 * @author Clay
 * @date 2022/12/25
 */
public class FormItemParseUtil {


    /**
     * 检查表单
     *
     * @param formItems   需要简单的对象
     * @param fromPermMap 表单权限数据
     * @param formData    表单数据
     * @return 检查完成后的表单数据
     */
    public static List<FormItem> checkFromItemPermsNew(List<FormItem> formItems, Map<String, FormPerm> fromPermMap, JSONObject formData) {
        return checkFromItemPermsNew(formItems, fromPermMap, formData, false);
    }

    public static JSONArray checkFromItemPerms(JSONArray objects, Map<String, FormPerm> fromPermMap, JSONObject formData) {
        return checkFromItemPerms(objects, fromPermMap, formData, false);
    }

    /**
     * 检查表单
     *
     * @param formItems   需要简单的对象
     * @param fromPermMap 表单权限数据
     * @param formData    表单数据
     * @param read        当表单没有对应的权限对象是,指定是否是可读权限,true 可读, false 可编辑
     * @return 检查完成后的表单数据
     */
    public static List<FormItem> checkFromItemPermsNew(List<FormItem> formItems, Map<String, FormPerm> fromPermMap, JSONObject formData, Boolean read) {
        List<FormItem> result = new ArrayList<>();
        //判断当前传参是否为空的list
        if (null == formItems || formItems.isEmpty()){
            return new ArrayList<>();
        }
        for (FormItem formItem : formItems) {
            boolean flag = true;
            //如果是布局组件则需要递归
            if (FormItemEnum.SpanLayout.getName().equals(formItem.getName())){
                //获取到配置信息
                JSONObject props = formItem.getProps();
                //获取到布局下的items
                List<FormItem> items = props.getList("items",FormItem.class);
                //递归检查items
                List<FormItem> resultItems = checkFromItemPermsNew(items, fromPermMap, formData, read);
                props.put("items",resultItems);
                formItem.setProps(props);
            }else {
                //进行权限检查
                String id = formItem.getId();
                FormPerm formPerm = fromPermMap.get(id);
                if (null == formPerm) {
                    if (read) {
                        formItem.setPerm(FormPermEnum.READ.getType());
                    } else {
                        formItem.setPerm(FormPermEnum.EDIT.getType());
                    }
                }else {
                    switch (formPerm.getPerm()) {
                        //编辑和可读状态进行权限赋值
                        case EDIT:
                        case READ:
                            formItem.setPerm(formPerm.getPerm().getType());
                            break;
                        //影藏则同上,将表单数据清除,当前表单组件不显示
                        case HIDE:
                            if (null != formData) {
                                formData.remove(id);
                            }
                            flag = false;
                            break;
                        default:
                            break;
                    }
                }
            }
            if (flag){
                result.add(formItem);
            }
        }
        return result;
    }

    public static JSONArray checkFromItemPerms(JSONArray objects, Map<String, FormPerm> fromPermMap, JSONObject formData, Boolean read) {
        //判断当前传参是否为空的list
        if (null == objects || objects.isEmpty()) {
            return null;
        }
        //构建需要返回的对象
        JSONArray result = new JSONArray();
        for (int i = 0; i < objects.size(); i++) {
            //获取到当前需要检查的对象
            JSONObject fromItem = objects.getJSONObject(i);
            String id = fromItem.getString("id");
            //如果是布局组件则需要递归
            if (FormItemEnum.SpanLayout.getName().equals(fromItem.getString("name"))) {
                //获取到配置信息
                JSONObject props = fromItem.getJSONObject("props");
                //获取到布局下的items
                JSONArray items = props.getJSONArray("items");
                //递归检查items
                JSONArray resultItems = checkFromItemPerms(items, fromPermMap, formData, read);
                //如果为空,则清除当前的组件
                if (null != resultItems && !resultItems.isEmpty()) {
                    //不为空则更新当前的组件信息
                    result.addAll(resultItems);
                }
            } else {
                //进行权限检查
                FormPerm formPerm = fromPermMap.get(id);
                if (null == formPerm) {
                    if (read) {
                        fromItem.put("perm", FormPermEnum.READ.getType());
                    } else {
                        fromItem.put("perm", FormPermEnum.EDIT.getType());
                    }
                    result.add(fromItem);
                    continue;
                }
                switch (formPerm.getPerm()) {
                    //编辑和可读状态进行权限赋值
                    case EDIT:
                    case READ:
                        fromItem.put("perm", formPerm.getPerm().getType());
                        result.add(fromItem);
                        break;
                    //影藏则同上,将表单数据清除,当前表单组件不显示
                    case HIDE:
                        if (null != formData) {
                            formData.remove(id);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        return result;
    }

    /**
     * 检查表单的最新权限
     *
     * @param formData    表单数据
     * @param formDataOld 历史表单数据
     * @param fromPermMap 表单权限数据
     * @return 校验完成的表单数据
     */
    public static JSONObject checkFromDataPrem(JSONObject formData, JSONObject formDataOld, Map<String, FormPerm> fromPermMap) {
        for (String key : formData.keySet()) {
            FormPerm formPerm = fromPermMap.get(key);
            if (null != formPerm && formPerm.getPerm() == FormPermEnum.EDIT) {
                formDataOld.remove(key);
                formDataOld.put(key, formData.get(key));
            }
        }
        return formDataOld;
    }

}
