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

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.team.project.api.entity.BusinessCategory;
import cn.stylefeng.workflow.api.entity.design.Model;
import cn.stylefeng.workflow.design.api.pojo.BusinessCategoryVo;

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

public class ModelFactory {

    public static List<BusinessCategoryVo> combineCategoryAndModel(List<BusinessCategory> categoryList, List<Model> modelList) {
        List<BusinessCategoryVo> businessCategoryVos = new ArrayList<>();

        if (ObjectUtil.isEmpty(categoryList) || ObjectUtil.isEmpty(modelList)) {
            return businessCategoryVos;
        }

        for (BusinessCategory businessCategory : categoryList) {

            BusinessCategoryVo temp = new BusinessCategoryVo();

            temp.setCategoryName(businessCategory.getCategoryName());
            temp.setCategoryCode(businessCategory.getCategoryCode());

            ArrayList<Model> models = new ArrayList<>();

            for (Model model : modelList) {
                if (model.getBusinessCategoryId().equals(businessCategory.getBusinessCategoryId())) {
                    models.add(model);
                }
            }

            temp.setModelList(models);

            if (ObjectUtil.isNotEmpty(temp.getModelList())) {
                businessCategoryVos.add(temp);
            }
        }

        return businessCategoryVos;
    }

    public static List<BusinessCategoryVo> combineUserCommonFlow(List<BusinessCategoryVo> businessCategoryVos, List<Long> userCommonFlowModelIdList) {
        if (ObjectUtil.isEmpty(userCommonFlowModelIdList)) {
            return businessCategoryVos;
        }

        BusinessCategoryVo commonFlowCategory = new BusinessCategoryVo();
        commonFlowCategory.setCategoryName("常用流程");

        List<Model> commonFlowModels = new ArrayList<>();
        for (Long commonFLowId : userCommonFlowModelIdList) {
            for (BusinessCategoryVo businessCategoryVo : businessCategoryVos) {
                for (Model model : businessCategoryVo.getModelList()) {
                    if (model.getModelId().equals(commonFLowId)) {

                        model.setCommonFlowFlag(true);

                        commonFlowModels.add(model);
                    }
                }
            }
        }
        commonFlowCategory.setModelList(commonFlowModels);

        businessCategoryVos.add(0, commonFlowCategory);
        return businessCategoryVos;
    }

}
