package com.analysys.compatibilitydemo.push;

import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.analysys.AnalysysAgent;
import com.analysys.push.Constants;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;


public class AliyunMessageReceiver extends MessageReceiver {
    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "receiver";
    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        // TODO 处理推送通知
        Log.e("AliyunMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);

        try {
            JSONObject jsonObject = new JSONObject();
            if (extraMap != null) {
                jsonObject.put(Constants.PUSH_KEY_INFO, extraMap.get(Constants.PUSH_KEY_INFO));
            }
            AnalysysAgent.trackCampaign(context,jsonObject.toString(),false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        Log.e("AliyunMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    }
    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        Log.e("AliyunMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);

        try {
            JSONObject extraJson = new JSONObject(extraMap);
            JSONObject jsonObject = new JSONObject();
            if (extraMap != null) {
                jsonObject.put(Constants.PUSH_KEY_INFO, extraJson.get(Constants.PUSH_KEY_INFO));
            }
            AnalysysAgent.trackCampaign(context,jsonObject.toString(),true);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        Log.e("AliyunMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }
    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        Log.e("AliyunMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }
    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        Log.e("AliyunMessageReceiver", "onNotificationRemoved");
    }
}