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

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.team.form.api.FormModelServiceApi;
import cn.stylefeng.team.form.api.entity.FormItem;
import cn.stylefeng.team.form.api.pojo.FormDesignVo;
import cn.stylefeng.workflow.api.constants.VariableConstants;
import cn.stylefeng.workflow.api.enums.CommonVariableEnum;
import cn.stylefeng.workflow.api.pojo.config.model.FlowGlobalConfig;
import cn.stylefeng.workflow.api.pojo.config.model.ModelForm;
import cn.stylefeng.workflow.api.pojo.config.model.ModelVariable;
import cn.stylefeng.workflow.api.pojo.variable.VariableCategory;
import cn.stylefeng.workflow.api.pojo.variable.VariableSelectItem;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class BaseVariablesFactory {

    public static VariableCategory createBaseVariables() {

        VariableCategory variableCategory = new VariableCategory();
        variableCategory.setCategoryName(VariableConstants.COMMON_VARIABLE_NAME);

        ArrayList<VariableSelectItem> variables = new ArrayList<>();

        for (CommonVariableEnum commonVariableEnum : CommonVariableEnum.values()) {
            VariableSelectItem orgVariable = new VariableSelectItem();
            orgVariable.setVariableCode(commonVariableEnum.getVariableCode());
            orgVariable.setVariableName(commonVariableEnum.getName());
            variables.add(orgVariable);
        }

        variableCategory.setVariableInfoList(variables);

        return variableCategory;
    }

    public static VariableCategory createFormVariable(FlowGlobalConfig definitionGlobalConfig) {

        VariableCategory variableCategory = new VariableCategory();
        variableCategory.setCategoryName(VariableConstants.FORM_VARIABLE_NAME);

        ArrayList<VariableSelectItem> variables = new ArrayList<>();

        if (ObjectUtil.isEmpty(definitionGlobalConfig) || ObjectUtil.isEmpty(definitionGlobalConfig.getFormDefinitionList())) {
            variableCategory.setVariableInfoList(variables);
            return variableCategory;
        }

        List<ModelForm> formDefinitionList = definitionGlobalConfig.getFormDefinitionList();

        List<Long> formDefinitionIdList = formDefinitionList.stream().map(ModelForm::getFormDefinitionId).collect(Collectors.toList());

        FormModelServiceApi formModelServiceApi = SpringUtil.getBean(FormModelServiceApi.class);
        List<FormDesignVo> fromDesignList = formModelServiceApi.getFromDesignList(formDefinitionIdList);

        if (ObjectUtil.isEmpty(fromDesignList)) {
            return variableCategory;
        }

        for (FormDesignVo formDesignVo : fromDesignList) {
            List<FormItem> formItemList = formDesignVo.getFormItemList();
            if (ObjectUtil.isEmpty(formItemList)) {
                continue;
            }
            for (FormItem formItem : formItemList) {
                VariableSelectItem variableSelectItem = new VariableSelectItem(formItem.getItemLabelName(), formItem.getFieldCode(),
                        formItem.getItemLabelName());
                variables.add(variableSelectItem);
            }
        }
        variableCategory.setVariableInfoList(variables);

        return variableCategory;
    }

    public static VariableCategory createModelVariables(FlowGlobalConfig definitionGlobalConfig) {

        VariableCategory variableCategory = new VariableCategory();
        variableCategory.setCategoryName(VariableConstants.MODEL_VARIABLE_NAME);

        ArrayList<VariableSelectItem> variables = new ArrayList<>();

        if (ObjectUtil.isEmpty(definitionGlobalConfig)) {
            variableCategory.setVariableInfoList(variables);
            return variableCategory;
        }

        List<ModelVariable> modelVariableList = definitionGlobalConfig.getModelVariableList();
        if (ObjectUtil.isEmpty(modelVariableList)) {
            return variableCategory;
        }

        for (ModelVariable modelVariable : modelVariableList) {
            VariableSelectItem variableSelectItem = new VariableSelectItem(modelVariable.getVariableName(), modelVariable.getVariableCode(),
                    modelVariable.getVariableName());
            variables.add(variableSelectItem);
        }

        variableCategory.setVariableInfoList(variables);

        return variableCategory;
    }

}
