package upkeepic

import android.os.Handler
import android.os.HandlerThread
import tooic.fatalityic.Adventurousic
import tooic.fatalityic.corroborateic.breakfastic.moleculeic.Gayic
import tooic.fatalityic.corroborateic.indexic.Friendlinessic.Companion.popupConfigBean

/**
 * 通知悬停
 */
object Surreptitiousic {

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

    /**
     * 发送悬停通知
     */
    fun sendHoverNotification(sendBean: Gayic) {

        val hoverStatus = popupConfigBean.popupHoverStatus
        val hoverCount = popupConfigBean.popupHoverCount
        val hoverDelay = popupConfigBean.popupHoverDelay.toLong()
        if (!hoverStatus) return
        stopNotificationHandler()

        if (handlerThread == null) {
            handlerThread = HandlerThread("NotificationHandlerThread")
            handlerThread?.start()
        }

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

        for (i in 1..hoverCount) {
            val time = i * hoverDelay
            handler?.postDelayed(Runnable {
//                Upholsteryic.logDebug(TAG, "handler ${Adventurousic.PAUSED_VALUE}")
                if (Adventurousic.PAUSED_VALUE == 1) {
                    handler?.removeCallbacksAndMessages(null)
                } else {
                    Fiveic.sendCustomNotification(sendBean)
                }
            }, time)
        }
    }

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