package com.sinata.bc_chat.receiver;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.bc_chat.bc_base.utils.Const;
import com.bc_chat.im.service.CommentPushEntity;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.zhaohaoting.framework.utils.Logger;
import com.zhaohaoting.framework.utils.observer.ObserverManager;

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

import io.rong.push.PushType;
import io.rong.push.notification.PushMessageReceiver;
import io.rong.push.notification.PushNotificationMessage;

/**
 * author：Created by zhaohaoting on 2019/3/29
 * email：526309416@qq.com
 * desc：
 */
public class NotificationReceiver extends PushMessageReceiver {
    @Override
    public boolean onNotificationMessageArrived(Context context, PushType pushType, PushNotificationMessage pushNotificationMessage) {
//        String pushContent = pushNotificationMessage.getExtra();
//        if (!TextUtils.isEmpty(pushContent)) {
//            try {
//                JSONObject jsonObject = new JSONObject(pushContent);
//                String type = jsonObject.optString("pushType");
//                if (Const.PushMethod.DISS_GROUP.equals(type)) {
//                    String groupId = jsonObject.optString("groupId");
//                    RongIM.getInstance().clearMessages(Conversation.ConversationType.GROUP, groupId, new RongIMClient.ResultCallback<Boolean>() {
//                        @Override
//                        public void onSuccess(Boolean aBoolean) {
//                            RongIM.getInstance().removeConversation(Conversation.ConversationType.GROUP, groupId, null);
//                        }
//
//                        @Override
//                        public void onError(RongIMClient.ErrorCode errorCode) {
//                        }
//                    });
//                }
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//        }
        return false;
    }

    @Override
    public boolean onNotificationMessageClicked(Context context, PushType pushType, PushNotificationMessage pushNotificationMessage) {
        Logger.e("onNotificationMessageClicked", pushType + "===============" + pushNotificationMessage.getExtra() + " 1 " + pushNotificationMessage.getPushData() + " 2 " + pushNotificationMessage.getPushContent());

        if (String.valueOf(-1).equals(pushNotificationMessage.getSenderId())) {
            String isFromPush = !pushNotificationMessage.getSourceType().equals(PushNotificationMessage.PushSourceType.FROM_OFFLINE_MESSAGE) && !pushNotificationMessage.getSourceType().equals(PushNotificationMessage.PushSourceType.FROM_ADMIN) ? "false" : "true";
            Intent intent = new Intent();
            Uri.Builder builder = Uri.parse("rong://" + context.getPackageName()).buildUpon()
                    .appendPath("conversationlist").appendQueryParameter("isFromPush", isFromPush);
            JSONObject options = new JSONObject();
            JSONObject appData = new JSONObject();
            try {
//                {"content":"用户18123292546回复了你的评论。","extra":{"cid":"1026","commentId":"110","type":12}}
                appData.put("extra", new JSONObject(pushNotificationMessage.getPushData()).getJSONObject("extra"));
                options.put("appData", appData.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            intent.putExtra("options", options.toString());
            intent.setData(builder.build());
            intent.setPackage(context.getPackageName());
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
            return true;
        }
        return false;
    }

    @Override
    public void onThirdPartyPushState(PushType pushType, String action, long resultCode) {
        super.onThirdPartyPushState(pushType, action, resultCode);
    }
}
