package cn.axzo.msg.center.notices.integration.client.impl;

import cn.axzo.msg.center.notices.common.constans.ChuangLanConstants;
import cn.axzo.msg.center.notices.common.domain.ServiceContextHolder;
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.ChuangLanSmsClient;
import cn.axzo.msg.center.notices.integration.dto.request.BaseSmsRequest;
import cn.axzo.msg.center.notices.integration.dto.request.MessageRequest;
import cn.axzo.msg.center.notices.integration.dto.response.ChuangLanSmsResponse;
import cn.axzo.msg.center.notices.integration.support.ChuangLanMessageRequestResolver;
import cn.axzo.msg.center.notices.integration.support.ChuangLanMessageRequestResolverFactory;
import cn.azxo.framework.common.utils.LogUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 创蓝短信客户端实现类
 *
 * @author zhaoyong
 * @see ChuangLanSmsClientImpl
 * @since 2021-08-07 17:02
 */
@Slf4j
@Component("chuangLanSmsClient")
public class ChuangLanSmsClientImpl implements ChuangLanSmsClient {

  @Resource(name = "chuangLanMessageRequestResolverFactory")
  private ChuangLanMessageRequestResolverFactory factory;

  @Resource(name = "restTemplate")
  private RestTemplate restTemplate;

  @Override
  public ChuangLanSmsResponse sendMessage(MessageRequest request) {
    log.info("[manager -> client]ChuangLanSmsClientImpl.sendBatchMessage request param is {}", JSON.toJSONString(request));

    // 获取请求参数解析器
    ChuangLanMessageRequestResolver requestResolver = factory.getService(request.getMessageTemplateType());
    BizException.error(requestResolver != null,
        ReturnCodeEnum.INVALID_PARAMETER, "不支持的模板类型 : " + request.getMessageTemplateType());

    // 获取发送创蓝短信请求类
    BaseSmsRequest smsRequest = requestResolver.resolve(request);
    log.info("[mns -> chuanglan]ChuangLanSmsClientImpl.sendBatchMessage request param is {}", JSON.toJSONString(smsRequest));

    // 发送短信
    ChuangLanSmsResponse response;
    try {
      response = restTemplate.postForObject(request.getRequestUrl(),
          smsRequest, ChuangLanSmsResponse.class);
    } catch (Exception e) {
      LogUtil.error("[mns -> chuanglan]ChuangLanSmsClientImpl.sendBatchMessage is fail.", e);
      throw new BizException(ReturnCodeEnum.SYSTEM_ERROR, "invoke chuanglang fail");
    }

    // 添加响应对象
    ServiceContextHolder.get().setChannelResponse(response);

    // 检查响应对象
    checkResponse(response);

    log.info("[mns <- chuanglan]ChuangLanSmsClientImpl.sendBatchMessage response is {}", JSON.toJSONString(response));

    // 返回响应
    return response;
  }

  /**
   * 检查响应信息
   * @param response
   */
  private void checkResponse(ChuangLanSmsResponse response) {
    if(response == null) {
      throw new BizException(ReturnCodeEnum.SYSTEM_ERROR, "response is null");
    }
    if(!ChuangLanConstants.SUCCESS_CODE.equals(response.getCode())) {
      throw new BizException(ReturnCodeEnum.SYSTEM_ERROR.getCode(), response.getErrorMsg(), response);
    }
  }
}
