package com.base.locationsharewhite.notification

import android.content.Context
import android.os.Handler
import android.os.HandlerThread
import com.base.locationsharewhite.fcm.MsgMgr

object NotificationHoverUtils {

    private val TAG = "NotificationHoverUtils"
    private var handlerThread: HandlerThread? = null
    private var handler: Handler? = null


    /**
     * 发送悬停通知
     */
    fun sendHoverNotification(context: Context, hoverCount: Int, hoverDelay: Int) {
        if (handlerThread == null) {
            handlerThread = HandlerThread("NotificationHandlerThread")
            handlerThread?.start()
        }

        // 创建 Handler
        if (handler == null) {
            handlerThread?.let {
                handler = Handler(it.getLooper())
            }
        }

        for (i in 1..hoverCount) {
            val time = (i * hoverDelay * 1000 + 300).toLong()
            handler?.postDelayed(Runnable {
                //发送普通通知，不需要在悬停，这里就关闭了悬停
                MsgMgr.sendNotification(context, MsgMgr.hoverActionId, 0)
            }, time)
        }
    }

    fun stopNotificationHandler() {
        // 停止 HandlerThread
        if (handler != null) {
            handler?.removeCallbacksAndMessages(null)
        }
        if (handlerThread != null) {
            handlerThread?.quit()
            handlerThread = null
        }
        handler = null
    }
}