package cn.stylefeng.workflow.instance.modular.message.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.sys.api.MessagePublishApi;
import cn.stylefeng.roses.kernel.sys.api.enums.message.MessageTypeEnum;
import cn.stylefeng.roses.kernel.sys.api.enums.message.PriorityLevelEnum;
import cn.stylefeng.roses.kernel.sys.api.pojo.message.MessageSendDTO;
import cn.stylefeng.workflow.api.entity.task.RuTask;
import cn.stylefeng.workflow.api.enums.TaskConditionEnum;
import cn.stylefeng.workflow.api.enums.TaskTypeEnum;
import cn.stylefeng.workflow.api.pojo.InstanceContext;
import cn.stylefeng.workflow.api.pojo.config.node.notice.NodeNotice;
import cn.stylefeng.workflow.instance.api.constants.InstanceConstants;
import cn.stylefeng.workflow.instance.modular.message.TaskMessageApi;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static cn.stylefeng.workflow.instance.api.constants.InstanceConstants.TASK_NOTICE_URL_TEMPLATE;

@Service
public class WebsiteMessageImpl implements TaskMessageApi {

    @Resource
    private MessagePublishApi messagePublishApi;

    @Override
    public boolean validateSendFlag(NodeNotice nodeNotice) {
        if (nodeNotice == null) {
            return false;
        }
        return nodeNotice.getSiteMessage();
    }

    @Override
    public void sendMessage(Long userId, RuTask ruTask, NodeNotice nodeNotice, InstanceContext instanceContext) {

        MessageSendDTO messageSendDTO = new MessageSendDTO();

        messageSendDTO.setUserIdList(CollectionUtil.set(false, userId));

        String title = "审批任务通知";
        messageSendDTO.setMessageTitle(title);

        String content = "流程标题：" + instanceContext.getRuntimeInstance().getInstanceTitle() + "，请即时处理。";
        messageSendDTO.setMessageContent(content);

        messageSendDTO.setMessageType(MessageTypeEnum.URL.getCode());

        messageSendDTO.setMessageUrl(StrUtil.format(TASK_NOTICE_URL_TEMPLATE, ruTask.getTaskId(), instanceContext.getRuntimeInstance().getInstanceId()));

        messageSendDTO.setPriorityLevel(PriorityLevelEnum.MIDDLE.getCode());

        messageSendDTO.setBusinessType(InstanceConstants.APPROVAL_TASK_MESSAGE_BUSINESS_TYPE);

        messageSendDTO.setBusinessDetail(createMessageBusinessDetail(ruTask, instanceContext));

        messagePublishApi.batchSendMessage(messageSendDTO);
    }

    private Map<String, Object> createMessageBusinessDetail(RuTask ruTask, InstanceContext instanceContext) {

        Map<String, Object> businessDetail = new HashMap<>();

        businessDetail.put("taskId", ruTask.getTaskId());

        if (ruTask.getTaskType().equals(TaskTypeEnum.PROCESSING.getCode())) {
            businessDetail.put("taskCondition", TaskConditionEnum.WAIT_TO_DO.getKey());
        } else if (ruTask.getTaskType().equals(TaskTypeEnum.COPY.getCode())) {
            businessDetail.put("taskCondition", TaskConditionEnum.WAIT_TO_READ.getKey());
        }

        businessDetail.put("instanceId", instanceContext.getRuntimeInstance().getInstanceId());

        businessDetail.put("definitionId", instanceContext.getRuntimeInstance().getDefinitionId());

        businessDetail.put("modelId", instanceContext.getRuntimeInstance().getModelId());

        businessDetail.put("instanceTitle", instanceContext.getRuntimeInstance().getInstanceTitle());

        return businessDetail;
    }

}
