package org.springblade.flow.engine.custom.behavior.factory;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.*;
import org.flowable.engine.impl.bpmn.behavior.*;
import org.flowable.engine.impl.bpmn.parser.factory.DefaultActivityBehaviorFactory;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springblade.flow.engine.custom.behavior.impl.*;
import org.springblade.flow.engine.custom.constants.BasicActivitiConstants;
import org.springblade.flow.engine.entity.FlowTaskCirculateEntity;
import org.springblade.flow.util.RandomUtil;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 *  行为类使用了 工厂设计模式
 *  同样可以覆盖别的方法,加入其他元素的自定义行为,参考 @see ActivityBehaviorFactory
 *  该类控制执行到某一元素时触发
 */
@Component
@Slf4j
public class CustomActivityBehaviorFactory extends DefaultActivityBehaviorFactory {

    /**
     * 重写子流程的行为类
     * @param subProcess
     * @return
     */
    @Override
    public SubProcessActivityBehavior createSubprocessActivityBehavior(SubProcess subProcess) {
        return new CustomSubProcessActivityBehavior();
    }


    /**
     * 用户节点的行为类
     * @param userTask
     * @return
     */
    @Override
    public UserTaskActivityBehavior createUserTaskActivityBehavior(UserTask userTask) {
        log.info("change usertask Behavior : {}  ", userTask);
        return new CustomUserTaskActivityBehavior(userTask);
    }


    /**
     * 并行会签的行为类
     * @param activity
     * @param innerActivityBehavior
     * @return
     */
    @Override
    public ParallelMultiInstanceBehavior createParallelMultiInstanceBehavior(Activity activity, AbstractBpmnActivityBehavior innerActivityBehavior) {
        return new CustomParallelMultiInstanceBehavior(activity,innerActivityBehavior);
    }

    /**
     * 串行会签的行为类
     * @param activity
     * @param innerActivityBehavior
     * @return
     */
    @Override
    public SequentialMultiInstanceBehavior createSequentialMultiInstanceBehavior(Activity activity, AbstractBpmnActivityBehavior innerActivityBehavior) {
        return new CustomSequentialMultiInstanceBehavior(activity,innerActivityBehavior);
    }

    /**
     * 调用活动
     * @param callActivity
     * @return
     */
    @Override
    public CallActivityBehavior createCallActivityBehavior(CallActivity callActivity) {
        return new CustomCallActivityActivityBehavior(callActivity);
    }

    /**
     * 根据不同的属性创建不同的行为类
     * @param manualTask
     * @return
     */
    @Override
    public ManualTaskActivityBehavior createManualTaskActivityBehavior(ManualTask manualTask) {
        String attributeValue = manualTask.getAttributeValue(BasicActivitiConstants.exActivitiPrefixURL, BasicActivitiConstants.extensionAttributeName);
        if(StringUtils.isBlank(attributeValue)){
            return super.createManualTaskActivityBehavior(manualTask);
        }
        else{
            return new CustomManualTaskActivityBehavior();
        }
    }



}
