package com.cooleshow.teacher.push;

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


import com.cooleshow.base.utils.LogUtils;

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

import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

/**
 * 自定义JPush message 接收器,包括操作tag/alias的结果返回(仅仅包含tag/alias新接口部分)
 */
public class MyJPushMessageReceiver extends JPushMessageReceiver {

    private String memo;

    /**
     * 用户点击打开了通知
     * @param context
     * @param notificationMessage
     */
    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        startActivity(context, notificationMessage);
    }

    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onCheckTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage);
    }

    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onMobileNumberOperatorResult(context, jPushMessage);
    }

    public void startActivity(Context context, NotificationMessage notificationMessage) {
        LogUtils.e(notificationMessage.notificationExtras);
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject(notificationMessage.notificationExtras);
//            memo = jsonObject.getString("memo");
//            Log.i("JPushMessageReceiver","memo:"+memo);
        } catch (JSONException e) {
            e.printStackTrace();
        }


//        MainActivity mainActivity = MainActivity.instance;
//        if (ActivityManager.getInstance().isOpenActivity(MainActivity.class)&&mainActivity != null) {
//            mainActivity.startActivity(memo);
//            LOG.e("OpenActivity");
//        } else {
//            Intent i = new Intent(context, MainActivity.class);
//            i.putExtra("memo", memo);
//            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            context.startActivity(i);
//            LOG.e("startActivity");
//        }
    }

}
