package cn.stylefeng.workflow.design.modular.factory;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.team.form.api.entity.FormItem;
import cn.stylefeng.team.form.api.pojo.FormDesignVo;
import cn.stylefeng.workflow.api.entity.design.Node;
import cn.stylefeng.workflow.api.pojo.config.node.NodeData;
import cn.stylefeng.workflow.api.pojo.config.node.form.FieldPermission;
import cn.stylefeng.workflow.api.pojo.config.node.form.FormPermission;

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

public class NodeFromDesignFactory {

    public static List<FormDesignVo> fillNodeFormDesign(Node nodeInfo, List<FormDesignVo> originFormDesign) {

        NodeData nodeData = nodeInfo.getNodeData();
        if (ObjectUtil.isEmpty(nodeData)) {
            return originFormDesign;
        }

        List<FormPermission> formPermission = nodeData.getFormPermission();
        if (ObjectUtil.isEmpty(formPermission)) {
            return originFormDesign;
        }


        for (FormPermission nodeFormPermission : formPermission) {

            for (FormDesignVo originFormDesignItem : originFormDesign) {

                if (nodeFormPermission.getFormDefinitionId().equals(originFormDesignItem.getFormDefinitionId())) {

                    List<FieldPermission> fieldPermissionList = nodeFormPermission.getFieldPermissionList();

                    List<FormItem> originFormDesignItemFormItemList = originFormDesignItem.getFormItemList();

                    fillFormItemFieldPermission(fieldPermissionList, originFormDesignItemFormItemList);
                }
            }
        }

        return originFormDesign;
    }

    public static void fillFormItemFieldPermission(List<FieldPermission> fieldPermissionList,
                                                   List<FormItem> originFormDesignItemFormItemList) {

        for (FieldPermission fieldPermission : fieldPermissionList) {

            for (FormItem formItem : originFormDesignItemFormItemList) {

                if (ObjectUtil.isEmpty(fieldPermission.getFieldCode()) || ObjectUtil.isEmpty(formItem.getFieldCode())) {
                    continue;
                }

                if (fieldPermission.getFieldCode().equals(formItem.getFieldCode())) {
                    formItem.setItemEditFlag(fieldPermission.getEdit());
                    formItem.setItemRequiredFlag(fieldPermission.getRequire());
                    formItem.setItemHiddenFlag(fieldPermission.getHide());
                }

                List<FieldPermission> subTableFieldPermissionList = fieldPermission.getSubTableFieldPermissionList();
                List<FormItem> formItemChildren = formItem.getChildren();

                if (ObjectUtil.isNotEmpty(subTableFieldPermissionList) && ObjectUtil.isNotEmpty(formItemChildren)) {
                    fillFormItemFieldPermission(subTableFieldPermissionList, formItemChildren);
                }
            }
        }
    }

    public static List<FormDesignVo> ensureHiddenItem(List<FormDesignVo> formDesignVoList) {

        List<FormDesignVo> tempFormDesignVoList = new ArrayList<>();

        for (FormDesignVo formDesignVo : formDesignVoList) {

            boolean existNotHidden = false;

            List<FormItem> formItemList = formDesignVo.getFormItemList();
            for (FormItem formItem : formItemList) {

                if (formItem.getItemHiddenFlag() == null) {
                    formItem.setItemHiddenFlag(false);
                }
                if (formItem.getItemEditFlag() == null) {
                    formItem.setItemEditFlag(false);
                }
                if (formItem.getItemRequiredFlag() == null) {
                    formItem.setItemRequiredFlag(false);
                }

                if (!formItem.getItemHiddenFlag()) {
                    existNotHidden = true;
                    break;
                }
            }

            if (existNotHidden) {
                tempFormDesignVoList.add(formDesignVo);
            }
        }

        return tempFormDesignVoList;
    }

}
