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

import cn.hutool.json.JSONUtil;
import com.sikey.wa04.business.infrastructure.adapter.push.template.NotificationMessageTemplate;
import com.sikey.wa04.business.infrastructure.configuration.PushConfig;
import okhttp3.*;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

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

class XiaomiPushNotificationTest {

    String appSecret = "Fe1luYm5um9QkQQBjS/bHA==";
    private static final String AUTHENTICATE_REQUEST_URL = "https://api.xmpush.xiaomi.com/v3/message/regid";
    private static final String AUTHENTICATE_REQUEST_CONTENT_TYPE = "application/json; charset=utf-8";

    //App的包名
    private static final String RESTRICTED_PACKAGE_NAME = "com.sikey.interconnect";

    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 = 40;

    //设备请求小米获取到的RegId
    private static final String RegId = "7m91ljjaDGf6oLWJJeZjSjq/c/nuSnhbrf7othmzeMu4UJqbFuvbbgYzYhjwXM3z";

    @Test
    void send() {
        PushConfig pushConfig = new PushConfig();
        PushConfig.XiaomiOAuth2Config xiaomiOAuth2Config = new PushConfig.XiaomiOAuth2Config();
        xiaomiOAuth2Config.setAppSecret(appSecret);
        xiaomiOAuth2Config.setMaxRetry(3);
        pushConfig.setXiaomi(xiaomiOAuth2Config);
        XiaomiPushNotification xiaomiPushNotification = new XiaomiPushNotification();
        AccessToken xiaomiAccessToken = xiaomiPushNotification.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("ackId", ack_id);
        params.put("msgId", MsgId);
        params.put("msgType", 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("新消息").setBody("您有新的消息，请查看").setParams(params);
        try {
            String result = String.valueOf(xiaomiPushNotification.send(messageTemplate, xiaomiAccessToken));
            System.out.println(result);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}