package cn.axzo.msg.center.im.service.impl;

import cn.axzo.im.center.api.feign.MessageController;
import cn.axzo.im.center.api.vo.ApiChannel;
import cn.axzo.im.center.api.vo.req.UpdateTemplateSendPriorityRequest;
import cn.axzo.msg.center.api.request.v3.SetImSendPriorityRequest;
import cn.axzo.msg.center.common.utils.BizAssertions;
import cn.axzo.msg.center.dal.MessageBaseTemplateDao;
import cn.axzo.msg.center.domain.entity.MessageBaseTemplate;
import cn.axzo.msg.center.im.service.IMService;
import cn.axzo.msg.center.inside.notices.config.ImMessageProps;
import cn.axzo.msg.center.inside.notices.service.impl.v3.msg.TemplateMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * @description
 *   即时通信功能模块
 *
 * @author cold_blade
 * @date 2023/9/27
 * @version 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IMServiceImpl implements IMService {

    private final MessageController messageController;
    private final MessageBaseTemplateDao messageBaseTemplateDao;
    private final ImMessageProps imProps;

    @Override
    public Object setImSendPriority(SetImSendPriorityRequest request) {
        BizAssertions.assertNotNull(request.getTemplateCode(), "templateCode can't be blank");
        BizAssertions.assertNotNull(request.getSendPriority(), "sendPriority can't be null");
        BizAssertions.assertTrue(request.getSendPriority() > 0, "sendPriority must be greater than 0");
        boolean templateUpdated = messageBaseTemplateDao.lambdaUpdate()
                .eq(MessageBaseTemplate::getCode, request.getTemplateCode())
                .set(MessageBaseTemplate::getImSendPriority, request.getSendPriority())
                .update();
        BizAssertions.assertTrue(templateUpdated, "未找到对应的模版或者模版已经删除");
        UpdateTemplateSendPriorityRequest updatePriorityRequest = new UpdateTemplateSendPriorityRequest();
        updatePriorityRequest.setBizIdPrefix(TemplateMessage.getBizIdPrefix(request.getTemplateCode()));
        updatePriorityRequest.setSendPriority(request.getSendPriority());
        updatePriorityRequest.setToken(imProps.getControllerToken());
        Boolean imMessageUpdated = messageController.updateSendPriority(updatePriorityRequest).getData();
        Set<ApiChannel> interceptedChannels = new HashSet<>();
        if (Boolean.TRUE.equals(imMessageUpdated)) {
            // 期待LB可以正常调度
            for (int i = 0; i < 4; i++) {
                Set<ApiChannel> channels = messageController
                        .interceptSend(imProps.getControllerToken())
                        .getData();
                if (channels != null)
                    interceptedChannels.addAll(channels);
                if (interceptedChannels.size() == ApiChannel.values().length)
                    break;
            }
        }
        HashMap<String, Object> result = new HashMap<>();
        result.put("templatePriorityUpdated", templateUpdated);
        result.put("imMessagePriorityUpdated", imMessageUpdated);
        result.put("interceptedChannels", interceptedChannels);
        return result;
    }

}