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

import cn.axzo.msg.center.common.utils.PlaceholderResolver;
import cn.axzo.msg.center.notices.integration.client.impl.RegulatoryGaoxinClient;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 政务系统
 *
 * @author yanglin
 */
@Slf4j
@RequiredArgsConstructor
@Service("regulatoryGaoxinManager")
public class RegulatoryGaoxinManager implements SmsSendManager {

    private static final String USE_CHANNEL_CONTENT = "use_channel_content";

    private final RegulatoryGaoxinClient regulatoryGaoxinClient;

    @Override
    public SendSmsCommonResponseDto sendMessage(MessageSendRequestDto request) {
        log.info("[RegulatoryAuthorityManager#sendMessage] -> 政务 - 发送sms {}", JSON.toJSONString(request));
        JSONObject expansion = request.getRequest().parseExpansion();
        Boolean useChannelContent = expansion.getBoolean(USE_CHANNEL_CONTENT);
        if (useChannelContent == null) useChannelContent = false;
        String templateContent = useChannelContent
                ? request.getChannelTemplate().getTemplateContent()
                : request.getTemplate().getTemplateContent();
        String content = PlaceholderResolver.getDefaultResolver().resolveByMap(
                templateContent, request.getTemplateMap());
        String respStr = regulatoryGaoxinClient.send(request.getPhoneNo(), content);
        SendSmsCommonResponseDto result = new SendSmsCommonResponseDto();
        result.setBizId(respStr);
        result.setRequestId("");
        result.setMessage("");
        return result;
    }

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

}