package org.jeecg.common.config;

import com.google.common.collect.Lists;

import org.activiti.engine.*;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.jeecg.modules.activiti.manage.CustomGroupEntityManager;
import org.jeecg.modules.activiti.manage.CustomUserEntityManager;
import org.jeecg.modules.apply.lisener.CustomEventListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * 工作流配置
 *
 * @author yao
 * @date 2019/06/13
 */
@Configuration
public class ActivitiConfig {

    @Autowired
    private CustomUserEntityManager customUserEntityManager;

    @Autowired
    private CustomGroupEntityManager customGroupEntityManager;

    @Autowired
    private CustomEventListener customEventListener;


    /**
     * 核心流程引擎类
     *
     * @param transactionManager transactionManager
     * @param dataSource         dataSource
     * @return ProcessEngine
     */
    @Bean
    public ProcessEngine processEngine(PlatformTransactionManager transactionManager, DataSource dataSource)
            throws IOException {
        SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
        // 自动部署已有的流程文件
//        Resource[] resources = new PathMatchingResourcePatternResolver()
//                .getResources(ResourceLoader.CLASSPATH_URL_PREFIX + "processes/*.bpmn");
//        Resource[] resources2 = new PathMatchingResourcePatternResolver()
//                .getResources(ResourceLoader.CLASSPATH_URL_PREFIX + "processes/*.bpmn20.xml");
//        ArrayList<Resource> arrayList = new ArrayList(Arrays.asList(resources));
//        arrayList.addAll(Arrays.asList(resources2));
//        Resource[] arr =new Resource[arrayList.size()];
//        Resource[] resources1 = arrayList.toArray(arr);
//        processEngineConfiguration.setDeploymentResources(resources1);
//        processEngineConfiguration.setDeploymentName("系统自动部署流程");

        processEngineConfiguration.setTransactionManager(transactionManager);
        processEngineConfiguration.setDataSource(dataSource);
        processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE);
//        processEngineConfiguration.setDatabaseSchema("tcore_dev");
//        processEngineConfiguration.setDatabaseTablePrefix("ACT");
        // 自定义用户和组
        processEngineConfiguration.setUserEntityManager(customUserEntityManager);
        processEngineConfiguration.setGroupEntityManager(customGroupEntityManager);

        processEngineConfiguration.setActivityFontName("SimHei");
        processEngineConfiguration.setAnnotationFontName("SimHei");
        processEngineConfiguration.setLabelFontName("SimHei");


        //自定义事件监听配置
        processEngineConfiguration.setEventListeners(Lists.newArrayList(customEventListener));
        return processEngineConfiguration.buildProcessEngine();

    }

    /**
     * 流程仓库Service，用于管理流程仓库，例如部署、删除、读取流程资源
     *
     * @param processEngine processEngine
     * @return RepositoryService
     */
    @Bean
    public RepositoryService repositoryService(ProcessEngine processEngine) {
        return processEngine.getRepositoryService();
    }

    /**
     * 运行时Service，可以也拿过来处理所有正在运行状态的流程实例、任务等
     *
     * @param processEngine processEngine
     * @return RuntimeService
     */
    @Bean
    public RuntimeService runtimeService(ProcessEngine processEngine) {
        return processEngine.getRuntimeService();
    }

    /**
     * 任务Service，用于管理和查询任务，例如签收、办理、指派等
     *
     * @param processEngine processEngine
     * @return ApprovalTaskService
     */
    @Bean
    public TaskService taskService(ProcessEngine processEngine) {
        return processEngine.getTaskService();
    }

    /**
     * 历史Service，用于查询所有历史数据，例如流程实例、任务、活动、变量、附件
     *
     * @param processEngine processEngine
     * @return HistoryService
     */
    @Bean
    public HistoryService historyService(ProcessEngine processEngine) {
        return processEngine.getHistoryService();
    }

    /**
     * 引擎管理Service，和具体业务无关，主要可以查询引擎配置、数据库、作业等
     *
     * @param processEngine processEngine
     * @return ManagementService
     */
    @Bean
    public ManagementService managementService(ProcessEngine processEngine) {
        return processEngine.getManagementService();
    }

    /**
     * 身份Service，用于管理和查询用户、组之间的关系
     *
     * @param processEngine processEngine
     * @return IdentityService
     */
    @Bean
    public IdentityService identityService(ProcessEngine processEngine) {
        return processEngine.getIdentityService();
    }

    /**
     * 表单Service，用于读取流程、任务相关的表单数据
     *
     * @param processEngine processEngine
     * @return FormService
     */
    @Bean
    public FormService formService(ProcessEngine processEngine) {
        return processEngine.getFormService();
    }

    /**
     * 一个新增的服务，用于动态修改流程中的一些参数信息等，是引擎中的一个辅助的服务
     *
     * @param processEngine processEngine
     * @return DynamicBpmnService
     */
    @Bean
    public DynamicBpmnService dynamicBpmnService(ProcessEngine processEngine) {
        return processEngine.getDynamicBpmnService();
    }
}
