package cn.palmte.work.utils.Umengpush;


import cn.palmte.work.utils.Umengpush.ios.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class IOSPushUtils {
	private static final String ALIAS_TYPE ="SW";
	private String appkey = null;
	private String appMasterSecret = null;
	private PushClient client = new PushClient();


	public IOSPushUtils(String key, String secret) {
		appkey = key;
		appMasterSecret = secret;
	}


    /**
     * IOS广播：全用户推送
     */
	public String sendIOSBroadcast(String title, String text, JSONObject fieldObject) throws Exception {
		IOSBroadcast broadcast = new IOSBroadcast(appkey,appMasterSecret);
        //alert值设置为字符串
		//broadcast.setAlert("IOS 广播测试");
		//alert的值设置为字典
		broadcast.setAlert(title , "" , text);
		broadcast.setCustomizedField("extras",fieldObject);//自定义key：自定义内容
		broadcast.setBadge( 0);
		broadcast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		broadcast.setProductionMode();
		// Set customized fields
		broadcast.setCustomizedField("test", "helloworld");
		return client.send(broadcast);
	}
	
	public void sendIOSUnicast() throws Exception {
		IOSUnicast unicast = new IOSUnicast(appkey,appMasterSecret);
		// TODO Set your device token
		unicast.setDeviceToken( "your device_token");
		//alert值设置为字符串
		//unicast.setAlert("IOS 单播测试");
		//alert的值设置为字典
		unicast.setAlert("今日天气" , "" , "今日可能下雨🌂");
		unicast.setBadge( 0);
		unicast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		unicast.setProductionMode();
		// Set customized fields
		unicast.setCustomizedField("test", "helloworld");
		client.send(unicast);
	}

	
	public String sendIOSGroupcast(String tag, String title, String text, JSONObject fieldObject) throws Exception {
		IOSGroupcast groupcast = new IOSGroupcast(appkey,appMasterSecret);
		/*  TODO
		 *  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", tag);
		tagArray.add(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(title , "" , text);
		groupcast.setCustomizedField("extras",fieldObject);//自定义key：自定义内容
		groupcast.setBadge( 0);
		groupcast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		groupcast.setProductionMode();
		return client.send(groupcast);
	}
	
	public String sendIOSCustomizedcast(String alias, String aliasType, String title, String text, JSONObject fieldObject) throws Exception {
		IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey,appMasterSecret);
		// TODO 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, aliasType);
		//customizedcast.setAlert("IOS 个性化测试");
		//alert的值设置为字典
		customizedcast.setAlert(title , "" , text);
		customizedcast.setCustomizedField("extras",fieldObject);//自定义key：自定义内容
		customizedcast.setBadge( 0);
		customizedcast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		customizedcast.setProductionMode();
		return client.send(customizedcast);
	}
	
	public void sendIOSFilecast() throws Exception {
		IOSFilecast filecast = new IOSFilecast(appkey,appMasterSecret);
		// TODO 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");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		filecast.setProductionMode();
		client.send(filecast);
	}
	

}
