package com.zxhyis.toolss.notification.service

import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.IBinder
import android.os.Process
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.NetworkUtils
import com.blankj.utilcode.util.ScreenUtils
import com.zxhyis.toolss.ad.ComUtils
import com.zxhyis.toolss.ad.bean.EventParams
import com.zxhyis.toolss.logic.LogEx
import com.zxhyis.toolss.notification.NotificationUtils
import com.zxhyis.toolss.notification.broadcast.ActionBroadcast
import com.zxhyis.toolss.notification.task.PeriodicTaskScheduler


class NotificationService : Service() {

    private val TAG = "NotificationService"

    companion object {
        var isRunning = false
    }

    private var eventParams: EventParams? = null

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        LogEx.logDebug(TAG, "onStartCommand")
        if (isRunning) {
            LogEx.logDebug(TAG, "Service is running" + Process.myPid())
            return START_NOT_STICKY
        }
        isRunning = true
        LogEx.logDebug(TAG, "Service is start " + Process.myPid())
        initEvenParams()
        if (eventParams != null) {
            startTimer()
        }

        initBroadcast()
        val notification = NotificationUtils.createPermanentNotification(applicationContext)
        startForeground(1, notification)
        return START_STICKY
    }

    private fun startTimer() {
        LogEx.logDebug(TAG, "========================startTimer======================")
        val pushManagement = ComUtils.getPushConfig()
        PeriodicTaskScheduler.instance?.setPushParams(pushManagement)
            ?.setEventParam(eventParams)?.start(applicationContext)
    }

    fun Context.initBroadcast() {
        LogEx.logDebug(TAG, "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)
        }
    }

    private fun initEvenParams() {
        eventParams = EventParams(
            ScreenUtils.getScreenHeight(),
            ScreenUtils.getScreenWidth(),
            DeviceUtils.getModel(),
            NetworkUtils.getNetworkType(),
            AppUtils.getAppVersionName(),
            DeviceUtils.getAndroidID()
        )
    }

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


        return null
    }

    override fun onDestroy() {
        LogEx.logDebug(TAG, "service is onDestroy")
        super.onDestroy()
    }
}