package cn.stylefeng.workflow.design.modular.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.workflow.api.DefinitionApi;
import cn.stylefeng.workflow.api.pojo.config.model.FlowGlobalConfig;
import cn.stylefeng.workflow.api.pojo.variable.VariableCategory;
import cn.stylefeng.workflow.design.modular.factory.BaseVariablesFactory;
import cn.stylefeng.workflow.design.modular.service.VariableService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

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

@Service
public class VariableServiceImpl implements VariableService {

    @Resource
    private DefinitionApi definitionApi;

    @Override
    public List<VariableCategory> getModelVariables(Long definitionId) {

        ArrayList<VariableCategory> variableCategories = new ArrayList<>();

        VariableCategory baseVariables = BaseVariablesFactory.createBaseVariables();
        variableCategories.add(baseVariables);

        FlowGlobalConfig definitionGlobalConfig = definitionApi.getDefinitionGlobalConfig(definitionId);
        VariableCategory formVariable = BaseVariablesFactory.createFormVariable(definitionGlobalConfig);
        if (ObjectUtil.isNotEmpty(formVariable.getVariableInfoList())) {
            variableCategories.add(formVariable);
        }

        VariableCategory modelVariables = BaseVariablesFactory.createModelVariables(definitionGlobalConfig);
        if (ObjectUtil.isNotEmpty(modelVariables.getVariableInfoList())) {
            variableCategories.add(modelVariables);
        }

        return variableCategories;
    }

}