package cn.reghao.tnb.message.app.service;

import cn.reghao.jutil.jdk.serializer.JsonConverter;
import cn.reghao.tnb.common.util.ConstantId;
import cn.reghao.tnb.message.api.constant.MsgType;
import cn.reghao.tnb.message.api.dto.UserMessageDto;
import cn.reghao.tnb.message.api.dto.msg.RtmpMessage;
import cn.reghao.tnb.message.app.model.msg.DingMsgUtil;
import cn.reghao.tnb.message.app.service.notifier.ding.DingMsg;
import cn.reghao.tnb.message.api.constant.NotifyType;
import cn.reghao.tnb.message.api.dto.msg.LoginMessage;
import cn.reghao.tnb.message.api.dto.msg.VerifyMessage;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2023-08-28 13:55:03
 */
@Slf4j
@Service
public class MessageConsumer {
    private final NotifyService notifyService;
    private final UserMessageService userMessageService;

    public MessageConsumer(NotifyService notifyService, UserMessageService userMessageService) {
        this.notifyService = notifyService;
        this.userMessageService = userMessageService;
    }

    public void handleMessage(String jsonPayload) {
        JsonObject jsonObject = JsonConverter.jsonToJsonElement(jsonPayload).getAsJsonObject();
        int notifyType = jsonObject.get("notifyType").getAsInt();
        int msgTypeCode = jsonObject.get("msgType").getAsInt();
        MsgType msgType = MsgType.getByCode(msgTypeCode);
        switch (NotifyType.getByCode(notifyType)) {
            case webhook:
                handleWebhookMessage(msgType, jsonPayload);
                break;
            case email:
                break;
            case mobile:
                break;
        }
    }

    private void handleWebhookMessage(MsgType msgType, String jsonPayload) {
        DingMsg dingMsg = null;
        switch (msgType) {
            case verifyMsg:
                VerifyMessage verifyMessage = JsonConverter.jsonToObject(jsonPayload, VerifyMessage.class);
                dingMsg = DingMsgUtil.getDingMsg(verifyMessage);
                break;
            case loginMsg:
                LoginMessage loginMessage = JsonConverter.jsonToObject(jsonPayload, LoginMessage.class);
                dingMsg = DingMsgUtil.getDingMsg(loginMessage);
                break;
            case rtmpMsg:
                RtmpMessage rtmpMessage = JsonConverter.jsonToObject(jsonPayload, RtmpMessage.class);
                dingMsg = DingMsgUtil.getDingMsg(rtmpMessage);
                break;
            default:
                break;
        }

        if (dingMsg != null) {
            String webhookName = "ding_bdbot";
            notifyService.notify(webhookName, dingMsg);
        }

        int msgTypeCode = msgType.getValue();
        String title = "webhook 通知";
        String content = String.format("发送 %s 消息, 消息内容: %s", msgType.name(), jsonPayload);
        UserMessageDto userMessageDto = new UserMessageDto(msgTypeCode, title, content, ConstantId.ANONYMOUS_USER_ID);
        userMessageService.addMessage(userMessageDto);
    }
}
