package org.springblade.flow.engine.custom.type;

import static org.springblade.flow.engine.custom.constants.GlobalProcessVariableConstants.APPLY_USER_ID;
import static org.springblade.flow.engine.custom.constants.GlobalProcessVariableConstants.BUSINESS_TITLE;
import static org.springblade.flow.engine.custom.constants.GlobalProcessVariableConstants.PROCESS_STATUS;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.flowable.common.engine.api.delegate.event.FlowableEvent;
//import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.springblade.flow.base.util.SpringContextHolder;
import org.springblade.flow.engine.custom.helper.UserTaskNodeHelper;
import org.springblade.flow.engine.enums.FlowMessageEnum;
import org.springblade.flow.engine.enums.FlowTaskStatusEnum;
import org.springblade.flow.message.enums.MessageTypeEnum;
import org.springblade.flow.message.model.message.SentMessageForm;
import org.springblade.flow.message.util.SentMessageUtil;
import org.springblade.flow.util.JsonUtil;
import org.springblade.flow.util.JsonUtilEx;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * @author LoveMyOrange
 * @create 2022-01-18 17:23
 */
@Slf4j
@Component
public class ProcessCompletedListener  implements FlowableEventListener {
    @Override
    public void onEvent(FlowableEvent event) {
        if(event instanceof FlowableEntityEventImpl){
            FlowableEntityEventImpl activitiEntityEvent=(FlowableEntityEventImpl)event;
            UserTaskNodeHelper userTaskNodeHelper = SpringContextHolder.getBean(UserTaskNodeHelper.class);
            SentMessageUtil sentMessageUtil = SpringContextHolder.getBean(SentMessageUtil.class);
            ExecutionEntity entity = (ExecutionEntity) activitiEntityEvent.getEntity();
            String processInstanceId = activitiEntityEvent.getProcessInstanceId();
            RuntimeService runtimeService = SpringContextHolder.getBean(RuntimeService.class);
            runtimeService.setVariable(entity.getProcessInstanceId(),PROCESS_STATUS,FlowTaskStatusEnum.Adopt.getCode());
            String applyUserId =  entity.getVariable(APPLY_USER_ID,String.class);
            String fullName = entity.getVariable(BUSINESS_TITLE, String.class);
            List<String> messageType=new ArrayList<>();
            List<String> userList = new ArrayList<>();
            userList.add(applyUserId);
            messageType.add(MessageTypeEnum.SysMessage.getCode());
            String msgTitle="【流程结束】";
            SentMessageForm sentMessageForm = this.message(msgTitle, FlowMessageEnum.me.getCode(), messageType, processInstanceId, userList,fullName);
            sentMessageUtil.sendMessage(sentMessageForm);
        }
        else{
            return;
        }
    }

    @Override
    public boolean isFailOnException() {
        return true;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }



    /**
     * 发送消息封装
     *
     * @param title       名称
     * @param type        类型
     * @param messageType 发送消息类型
     * @param userList    发送用户
     * @return
     */
    private SentMessageForm message(String title, int type, List<String> messageType, String processInstanceId, List<String> userList, String fullName) {
        SentMessageForm sentMessageForm = new SentMessageForm();
        sentMessageForm.setSendType(messageType);
        Map<String, Object> message = new HashMap<>(16);
        message.put("type", type);
        message.put("id", processInstanceId);
        sentMessageForm.setContent(JsonUtilEx.getObjectToString(message));
        sentMessageForm.setTitle(fullName + title);
        sentMessageForm.setToUserIds(userList);
        Map<String, String> smsContent = new HashMap<>(16);
        this.message(fullName, title, smsContent);
        sentMessageForm.setSmsContent(smsContent);
        return sentMessageForm;
    }

    /**
     * 短信模板
     *
     * @param title  标题
     * @param status 状态
     * @return
     */
    private void message(String title, String status, Map<String, String> msg) {
        Map<String, String> aliyun = new HashMap<>(16);
        aliyun.put("title", title);
        aliyun.put("status", status);
        msg.put("1", JsonUtil.getObjectToString(aliyun));
        msg.put("2", "2");
    }
}
