package  com.test.basd.smartjunkcleaner.display

import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.IBinder


class NotificationService : Service() {


    companion object {
        var isRunning = false
    }



    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (isRunning) {
            stopSelf()
            return START_NOT_STICKY
        }
//        isRuning = true
//        Log.d("MyService", "Service is start" + Process.myPid());
//        initEvenParams()
//        eventParams?.let {
//            startTimer()
//        }
        initBroadcast()
        val notification = NotificationUtils.createPermanentNotification(applicationContext)
        startForeground(1, notification)
        return START_STICKY
    }

    private fun startTimer() {
//        val pushManagement = ComUtils.getPushConfig()
//        PeriodicTaskScheduler.getInstance().setPushParams(pushManagement)
//            .setEventParam(eventParams).start(applicationContext)
    }

    fun Context.initBroadcast() {
        val filter = IntentFilter()
        filter.addAction(Intent.ACTION_SCREEN_OFF)
        filter.addAction(Intent.ACTION_SCREEN_ON)
        filter.addAction(Intent.ACTION_USER_PRESENT)
        filter.addAction(Intent.ACTION_POWER_CONNECTED)
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED)
        filter.addAction(Intent.ACTION_BATTERY_CHANGED)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            registerReceiver(ActionBroadcast(), filter, Context.RECEIVER_EXPORTED)
        } else {
            registerReceiver(ActionBroadcast(), filter)
        }
    }



    override fun onBind(intent: Intent?): IBinder? {


        return null
    }

    override fun onDestroy() {
        isRunning = false
        super.onDestroy()
        // 取消订阅

    }
}