package com.wecloud.im.biz.core.push;


import org.json.JSONArray;
import org.json.JSONObject;

import com.wecloud.im.biz.core.push.android.AndroidBroadcast;
import com.wecloud.im.biz.core.push.android.AndroidCustomizedcast;
import com.wecloud.im.biz.core.push.android.AndroidFilecast;
import com.wecloud.im.biz.core.push.android.AndroidGroupcast;
import com.wecloud.im.biz.core.push.android.AndroidUnicast;
import com.wecloud.im.biz.core.push.ios.IOSBroadcast;
import com.wecloud.im.biz.core.push.ios.IOSCustomizedcast;
import com.wecloud.im.biz.core.push.ios.IOSFilecast;
import com.wecloud.im.biz.core.push.ios.IOSGroupcast;
import com.wecloud.im.biz.core.push.ios.IOSUnicast;

/**
 * 推送工具类
 */
public class PushUtils {
    private final String timestamp = null;
    private final PushClient client = new PushClient();
    private String appkey = null;
    private String appMasterSecret = null;

    public PushUtils(String key, String secret) {
        try {
            appkey = key;
            appMasterSecret = secret;
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public static void main(String[] args) {
        //  set your appkey and master secret here
        PushUtils demo = new PushUtils("your appkey", "your master secret");
        try {

            // 安卓单推
            String deviceToken = "your device_token";
            String unicastText = "Android unicast text";
            String unicastTicker = "Android unicast ticker";
            String title = "中文的title";
            demo.sendAndroidUnicast(deviceToken, unicastText, unicastTicker, title);

            // ios 单推
            String deviceTokenIOS = "your device_token";
            String titleIOS = "今日天气";
            String subtitle = "";
            String body = "今日可能下雨🌂";
            demo.sendIOSUnicast(deviceTokenIOS, titleIOS, subtitle, body);

            /*  these methods are all available, just fill in some fields and do the test
             * demo.sendAndroidCustomizedcastFile();
             * demo.sendAndroidBroadcast();
             * demo.sendAndroidGroupcast();
             * demo.sendAndroidCustomizedcast();
             * demo.sendAndroidFilecast();
             *
             * demo.sendIOSBroadcast();
             * demo.sendIOSGroupcast();
             * demo.sendIOSCustomizedcast();
             * demo.sendIOSFilecast();
             */
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void sendAndroidBroadcast() throws Exception {
        AndroidBroadcast broadcast = new AndroidBroadcast(appkey, appMasterSecret);
        broadcast.setTicker("Android broadcast ticker");
        broadcast.setTitle("中文的title");
        broadcast.setText("Android broadcast text");
        broadcast.goAppAfterOpen();
        broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        //  Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        broadcast.setProductionMode();
        // Set customized fields
        broadcast.setExtraField("test", "helloworld");
        //厂商通道相关参数
        broadcast.setChannelActivity("your channel activity");
        broadcast.setChannelProperties("abc");
        client.send(broadcast);
    }

    /**
     * 安卓单推
     *
     * @param deviceToken
     * @param unicastText
     * @param unicastTicker
     * @param title
     * @throws Exception
     */
    public void sendAndroidUnicast(String deviceToken, String unicastText, String unicastTicker, String title) throws Exception {
        AndroidUnicast unicast = new AndroidUnicast(appkey, appMasterSecret);
        //  Set your device token


        unicast.setDeviceToken(deviceToken);
        unicast.setTicker(unicastTicker);
        unicast.setTitle(title);

        unicast.setText(unicastText);
        unicast.goAppAfterOpen();
        unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        //  Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        unicast.setProductionMode();
        // Set customized fields
        unicast.setExtraField("test", "helloworld");
        unicast.setChannelActivity("your channel activity");
        unicast.setChannelProperties("abc");
        client.send(unicast);
    }

    public void sendAndroidGroupcast() throws Exception {
        AndroidGroupcast groupcast = new AndroidGroupcast(appkey, appMasterSecret);
        /*
         *  Construct the filter condition:
         *  "where":
         *	{
         *		"and":
         *		[
         *			{"tag":"test"},
         *			{"tag":"Test"}
         *		]
         *	}
         */
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        JSONObject TestTag = new JSONObject();
        testTag.put("tag", "test");
        TestTag.put("tag", "Test");
        tagArray.put(testTag);
        tagArray.put(TestTag);
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);

        groupcast.setFilter(filterJson);
        groupcast.setTicker("Android groupcast ticker");
        groupcast.setTitle("中文的title");
        groupcast.setText("Android groupcast text");
        groupcast.goAppAfterOpen();
        groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        groupcast.setChannelActivity("your channel activity");
        //  Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        groupcast.setProductionMode();
        //厂商通道相关参数
        groupcast.setChannelActivity("your channel activity");
        groupcast.setChannelProperties("abc");
        client.send(groupcast);
    }

    public void sendAndroidCustomizedcast() throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey, appMasterSecret);
        //  Set your alias here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        customizedcast.setAlias("alias", "alias_type");
        customizedcast.setTicker("Android customizedcast ticker");
        customizedcast.setTitle("中文的title");
        customizedcast.setText("Android customizedcast text");
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        //  Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        customizedcast.setProductionMode();
        //厂商通道相关参数
        customizedcast.setChannelActivity("your channel activity");
        customizedcast.setChannelProperties("abc");
        client.send(customizedcast);
    }

    public void sendAndroidCustomizedcastFile() throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey, appMasterSecret);
        //  Set your alias here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb" + "\n" + "alias");
        customizedcast.setFileId(fileId, "alias_type");
        customizedcast.setTicker("Android customizedcast ticker");
        customizedcast.setTitle("中文的title");
        customizedcast.setText("Android customizedcast text");
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        //  Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        customizedcast.setProductionMode();
        //厂商通道相关参数
        customizedcast.setChannelActivity("your channel activity");
        customizedcast.setChannelProperties("abc");
        client.send(customizedcast);
    }

    public void sendAndroidFilecast() throws Exception {
        AndroidFilecast filecast = new AndroidFilecast(appkey, appMasterSecret);
        //  upload your device tokens, and use '\n' to split them if there are multiple tokens
        String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb");
        filecast.setFileId(fileId);
        filecast.setTicker("Android filecast ticker");
        filecast.setTitle("中文的title");
        filecast.setText("Android filecast text");
        filecast.goAppAfterOpen();
        filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        //厂商通道相关参数
        filecast.setChannelActivity("your channel activity");
        filecast.setChannelProperties("abc");
        client.send(filecast);
    }

    public void sendIOSBroadcast() throws Exception {
        IOSBroadcast broadcast = new IOSBroadcast(appkey, appMasterSecret);
        //alert值设置为字符串
        //broadcast.setAlert("IOS 广播测试");
        //alert的值设置为字典
        broadcast.setAlert("今日天气", "", "今日可能下雨🌂");
        broadcast.setBadge(0);
        broadcast.setSound("default");
        //  set 'production_mode' to 'true' if your app is under production mode
        broadcast.setTestMode();
        // Set customized fields
        broadcast.setCustomizedField("test", "helloworld");
        client.send(broadcast);
    }


    /**
     * ios单推
     *
     * @param deviceToken
     * @param title
     * @param subtitle
     * @param body
     * @throws Exception
     */
    public void sendIOSUnicast(String deviceToken, String title, String subtitle, String body) throws Exception {
        IOSUnicast unicast = new IOSUnicast(appkey, appMasterSecret);
        //  Set your device token

        unicast.setDeviceToken(deviceToken);
        //alert值设置为字符串
        //unicast.setAlert("IOS 单播测试");
        //alert的值设置为字典

        unicast.setAlert(title, subtitle, body);
        unicast.setBadge(0);
        unicast.setSound("default");
        //  set 'production_mode' to 'true' if your app is under production mode
        unicast.setTestMode();
        // Set customized fields
        unicast.setCustomizedField("test", "helloworld");
        client.send(unicast);
    }

    public void sendIOSGroupcast() throws Exception {
        IOSGroupcast groupcast = new IOSGroupcast(appkey, appMasterSecret);
        /*
         *  Construct the filter condition:
         *  "where":
         *	{
         *		"and":
         *		[
         *			{"tag":"iostest"}
         *		]
         *	}
         */
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        testTag.put("tag", "iostest");
        tagArray.put(testTag);
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);
        System.out.println(filterJson.toString());

        // Set filter condition into rootJson
        groupcast.setFilter(filterJson);
        //groupcast.setAlert("IOS 组播测试");
        //alert的值设置为字典
        groupcast.setAlert("今日天气", "subtitle", "今日可能下雨🌂");
        groupcast.setBadge(0);
        groupcast.setSound("default");
        //  set 'production_mode' to 'true' if your app is under production mode
        groupcast.setTestMode();
        client.send(groupcast);
    }

    public void sendIOSCustomizedcast() throws Exception {
        IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey, appMasterSecret);
        //  Set your alias and alias_type here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        customizedcast.setAlias("alias", "alias_type");
        //customizedcast.setAlert("IOS 个性化测试");
        //alert的值设置为字典
        customizedcast.setAlert("今日天气", "", "今日可能下雨🌂");
        customizedcast.setBadge(0);
        customizedcast.setSound("default");
        //  set 'production_mode' to 'true' if your app is under production mode
        customizedcast.setTestMode();
        client.send(customizedcast);
    }

    public void sendIOSFilecast() throws Exception {
        IOSFilecast filecast = new IOSFilecast(appkey, appMasterSecret);
        //  upload your device tokens, and use '\n' to split them if there are multiple tokens
        String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb");
        filecast.setFileId(fileId);
        //filecast.setAlert("IOS 文件播测试");
        //alert的值设置为字典
        filecast.setAlert("今日天气", "", "今日可能下雨🌂");
        filecast.setBadge(0);
        filecast.setSound("default");
        //  set 'production_mode' to 'true' if your app is under production mode
        filecast.setTestMode();
        client.send(filecast);
    }


}
