package cn.axzo.workflow.server.alter;

import cn.axzo.framework.jackson.utility.JSON;
import cn.axzo.riven.client.common.enums.DingTalkMsgTypeEnum;
import cn.axzo.riven.client.feign.DingDingMsgApi;
import cn.axzo.riven.client.model.SampleMarkdown;
import cn.axzo.riven.client.req.DingDingSendRebootGroupMsgReq;
import cn.axzo.workflow.common.model.NextNodePreCheckAlterDTO;
import cn.axzo.workflow.common.model.dto.AlterDTO;
import cn.axzo.workflow.core.conf.SupportRefreshProperties;
import cn.axzo.workflow.core.listener.Alter;
import cn.axzo.workflow.core.util.DingTalkUtils;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

import static cn.axzo.workflow.core.util.DingTalkUtils.getWebUrl;
import static cn.axzo.workflow.core.util.DingTalkUtils.mobiles;

/**
 * 钉钉告警实现
 *
 * @author wangli
 * @since 2024-09-13 11:40
 */
@Slf4j
@Component
public class DingTalkAlter implements Alter {

    @Value("${spring.profiles.active}")
    private String profile;
    @Resource
    private SupportRefreshProperties refreshProperties;
    @Resource
    private DingDingMsgApi dingDingMsgApi;

    @Override
    public void invoke(Object obj) {
        log.info("send biz node alter : {}", JSON.toJSONString(obj));
        if (obj instanceof AlterDTO) {
            AlterDTO alterDTO = (AlterDTO) obj;
            if (Objects.equals(profile, "master")) {
                rivenDingtalk(alterDTO);
            } else {
                DingTalkUtils.sendDingTalkForBizNodeAlter(profile, alterDTO, refreshProperties.getAlterMobiles());
            }
        }
        if (obj instanceof NextNodePreCheckAlterDTO) {
            NextNodePreCheckAlterDTO alterDTO = (NextNodePreCheckAlterDTO) obj;
            if (Objects.equals(profile, "master")) {
                rivenDingtalkForNodePreCheck(alterDTO);
            } else {
                DingTalkUtils.sendDingTalkForNodePreCheck(profile, alterDTO, refreshProperties.getAlterMobiles());
            }
        }
    }

    private void rivenDingtalkForNodePreCheck(NextNodePreCheckAlterDTO alterDTO) {
        DingDingSendRebootGroupMsgReq req = new DingDingSendRebootGroupMsgReq();
        req.setDingDingScene("WORKFLOW_ENGINE_BIZNODE_ALTER");
        String processInstanceId = alterDTO.getProcessInstanceId();
        String title = "Notice 审批模板节点预检查告警, Env: " + profile;
        String text = "#### [" + profile + "]审批模板节点预检查告警\n" +
//                "> 相关信息: " + JSONUtil.toJsonStr(alterDTO) + "\n\n" +
                "> 实例 ID：" + alterDTO.getProcessInstanceId() + "\n\n" +
                "> 检测节点 ID：" + alterDTO.getActivityId() + "\n\n" +
                "> ##### 错误信息：" + alterDTO.getErrorMsg() + "\n\n" +
                mobiles(refreshProperties.getAlterMobiles());
        SampleMarkdown markdown = new SampleMarkdown(title, text);

        JSONObject markdownJson = JSONObject.parseObject(markdown.toJson());
        JSONObject atMobiles = new JSONObject();
        atMobiles.put("atMobiles", refreshProperties.getAlterMobiles());
        markdownJson.put("at", atMobiles);
        markdownJson.put("isAtAll", false);
        req.setDingDingJson(markdownJson.toJSONString());
        req.setMsgType(DingTalkMsgTypeEnum.sampleMarkdown);
        dingDingMsgApi.sendRebootGroupMsg(req);
    }

    private void rivenDingtalk(AlterDTO alterDTO) {
        DingDingSendRebootGroupMsgReq req = new DingDingSendRebootGroupMsgReq();
        req.setDingDingScene("WORKFLOW_ENGINE_BIZNODE_ALTER");
        String processInstanceId = alterDTO.getProcessInstanceId();
        String title = "Notice 业务节点长时间停止告警, Env: " + profile;
        String text = "#### [" + profile + "]业务节点长时间停止\n" +
                "> 节点相关信息: " + JSONUtil.toJsonStr(alterDTO) + "\n\n" +
                "> ##### [点击查看审批日志](" + getWebUrl(profile) + "/#/workflow/examples?processInstanceId=" + processInstanceId + ") \n\n" +
                "> ##### 提示：如果以上地址提示未登录，请在对应环境 OMS 系统登录后并点击审批管理的功能后，再使用。或者复制实例 ID 到 OMS 中手动查询 \n\n " +
                mobiles(refreshProperties.getAlterMobiles());
        SampleMarkdown markdown = new SampleMarkdown(title, text);

        JSONObject markdownJson = JSONObject.parseObject(markdown.toJson());
        JSONObject atMobiles = new JSONObject();
        atMobiles.put("atMobiles", refreshProperties.getAlterMobiles());
        markdownJson.put("at", atMobiles);
        markdownJson.put("isAtAll", false);
        req.setDingDingJson(markdownJson.toJSONString());
        req.setMsgType(DingTalkMsgTypeEnum.sampleMarkdown);
        dingDingMsgApi.sendRebootGroupMsg(req);
    }

}
