
package org.jeecg.modules.workFlow.flowlong.engine.core;

import org.jeecg.modules.workFlow.flowlong.engine.*;
import org.jeecg.modules.workFlow.flowlong.engine.assist.Assert;
import org.jeecg.modules.workFlow.flowlong.engine.cache.FlowCache;
import org.jeecg.modules.workFlow.flowlong.engine.handler.ConditionNodeHandler;
import org.jeecg.modules.workFlow.flowlong.engine.handler.CreateTaskHandler;
import org.jeecg.modules.workFlow.flowlong.engine.handler.FlowJsonHandler;
import org.jeecg.modules.workFlow.flowlong.engine.handler.impl.SimpleConditionNodeHandler;
import org.jeecg.modules.workFlow.flowlong.engine.handler.impl.SimpleCreateTaskHandler;
import org.jeecg.modules.workFlow.flowlong.engine.impl.DefaultProcessModelParser;
import org.jeecg.modules.workFlow.flowlong.engine.model.NodeModel;
import org.jeecg.modules.workFlow.flowlong.engine.model.ProcessModel;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * FlowLong流程引擎上下文
 *

 */
@Slf4j
@Getter
@Setter
public class FlowLongContext {
    private ProcessService processService;
    private QueryService queryService;
    private RuntimeService runtimeService;
    private TaskService taskService;
    private FlowLongExpression flowLongExpression;

    /**
     * 流程任务创建处理器
     */
    private CreateTaskHandler createTaskHandler;

    /**
     * 流程执行条件处理器
     */
    private ConditionNodeHandler conditionNodeHandler;

    /**
     * 流程任务创建拦截器
     */
    private TaskCreateInterceptor taskCreateInterceptor;

    /**
     * 任务访问策略类
     */
    private TaskAccessStrategy taskAccessStrategy;

    /**
     * 审批参与者提供者
     */
    private TaskActorProvider taskActorProvider;

    /**
     * 任务提醒接口
     */
    private TaskReminder taskReminder;

    /**
     * 审批参与者提供者
     */
    private TaskTrigger taskTrigger;

    /**
     * 静态注入流程模型解析器
     */
    @Setter
    private static ProcessModelParser PROCESS_MODEL_PARSER;

    public static ProcessModel parseProcessModel(String content, String cacheKey, boolean redeploy) {
        return PROCESS_MODEL_PARSER.parse(content, cacheKey, redeploy);
    }

    public static void invalidateProcessModel(String cacheKey) {
        PROCESS_MODEL_PARSER.invalidate(cacheKey);
    }

    /**
     * 注入默认流程模型解析器
     *
     * @param flowCache          流程缓存
     * @param processModelParser 流程模型解析器
     */
    public FlowLongContext(FlowCache flowCache, ProcessModelParser processModelParser) {
        if (null == processModelParser) {
            PROCESS_MODEL_PARSER = new DefaultProcessModelParser(flowCache);
        } else {
            PROCESS_MODEL_PARSER = processModelParser;
        }
    }

    /**
     * 检查并返回条件表达式
     */
    public FlowLongExpression checkFlowLongExpression() {
        Assert.isNull(flowLongExpression, "Interface FlowLongExpression not implemented");
        return this.flowLongExpression;
    }

    /**
     * 流程 JSON 处理器，默认 jackson 实现
     * 使用其它json框架可在初始化时赋值该静态属性
     */
    @Setter
    private static FlowJsonHandler flowJsonHandler;

    public static <T> T fromJson(String jsonString, Class<T> clazz) {
        return getFlowJsonHandler().fromJson(jsonString, clazz);
    }

    public static String toJson(Object object) {
        return getFlowJsonHandler().toJson(object);
    }

    private static FlowJsonHandler getFlowJsonHandler() {
        Assert.isNull(flowJsonHandler, "Please implement the FlowJsonHandler interface class");
        return flowJsonHandler;
    }

    /**
     * 获取创建流程任务处理器实现类
     *
     * @return {@link CreateTaskHandler}
     */
    public CreateTaskHandler getCreateTaskHandler() {
        return null != createTaskHandler ? createTaskHandler : SimpleCreateTaskHandler.getInstance();
    }

    /**
     * 获取创建流程任务处理器实现类
     *
     * @return {@link CreateTaskHandler}
     */
    public ConditionNodeHandler getFlowConditionHandler() {
        return null != conditionNodeHandler ? conditionNodeHandler : SimpleConditionNodeHandler.getInstance();
    }

    /**
     * 创建流程任务
     *
     * @param execution 执行对象
     * @param nodeModel 节点模型
     * @return true 执行成功  false 执行失败
     */
    public boolean createTask(Execution execution, NodeModel nodeModel) {
        // 拦截器前置处理
        if (null != taskCreateInterceptor) {
            taskCreateInterceptor.before(this, execution);
        }

        // 执行创建任务
        boolean result = this.getCreateTaskHandler().handle(this, execution, nodeModel);

        // 拦截器后置处理
        if (null != taskCreateInterceptor) {
            taskCreateInterceptor.after(this, execution);
        }
        return result;
    }

    /**
     * 默认初始化流程引擎上下文
     *
     * @param configEngine 流程配置引擎
     * @return {@link FlowLongEngine}
     */
    public FlowLongContext build(FlowLongEngine configEngine, boolean banner) {
        if (log.isInfoEnabled()) {
            log.info("FlowLongEngine start......");
        }
        /*
         * 由服务上下文返回流程引擎
         */
        Assert.isNull(configEngine, "Unable to discover implementation class for LongEngine");
        if (log.isInfoEnabled()) {
            log.info("FlowLongEngine be found {}", configEngine.getClass());
        }
        configEngine.configure(this);

        if (banner) {
            System.out.println("┌─┐┬  ┌─┐┬ ┬┬  ┌─┐┌┐┌┌─┐");
            System.out.println("├┤ │  │ │││││  │ │││││ ┬");
            System.out.println("└  ┴─┘└─┘└┴┘┴─┘└─┘┘└┘└─┘  1.1.9");
        }

        return this;
    }

}
