package com.jay.monitor.data.server.report.alert.notify;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jay.monitor.data.server.costs.MdConstants;
import com.jay.monitor.data.server.costs.ServerConstants;
import com.jay.monitor.data.server.enums.AlertMsgEnums;
import com.jay.monitor.data.server.enums.AlertRuleEnums;
import com.jay.monitor.data.server.models.config.AlertConfigModel;
import com.jay.monitor.data.server.models.dto.alert.RealTimeAlert;
import com.jay.monitor.data.server.models.request.dingding.ActionCardModel;
import com.jay.monitor.data.server.report.alert.rule.AbstractAlertRule;
import com.jay.monitor.data.server.utils.DingDingUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

/**
 * @Module 告警
 * @Description 实时告警
 * @Author liukaixiong
 * @Date 2021/1/14 13:25
 */
@Component
public class DingDingRealTimeAlertProcess extends AbstractAlertRule {

    @Value("${spring.profiles.active:}")
    private String profiles;

    @Override
    public AlertMsgEnums alertType() {
        return AlertMsgEnums.DingDing;
    }

    @Override
    public boolean send(AlertRuleEnums triggerRule, RealTimeAlert realTimeAlert) {
        AlertConfigModel alertConfigModel = configStoreManager.getAlertConfigModel(AlertMsgEnums.DingDing);
        String url = alertConfigModel.getUrl();
        String consoleUrl = configStoreManager.getConsoleUrl();
        String logMonitorUrl = configStoreManager.getLogMonitorUrl();
        String serverLogConsole = configStoreManager.getServerLogConsole();

        realTimeAlert.setTriggerType(triggerRule);

        realTimeAlert.setTriggerCount(sleepMap.get(getCacheKey(realTimeAlert)).getNumber().get());

        String logUrl = logMonitorUrl + realTimeAlert.getLogId();

        String object = JSON.toJSONString(realTimeAlert, SerializerFeature.PrettyFormat,
                SerializerFeature.WriteDateUseDateFormat, SerializerFeature.SkipTransientField);

        StringBuilder mdText = new StringBuilder();
        mdText.append("【[日志跳转明细](" + logUrl + ")】");
        mdText.append("\r【[" + profiles + " - 控制台跳转明细](" + consoleUrl + ")】");
        mdText.append(MdConstants.newLine);
        mdText.append(" ``` ");
        mdText.append(MdConstants.newLine);
        mdText.append(object);
        mdText.append(MdConstants.newLine);

        List<ActionCardModel.ActionCardDTO.BtnsDTO> btnList = new ArrayList<>();
        Map<String, Object> extMap = realTimeAlert.getExtMap();
        if (extMap != null) {
            Object id = extMap.get(ServerConstants.ERROR_LOG_ID);
            if (AlertRuleEnums.STATUS == triggerRule && id != null) {
                String linkConsole = serverLogConsole + "?logId=" + id.toString();
                ActionCardModel.ActionCardDTO.BtnsDTO errorBtn = new ActionCardModel.ActionCardDTO.BtnsDTO("查看异常栈", linkConsole);
                btnList.add(errorBtn);
            }

            Object jsonLogId = extMap.get(ServerConstants.JSON_LOG_ID);

            if (jsonLogId != null) {
                String linkConsole = serverLogConsole + "?logId=" + jsonLogId.toString();
                ActionCardModel.ActionCardDTO.BtnsDTO errorBtn = new ActionCardModel.ActionCardDTO.BtnsDTO("查看请求参数", linkConsole);
                btnList.add(errorBtn);
            }
        }

        btnList.add(new ActionCardModel.ActionCardDTO.BtnsDTO("控制台查看", consoleUrl));

        ActionCardModel actionCardModel = DingDingUtils.buildActionCardModel("jay - 实时告警", mdText.toString(), 0, btnList);

        return DingDingUtils.send(url, actionCardModel);
    }


}
