package io.geekidea.springbootplus.test;

import cn.hutool.core.codec.Base64;
import com.turo.pushy.apns.DeliveryPriority;
import com.turo.pushy.apns.PushType;
import com.wecloud.im.ws.sender.IosPush;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class IosApnsBase64Test {

    public static void main(String[] args) throws IOException {

//    apnsCertificatePath 证书
//     * @param productFlag         环境
//     * @param deviceToken         设备token
//     * @param alertTitle          标题
//     * @param alertBody           副标题
//     * @param contentAvailable    Boolean.FALSE
//    * @param customProperty      自定义属性
//    * @param badge               角标数量
//    * @param priority            DeliveryPriority.IMMEDIATE
//    * @param pushType            PushType.ALERT
//    * @param topicBundleId       undleId
//    * @param sound               rtc= "call.caf"; 否则为default

        Map<String, Object> customProperty = new HashMap<String, Object>(10);
        String apnsCertificatePath = "frogsell_push_dev.p12";
        String deviceToken = "27c93ca84bbf17d9ff8eb05df0576ac49822db2ae1c02aa0afea83b5c3861276";
        String alertTitle = "你好333";
        String alertBody = "hi333";
        int badge = 1;
        String topicBundleId = "com.jdw.frogsell";
        boolean contentAvailable = false;


        InputStream certificate = IosPush.getApnsCertificate(apnsCertificatePath);
        String encode = Base64.encode(certificate);
        // 解码
        byte[] decode = Base64.decode(encode);

        InputStream inputStream2 = new ByteArrayInputStream(decode);

        IosPush.push("123456", inputStream2, Boolean.FALSE, deviceToken, alertTitle, alertBody,
                contentAvailable, customProperty, badge
                , DeliveryPriority.IMMEDIATE, PushType.ALERT, topicBundleId,
                "default");
    }

//    public static byte[] readInputStream(InputStream inputStream) throws IOException {
//        byte[] buffer = new byte[1024];
//        int len = 0;
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        while ((len = inputStream.read(buffer)) != -1) {
//            bos.write(buffer, 0, len);
//        }
//        bos.close();
//        return bos.toByteArray();
//    }

//    private static String inputToString(InputStream is) throws IOException {
//        final int bufferSize = 1024;
//        final char[] buffer = new char[bufferSize];
//        final StringBuilder out = new StringBuilder();
//        Reader in = new InputStreamReader(is, StandardCharsets.UTF_8);
//        for (; ; ) {
//            int rsz = in.read(buffer, 0, buffer.length);
//            if (rsz < 0)
//                break;
//            out.append(buffer, 0, rsz);
//        }
//        return  out.toString();
//    }
}
