package cn.palmte.work.utils.Umengpush;

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


public class AndroidPushUtils {
	private static final String ALIAS_TYPE ="SW";


	private String appkey;
	private String appMasterSecret;
	private PushClient client = new PushClient();


	public AndroidPushUtils(){

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

	/**
	 * 安卓广播：全部用户推送
	 */
	public String sendAndroidBroadcast(String title, String text, JSONObject fieldObject) throws Exception {
		AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret);
		broadcast.setTicker( "Android broadcast ticker");
		broadcast.setTitle(title);
		broadcast.setText(text);
//		broadcast.setCustomField(fieldObject);
		broadcast.goCustomAfterOpen(fieldObject);
		broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO 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");
		return client.send(broadcast);
	}
	
	public void sendAndroidUnicast() throws Exception {
		AndroidUnicast unicast = new AndroidUnicast(appkey,appMasterSecret);
		// TODO Set your device token
		unicast.setDeviceToken( "your device_token");
		unicast.setTicker( "Android unicast ticker");
		unicast.setTitle(  "中文的title");
		unicast.setText(   "Android unicast text");
		unicast.goAppAfterOpen();
		unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO 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 String sendAndroidGroupcast(String tag, String title, String text, JSONObject fieldObject) throws Exception {
		AndroidGroupcast groupcast = new AndroidGroupcast(appkey,appMasterSecret);
		/*  TODO
		 *  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", tag);
//		TestTag.put("tag", "Test");
		tagArray.add(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( text);
		groupcast.goCustomAfterOpen(fieldObject);
		groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		groupcast.setChannelActivity("your channel activity");
		// TODO 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");
		return client.send(groupcast);
	}

	/**
	 * 安卓：customizedcast 自定义播：通过alias进行推送，对单个或者多个alias进行推送
	 * ALIAS_TYPE 我们固定为DZG，更多了解 请关注https://developer.umeng.com/docs/66632/detail/89996
	 * @param alias 别名
	 * @param title 标题
	 * @param text  内容
	 * @param fieldObject 自定义内容
	 */
	public String sendAndroidCustomizedcast(String alias, String aliasType, String title, String text, JSONObject fieldObject) throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
		// TODO 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, aliasType);
		customizedcast.setTicker( "Android customizedcast ticker");
		customizedcast.setTitle( title);
		customizedcast.setText( text);
//		customizedcast.setCustomField(fieldObject);
		customizedcast.goCustomAfterOpen(fieldObject);
		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO 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");
		return client.send(customizedcast);
	}
	
	public void sendAndroidCustomizedcastFile() throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
		// TODO 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);
		// TODO 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);
		// 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.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);
	}
	

	

}
