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.File;
import java.io.FileInputStream;
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 = "5b761f954efe7493de0bc751942e1a8355853771b66a512f5687ca05e7335e99";
        String alertTitle = "你好333";
        String alertBody = "hi333";
        int badge = 1;
        String topicBundleId = "com.xteng.Hibro";
        boolean contentAvailable = false;


//        InputStream certificate = IosPush.getApnsCertificate(apnsCertificatePath);
//        String encode = Base64.encode(certificate);

        File file = new File("/Users/giaogiao/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/f31cd9e6d7da1d15c57c40575f5c85db/Message/MessageTemp/e1180914825140f051a87348bda5cbb5/File/hipro_test_push.p12");
        InputStream in = new FileInputStream(file);
        String encode = Base64.encode(in);

        // 解码
        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();
//    }
}
