package com.xty.common.services

import android.content.Context
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.common.bracelet.YCBBlueConnectHelper
import com.xty.common.bracelet.YCBManager
import com.xty.common.Const
import com.xty.common.LogUtils
import com.xty.common.MMkvHelper
import com.xty.common.arouter.ARouterUrl

/**
 * 状态栏消息监听
 */
@Route(path = ARouterUrl.NOTIFICATION_LISTENER)
class NotifictionCollector: NotificationListenerService(),OrderProiver {

    override fun onNotificationPosted(sbn: StatusBarNotification) {
        super.onNotificationPosted(sbn)

        LogUtils.i("liang", "open" + "-----" + sbn.getPackageName());
        LogUtils.i("liang", "open" + "------" + sbn.getNotification().tickerText);
        LogUtils.i("liang", "open" + "-----" + sbn.getNotification().extras.get("android.title"));
        LogUtils.i("liang", "open" + "-----" + sbn.getNotification().extras.get("android.text"));
        var pageName=sbn.getPackageName()
        var ticker=sbn.getNotification().tickerText
        var androidTitle=sbn.getNotification().extras.get("android.title")
        var androidText=sbn.getNotification().extras.get("android.text")
        if(pageName==null||ticker==null||androidTitle==null||androidText==null){
            //错误的状态栏消息  不使用
            return
        }
        if(YCBBlueConnectHelper.getBlueStatus(false)){
            when( sbn.getPackageName()){
                "com.tencent.mobileqq"->{
                    if(receiveStatus(Const.QQ_PUSH)){
                        var name="QQ"
                        var content=sbn.getNotification().tickerText.toString()
                        YCBManager.openMsg(0x03,name,content)
                    }
                }
                "com.tencent.mm"->{
                    if(receiveStatus(Const.WX_PUSH)){
                        var name="微信"
                        var   content=sbn.notification.tickerText.toString()
                        YCBManager.openMsg(0x03,name,content)
                    }

                }
                "com.android.mms"->{
                    if(receiveStatus(Const.MSG_PUSH)){
                        var name="短信"
                        var  content="${sbn.getNotification().extras.get("android.title")}:${sbn.getNotification().extras.get("android.text")}"
                        YCBManager.openMsg(0x03,name,content)
                    }

                }
                //电话
                "com.android.incallui"->{
                    if(receiveStatus(Const.PHONE_PUSH)){
                        var  name=sbn.getNotification().extras.get("android.title").toString()
                        var  content=sbn.getNotification().extras.get("android.title").toString()
                        YCBManager.openMsg(0x00,name,content)
                    }

                }
            }

        }
    }


    private fun receiveStatus(type:String):Boolean {
        return MMkvHelper.getBoolean(type)
    }
    override fun onNotificationRemoved(sbn: StatusBarNotification?,) {
        super.onNotificationRemoved(sbn)
    }

    override fun init(context: Context?) {
    }
}