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

import cn.iocoder.yudao.framework.http.core.Request;
import cn.iocoder.yudao.framework.http.core.Response;
import cn.iocoder.yudao.framework.http.core.client.HttpClient;
import cn.iocoder.yudao.framework.http.core.client.HttpClientFactory;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;

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


/**
 * @author wuxian
 * @since 2024-10-23
 **/
public class SendchampHttp {

    private static final String SMS_URL = "https://api.sendchamp.com/api/v1/sms/send";

    private static final String RECEIVE_URL = "https://api.sendchamp.com/api/v1/sms/status/";

    public Response postReq(Map<String, Object> param, Map<String, String> header) {
        HttpClient client = HttpClientFactory.get(Request.Util.OkHttp);
        Request req = Request.create(SMS_URL, Request.Method.POST, param);
        req.setParamFormat(Request.ParamFormat.JSON);
        try {
            Response res = client.execute(req, Request.Option.create(0, 0, header));
            System.out.println("response code ---------------------------->" + res.getCode());
            System.out.println("response content is --------------------》" + res.getBody());
            return res;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @SneakyThrows
    public Response getReceiveStatus(Map<String, String> header, String id) {
        HttpClient client = HttpClientFactory.get(Request.Util.OkHttp);
        Request req = Request.create(RECEIVE_URL + id, Request.Method.GET, "");
        try {
            Response res = client.execute(req, Request.Option.create(0, 0, header));
            System.out.println("response code ---------------------------->" + res.getCode());
            System.out.println("response content is --------------------》" + res.getBody());
            return res;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @NotNull
    public static 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;
    }

    @NotNull
    public static 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 = "2348140352000";
        String to = "8618926674857";
        String senderName = "Sendchamp";
        //信息如果超过200字符，需要拆成多条短信，多次发送
        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 = setParams(to, message, senderName);

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

    }


}
