package org.springblade.flow.engine.service;


import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springblade.flow.engine.enums.NotifyType;
import org.springblade.message.enums.MsgType;
import org.springblade.message.model.*;
import org.springblade.system.client.SubscribeNotifyConfClient;
import org.springblade.system.client.UserClient;
import org.springblade.system.client.model.UserModel;
import org.springblade.system.client.model.VisualSubscribeNotifyConfModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SendMessageService {
    @Autowired
    private UserClient userClient;

    @Autowired
    private SubscribeNotifyConfClient subscribeNotifyConfClient;

    public MsgResponse sendMsg(List<String>userIds, NotifyType notifyType,String subject,String content) {
        List<String>list= new ArrayList<>();
        MsgResponse msgResponse = null;
        for (String userId : userIds){
            // 查询用户信息
            UserModel userModel = userClient.getById(Long.valueOf(userId));
            if (notifyType == NotifyType.email) {
               if (StrUtil.isNotBlank(userModel.getEmail())){
                   list.add(userModel.getEmail());
               }
            }else {
                if (StrUtil.isNotBlank(userModel.getPhone())){
                    list.add(userModel.getPhone());
                }
            }
        }
        //查询系统三方配置
        List<VisualSubscribeNotifyConfModel> visualSubscribeNotifyConfModels = subscribeNotifyConfClient.notifyConfList(notifyType.getValue());
        Map<String, String> confMap = visualSubscribeNotifyConfModels.stream().collect(Collectors.toMap(VisualSubscribeNotifyConfModel::getAttriKey, VisualSubscribeNotifyConfModel::getAttriValue));
        if (notifyType == NotifyType.email) {
            String username = confMap.get("username");
            String password = confMap.get("password");
            String smtp = confMap.get("smtp");
            EmailInfo emailInfo = EmailInfo.builder().smtpHost(smtp).userName(username).passWord(password).build();
            EmailMsgRequest emailMsgRequest = EmailMsgRequest.builder().body(content).title(subject).msgType(MsgType.email).emails(list).emailInfo(emailInfo).build();
            //发送邮件
            msgResponse = SpringUtil.getBean(MsgType.email.getClazz()).sendEmailMessage(emailMsgRequest);
        }else if(notifyType == NotifyType.qywechat){
            // 发送企业微信
            AppMsgInfo appMsgInfo = AppMsgInfo.builder().agentId(confMap.get("agent_id")).corpSecret(confMap.get("corp_secret")).appId(confMap.get("corp_id"))
                    .appSecret(confMap.get("app_secret")).build();
            AppMsgRequest appMsgRequest = AppMsgRequest.builder().body(content).phones(list).appMsgInfo(appMsgInfo)
                    .title(subject).msgType(MsgType.appQywechat).build();
            msgResponse = SpringUtil.getBean(MsgType.appQywechat.getClazz()).sendAppMessage(appMsgRequest);
        }else if(notifyType == NotifyType.dingtalk){
            // 发送钉钉消息
            AppMsgInfo appMsgInfo = AppMsgInfo.builder().agentId(confMap.get("agent_id")).appId(confMap.get("app_key"))
                    .appSecret(confMap.get("app_secret")).build();
            AppMsgRequest appMsgRequest = AppMsgRequest.builder().body(content).phones(list).appMsgInfo(appMsgInfo)
                    .title(subject).msgType(MsgType.appDingtalk).build();
            msgResponse = SpringUtil.getBean(MsgType.appDingtalk.getClazz()).sendAppMessage(appMsgRequest);
        }else if (notifyType == NotifyType.feishu){
            // 发送飞书消息
            AppMsgInfo appMsgInfo = AppMsgInfo.builder().appId(confMap.get("app_id")).appSecret(confMap.get("app_secret")).build();
            AppMsgRequest appMsgRequest = AppMsgRequest.builder().body(content).phones(list).appMsgInfo(appMsgInfo)
                    .title(subject).msgType(MsgType.appFeishu).build();
            msgResponse = SpringUtil.getBean(MsgType.appFeishu.getClazz()).sendAppMessage(appMsgRequest);
        }
        return msgResponse;
    }

}
