package cn.reghao.bnt.web.admin.service;

import cn.reghao.bnt.web.admin.db.repository.WebhookRepository;
import cn.reghao.bnt.web.admin.model.po.Webhook;
import cn.reghao.bnt.web.admin.service.notifier.Notify;
import cn.reghao.bnt.web.admin.service.notifier.ding.DingMsg;
import cn.reghao.bnt.web.admin.service.notifier.ding.DingNotify;
import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;

/**
 * 通知服务
 *
 * @author reghao
 * @date 2021-02-25 00:10:17
 */
@Slf4j
@Service
public class NotifyService {
    private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("notify-service");
    private final DingNotify dingNotify;
    private final UserMessageService userMessageService;
    private final WebhookRepository webhookRepository;
    private final SiteOptionService siteOptionService;

    public NotifyService(DingNotify dingNotify, UserMessageService userMessageService,
                         WebhookRepository webhookRepository, SiteOptionService siteOptionService) {
        this.dingNotify = dingNotify;
        this.userMessageService = userMessageService;
        this.webhookRepository = webhookRepository;
        this.siteOptionService = siteOptionService;
    }

    public <T> void notify(T msg) {
        String receiver;
        if (msg instanceof DingMsg) {
            String webhookName = siteOptionService.getDevopsWebhook();
            Webhook webhook = webhookRepository.findByName(webhookName);
            if (webhook != null) {
                receiver = webhook.getUrl();
                DingMsg dingMsg = (DingMsg) msg;
                threadPool.execute(new NotifyTask<>(dingNotify, receiver, dingMsg, userMessageService));
            } else {
                log.error("webhook {} not exist", webhookName);
            }
        } else {
            log.error("unknown message {}", msg);
        }
    }

    public void notify(Webhook webhook, DingMsg dingMsg) {
        threadPool.execute(new NotifyTask<>(dingNotify, webhook.getUrl(), dingMsg, userMessageService));
    }
    /**
     * @param
     * @return
     * @date 2021-06-23 上午9:29
     */
    static class NotifyTask<T> implements Runnable {
        private final Notify<T> notify;
        private final String receiver;
        private final T msg;
        private final UserMessageService userMessageService;

        public NotifyTask(Notify<T> notify, String receiver, T msg, UserMessageService userMessageService) {
            this.notify = notify;
            this.receiver = receiver;
            this.msg = msg;
            this.userMessageService = userMessageService;
        }

        @Override
        public void run() {
            try {
                notify.send(receiver, msg);
            } catch (Exception e) {
                String title = "通知发送失败";
                String content = String.format("系统发送给 %s 的通知失败, 失败原因: %s, 通知内容: %s", receiver, e.getMessage(), msg);
                userMessageService.sendUserMessage(title, content);
            }
        }
    }
}
