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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * YCloudWhatsappHttp类用于处理与 Whatsapp 相关的 HTTP 请求，以发送短信和获取短信状态。
 *
 * @author wuxian
 * @since 2024-10-30
 **/
@Slf4j
public class YCloudWhatsappHttp {

    //请求地址
    private static final String WA_URL = "https://api.ycloud.com/v2/whatsapp/messages/sendDirectly";
    private static final String RECEIVE_URL = "https://api.ycloud.com/v2/whatsapp/messages/";

    /**
     * 发送WhatsApp消息
     *
     * @param header 请求头，包含认证信息
     * @param param  请求参数，包含消息内容
     * @return 响应结果
     */
    public Response postWhatsapp(Map<String, String> header, Map<String, Object> param) {
        return HttpUtils.post(WA_URL, param, header, Request.ParamFormat.JSON);
    }

    /**
     * 获取消息发送状态
     *
     * @param header 请求头，包含认证信息
     * @param id     消息ID
     * @return 响应结果
     */
    public Response getReceiveStatus(Map<String, String> header, String id) {
        return HttpUtils.get(RECEIVE_URL + id, header, "");
    }

    /**
     * 设置请求参数
     *
     * @param code         验证码
     * @param templateName 模板名称
     * @param lanCode      语言编号
     * @param to           接收手机号
     * @return 参数
     */
    public Map<String, Object> setParams(String code, String templateName, String lanCode, String to) {
        //构建模板参数
        Map<String, Object> template = new HashMap<>();
        Map<String, String> language = new HashMap<>();
        Parameters parameters = new Parameters();
        Button button = new Button();
        button.setIndex("0");
        button.setType("button");
        button.setSub_type("url");

        //模板包含语言(language)、组件（components），组件包含参数（parameters）
        parameters.setType("text");
        parameters.setText(code);
        List<Object> list1 = new ArrayList<>();
        list1.add(parameters);

        //构建json中的组件
        button.setParameters(list1);
        List<Object> list2 = new ArrayList<>();
        list2.add(new Components().setType("body").setParameters(list1));
        list2.add(button);

        language.put("code", lanCode);
        language.put("policy", "deterministic");

        template.put("language", language);
        template.put("name", templateName);
        template.put("components", list2);

        //构建最终requestBody的json
        Map<String, Object> param = new HashMap<>();
        param.put("from", "8618022485824");
        param.put("to", to);
        param.put("type", "template");
        param.put("template", template);
        return param;
    }

    /**
     * 设置请求头
     *
     * @param apiKey API密钥
     * @return 请求头
     */
    public Map<String, String> setHeader(String apiKey) {
        //构建http请求的头
        Map<String, String> header = new HashMap<>();
        header.put("Accept", "application/json");
        header.put("Content-Type", "application/json");
        header.put("X-API-Key", apiKey);
        return header;
    }

    public static void main(String[] args) {
        YCloudWhatsappHttp test = new YCloudWhatsappHttp();
        String apiKey = "9dbd912f56c101e53b23cb7b758ffda8";
        String code = "8888";
        String templateName = "ec_verification";
        String lanCode = "en";
        test.postWhatsapp(test.setHeader(apiKey), test.setParams(code, templateName, lanCode, "8618926674857"));
    }
}

@lombok.Setter
@lombok.Getter
class Parameters {
    private String type;
    private String text;

}

@lombok.Setter
@lombok.Getter
class Components {
    private String type;
    private List<Object> parameters = null;
}

@lombok.Setter
@lombok.Getter
class Button {
    private String type;
    private String sub_type;
    private String index;
    private List<Object> parameters = null;
}



