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

import com.jay.monitor.data.core.utils.JsonHelper;
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.config.AlertSleepModel;
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.models.store.AlertPlatformConfig;
import com.jay.monitor.data.server.report.alert.rule.AbstractAlertRule;
import com.jay.monitor.data.server.utils.DataUtils;
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;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @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, AlertPlatformConfig alertPlatformConfig,
        RealTimeAlert realTimeAlert) {

        AlertConfigModel alertConfigModel = configStoreManager.getAlertConfigModel(AlertMsgEnums.DingDing);

        // 钉钉地址
        String url = DataUtils.getDefaultValue(alertPlatformConfig.getUrl(), alertConfigModel.getUrl());

        // 当前服务web地址
        String consoleUrl = configStoreManager.getConsoleUrl();
        // 日志监控地址
        String logMonitorUrl = configStoreManager.getLogMonitorUrl();
        // 当前服务监控地址
        String serverLogConsole = configStoreManager.getServerLogConsole();

        AlertSleepModel alertSleepModel = sleepMap.computeIfAbsent(getCacheKey(realTimeAlert),
            k -> new AlertSleepModel(System.currentTimeMillis(), new AtomicInteger(0)));

        realTimeAlert.setTriggerCount(alertSleepModel.getNumber().get());

        String logUrl = logMonitorUrl + realTimeAlert.getLogId();

        String object = JsonHelper.toPrettyJsonString(realTimeAlert);

        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));

        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);

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

        return DingDingUtils.send(url, actionCardModel);
    }

}
