package cn.iocoder.yudao.framework.http.sms;

import cn.iocoder.yudao.framework.http.core.Request;
import cn.iocoder.yudao.framework.http.core.Response;
import cn.iocoder.yudao.framework.http.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;


/**
 * SendChampHttp类用于处理与SendChamp API相关的HTTP请求，以发送短信和获取短信状态
 * 该类包含了构造请求参数和请求头的方法，以及执行HTTP请求的方法
 *
 * @author wuxian
 * @since 2024-10-23
 **/
@Slf4j
public class SendChampHttp {
    // 定义发送短信的API URL
    private static final String SMS_URL = "https://api.sendchamp.com/api/v1/sms/send";
    // 定义获取短信状态的API URL前缀
    private static final String RECEIVE_URL = "https://api.sendchamp.com/api/v1/sms/status/";

    /**
     * 发送POST请求以发送短信
     *
     * @param param  请求参数，包含短信的接收者、消息内容等
     * @param header 请求头，包含API密钥等信息
     * @return 返回API的响应结果
     */
    public Response postReq(Map<String, Object> param, Map<String, String> header) {
        return HttpUtils.post(SMS_URL, param, header, Request.ParamFormat.JSON);
    }

    /**
     * 发送GET请求以获取短信发送状态
     *
     * @param header 请求头，包含API密钥等信息
     * @param id     短信发送的唯一标识符
     * @return 返回API的响应结果
     */
    public Response getReceiveStatus(Map<String, String> header, String id) {
        return HttpUtils.get(RECEIVE_URL + id, header, "");
    }

    /**
     * 设置HTTP请求的头信息
     *
     * @param apiKey SendChamp API密钥
     * @return 返回构造好的请求头Map对象
     */
    public Map<String, String> setHeader(String apiKey) {
        Map<String, String> header = new HashMap<>();
        header.put("Accept", "application/json,text/plain,*/*");
        header.put("Content-Type", "application/json");
        header.put("Authorization", apiKey);
        return header;
    }

    /**
     * 设置发送短信的请求参数
     *
     * @param to         短信接收者的电话号码
     * @param message    短信内容
     * @param senderName 发送者名称
     * @return 返回构造好的请求参数Map对象
     */
    public Map<String, Object> setParams(String to, String message, String senderName) {
        Map<String, Object> param = new HashMap<>();
        param.put("to", to);
        param.put("message", message);
        param.put("sender_name", senderName);
        param.put("route", "dnd");
        return param;
    }

    public static void main(String[] args) {
        SendChampHttp test = new SendChampHttp();

        String to = "8618926674857";
        String senderName = "ECLogistics";
        String message = "Good day！This is E&C logistics(ship from China to Nigeria),It has been a while since we last worked together, and we would like to reestablish our partnership. Our main service: GROUPAGE/FULL CONTAINE";
        Map<String, Object> param = test.setParams(to, message, senderName);

        String apiKey = "Bearer sendchamp_live_$2a$10$vQPdaDjl96Ybc5tzFmZYg.nqGirXuJBGDqJArthZnFR8P9mM5Z/JO";
        Map<String, String> header = test.setHeader(apiKey);
        test.postReq(param, header);
//        test.getReceiveStatus(header, "66e6e9df-b454-4df7-a968-af944a535757");
    }


}
