package cn.axzo.workflow.core.conf;

import cn.axzo.workflow.core.engine.behavior.CustomActivityBehaviorFactory;
import cn.axzo.workflow.core.engine.id.TimeBasedIdGenerator;
import com.google.common.collect.Lists;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.form.spring.SpringFormEngineConfiguration;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.flowable.common.engine.impl.AbstractEngineConfiguration.DB_SCHEMA_UPDATE_TRUE;

/**
 * Flowable 引擎相关全局配置
 *
 * @author wangli
 * @since 2023/7/13 11:18
 */
@Configuration
public class FlowableConfiguration {

    @Bean
    public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> processEngineConfigurer(
            ObjectProvider<FlowableEventListener> listeners,
            CustomActivityBehaviorFactory customActivityBehaviorFactory) {
        return configuration -> {
            configuration.setEventListeners(Lists.newArrayList(listeners));
            configuration.setActivityBehaviorFactory(customActivityBehaviorFactory);
            configuration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_TRUE);
            configuration.setEnableSafeBpmnXml(false);
            //            configuration.setCreateDiagramOnDeploy(false);
            configuration.setIdGenerator(new TimeBasedIdGenerator());
        };
    }

    @Bean
    public EngineConfigurationConfigurer<SpringFormEngineConfiguration> formEngineConfigurer() {
        return configuration -> configuration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_TRUE);
    }

    @Bean
    public CustomActivityBehaviorFactory customActivityBehaviorFactory() {
        return new CustomActivityBehaviorFactory();
    }
}
