package com.xty.common.receiver

import android.content.Context
import android.os.Bundle
import cn.jpush.android.api.JPushInterface
import cn.jpush.android.api.NotificationMessage
import cn.jpush.android.service.JPushMessageReceiver
import com.google.gson.Gson
import com.xty.common.Const
import com.xty.common.LogUtils
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.getImageUrl
import com.xty.common.model.PushBean
import com.xty.common.util.CommonUtils
import me.leolin.shortcutbadger.ShortcutBadger


class JpushReceive : JPushMessageReceiver() {
    override fun onNotifyMessageArrived(p0: Context?, notificationMessage: NotificationMessage?) {
        super.onNotifyMessageArrived(p0, notificationMessage)
        LogUtils.e("onNotifyMessageArrived:${notificationMessage.toString()}")
        //添加角标
        ShortcutBadger.applyCount(p0, 1)
    }

    override fun onNotifyMessageOpened(p0: Context, notificationMessage: NotificationMessage) {
        super.onNotifyMessageOpened(p0, notificationMessage)
        LogUtils.e("onNotifyMessageOpened:${notificationMessage.toString()}")
        val str: String = notificationMessage.notificationExtras
        var title = notificationMessage.notificationTitle
        var content = notificationMessage.notificationContent
        try {
            if (Const.mainLiveData.value == null) {
                LogUtils.e("onNotifyMessageOpened mainLiveData is null!")
                return
            }

            var gson = Gson()
            var bean = gson.fromJson(str, PushBean::class.java)
            if(bean.type.isNullOrEmpty()){
                val bundle = Bundle()
                bundle.putString("content", title)
                RouteManager.goAct(ARouterUrl.MESSAGE_DETAIL, bundle)
                return
            }
            //1 消息 2 会话
            when (bean.type) {
                "1" -> {
                    val bundle = Bundle()
                    bundle.putString("content", content)
                    RouteManager.goAct(ARouterUrl.MESSAGE_DETAIL, bundle)
                }
                "2" -> {
                    CommonUtils.getMyUserInfo()?.let {
                        var image = bean.avatarUrl.replace("\\", "")
                        val bundle = Bundle()
                        bundle.putString("title", bean.name)
                        bundle.putString("toUserId", bean.toUserId)
                        bundle.putString("fromUserId", it.id.toString())
                        bundle.putString("chatImage", getImageUrl(image))
                        RouteManager.goAct(ARouterUrl.CHAT_MSG, bundle)
                    }
                }
                else->{
                    val bundle = Bundle()
                    bundle.putString("content", title)
                    RouteManager.goAct(ARouterUrl.MESSAGE_DETAIL, bundle)
                }
            }

        } catch (throwable: Throwable) {
            LogUtils.e("onNotifyMessageOpened Exception:${throwable.message};;${throwable.cause}")
        }
    }

    /**
     * 清除通知回调
     */
    override fun onNotifyMessageDismiss(p0: Context?, p1: NotificationMessage?) {
        super.onNotifyMessageDismiss(p0, p1)
        JPushInterface.setBadgeNumber(p0, 0)
        //删除角标
        ShortcutBadger.applyCount(p0, 0)
    }

}