package com.sikey.wa04.business.infrastructure.adapter.push;

import com.sikey.wa04.business.infrastructure.adapter.push.template.NotificationMessageTemplate;
import com.sikey.wa04.business.infrastructure.configuration.PushConfig;
import okhttp3.OkHttpClient;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

class HuaweiV3PushNotificationTest {
    private static final String CLIENT_ID = "114048095";

    /**
     * 使用接口https://push-api.cloud.huawei.com/v2/[projectid]/messages:send时，才能使用
     */
    private static final String PROJECT_ID = "461323198429873300";

    private static final String CLIENT_SECRET = "39cf856f66e555b21b7964f1f14b0570a9a7753b9e09b28ea7a957828a9265d3";

    private static final OkHttpClient httpClient = new OkHttpClient();

    private static final String ack_id = "28865";

    private static final Long  MsgId = 62214L;


    private static final String sendId = "7cd3c863-9a16-4a23-9eee-e750ba4a62f9";
    private static final String recvId = "7a5b93ef-bcf2-41f8-bf65-c83cea00f3b1";

    private static final int msg_type = 1001;

    //设备请求小米获取到的RegId
    private static final String RegId = "IQAAAACy1KVqAADHmvmiB5V_98OZHEEtTaqaqsJb7gnwK2UntgZYC8XJowPe5FwxCbR6pD7PxazegPD_Ic6AT6esKktNrgi89zFDYl3IqVovykk7Bw";

    @Test
    public void send() throws Exception {

        PushConfig pushConfig = new PushConfig();
        PushConfig.PushHuaweiOAuth2Config pConfig = new PushConfig.PushHuaweiOAuth2Config();
        pConfig.setClientId(CLIENT_ID);
        pConfig.setClientSecret(CLIENT_SECRET);
        pConfig.setProjectId(PROJECT_ID);
        pushConfig.setHuawei(pConfig);
        HuaweiV3PushNotification pushNotification = new HuaweiV3PushNotification();
        AccessToken huaweiv3AccessToken = pushNotification.authenticate(pushConfig);

        Map<String, Object> content = new HashMap<>();
        content.put("roomId", "TK_m0NddolJMM_7a5b93ef-bcf2-41f8-bf65-c83cea00f3b1_1749021506");
        content.put("videoCallToken", "007eJxSYDC5tzyUeX3AhXWr5riJfz63a+LNGo5XU0LezKvSEHu++rK5AoOlUbKJsalFkmWapYWJoWWaZaKhQYqxiWmqqWWiuXmaoSuLeoYAHwPD+jObmRgZGBlYGBgZQHwmMMkMJlnApAODgYGFqUliWpqumbGxia6JmXmSrkWyQZKusWVisrF5WophUpJJfIh3vH+gm0eRd3FIunO8obmJubGFuYmZoZmRGSODASAAAP//EqIvKw==");

        NotificationMessageTemplate messageTemplate = new NotificationMessageTemplate();
        Map<String, Object> params = new HashMap<>();
        params.put("ack_id", ack_id);
        params.put("msg_id", MsgId);
        params.put("msg_type", msg_type);
        params.put("sendId", sendId);
        params.put("recvId", recvId);
        params.put("sendTime", Instant.now().toEpochMilli());
        params.put("content", content);
        params.put("from_push", "true");
        //System.out.println("json-params:"+JSONUtil.toJsonPrettyStr(params));
        messageTemplate.setMsgId(MsgId).setMsgType(msg_type).setRegId(RegId).setTitle("新消息77777777"+Instant.now()).setBody("您有新的消息，请查看77777777777777777").setParams(params);
        try {
            String result = String.valueOf(pushNotification.send(messageTemplate, huaweiv3AccessToken));
            System.out.println(result);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}