package cn.axzo.msg.center.notices.manager;

import cn.axzo.msg.center.notices.common.enums.MessageCommonStatusEnum;
import cn.axzo.msg.center.notices.common.enums.ReturnCodeEnum;
import cn.axzo.msg.center.notices.common.exception.BizException;
import cn.axzo.msg.center.notices.integration.client.LianLuClient;
import cn.axzo.msg.center.notices.integration.dto.request.LianLuSendTemplateSmsRequest;
import cn.axzo.msg.center.notices.integration.dto.response.LianLuBaseResponse;
import cn.axzo.msg.center.notices.manager.api.MessageTemplateParamManager;
import cn.axzo.msg.center.notices.manager.api.SmsSendManager;
import cn.axzo.msg.center.notices.manager.api.dto.request.BatchMessageSendRequestDto;
import cn.axzo.msg.center.notices.manager.api.dto.request.MessageSendRequestDto;
import cn.axzo.msg.center.notices.manager.api.dto.response.BatchMessageSendResponseDto;
import cn.axzo.msg.center.notices.manager.api.dto.response.SendSmsCommonResponseDto;
import cn.axzo.msg.center.notices.manager.api.dto.response.TemplateParamDto;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @description
 *   联麓短信发送
 * @author cold_blade
 * @date 2023/9/7
 * @version 1.0
 */
@Slf4j
@RequiredArgsConstructor
@Component("lianLuSmsSendManager")
public class LianLuSmsSendManagerImpl implements SmsSendManager {

    private final LianLuClient lianLuClient;
    private final MessageTemplateParamManager messageTemplateParamManager;

    @Override
    public SendSmsCommonResponseDto sendMessage(MessageSendRequestDto requestDto) {
        log.info("[LianLuSmsSendManagerImpl#sendMessage] -> lianlu - 发送sms {}", JSON.toJSONString(requestDto));
        // 请求转换
        LianLuSendTemplateSmsRequest request = new LianLuSendTemplateSmsRequest();
        request.setPhoneNo(requestDto.getPhoneNo());
        request.setTemplateCode(requestDto.getTemplateNo());
        request.setTemplateMap(requestDto.getTemplateMap());
        request.setRequestChannelNo(requestDto.getRequestChannelNo());
        // 按照参数的顺序获取对应的参数的值
        request.setTemplateParams(
                listAndSortTemplateParams(requestDto.getTemplateNo(), requestDto.getTemplateMap()));

        // 发送短信
        LianLuBaseResponse response = lianLuClient.sendSingleTemplateSms(request);

        // 响应请求
        SendSmsCommonResponseDto result = new SendSmsCommonResponseDto();
        result.setBizId(response.getTaskId());
        result.setRequestId(response.getRequestId());
        result.setMessage(result.getMessage());
        result.setStatus(response.isSuccess() ? MessageCommonStatusEnum.OK : MessageCommonStatusEnum.FAIL);
        return result;
    }

    @Override
    public BatchMessageSendResponseDto sendBatchMessage(BatchMessageSendRequestDto requestDto) {
        throw new UnsupportedOperationException();
    }

    private String[] listAndSortTemplateParams(String channelTemplateCode, Map<String, Object> templateMap) {
        List<TemplateParamDto> params = messageTemplateParamManager.getByCode(channelTemplateCode);
        if (CollectionUtils.isEmpty(params)) {
            return new String[]{};
        }
        // 入参检测
        for (TemplateParamDto param : params) {
            BizException.error(templateMap.containsKey(param.getParamName()), ReturnCodeEnum.INVALID_PARAMETER,
                    param.getParamName() + "不能为空");
        }
        return params.stream()
                .sorted(Comparator.comparing(TemplateParamDto::getParamOrder))
                .map(e -> templateMap.get(e.getParamName()).toString())
                .toArray(String[]::new);
    }
}
