package com.base.locationsharewhite.service

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.job.JobInfo
import android.app.job.JobParameters
import android.app.job.JobScheduler
import android.app.job.JobService
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.graphics.BitmapFactory
import android.graphics.drawable.Icon
import android.os.Build
import android.os.CountDownTimer
import android.provider.MediaStore
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.graphics.drawable.IconCompat
import androidx.work.Configuration
import com.base.locationsharewhite.R
import com.base.locationsharewhite.service.StayNotification.createPermanentNotification
import com.base.locationsharewhite.utils.LogEx
import kotlin.random.Random


/**
 *  常驻通知栏
 */
class StayJobService : JobService() {

    init {
        val builder = Configuration.Builder()
        builder.setJobSchedulerJobIdRange(0, 1000)
    }

    private val TAG = "StayJobService"

    companion object {

        private val NOTIFICATION_PERMANENT_ID = 186


        var isRunning = false
        private const val JOB_INFO_ID: Int = 101
        private const val JOB_PERIODIC: Long = 5 * 1000L
        fun Context.startJob() {
            if (isRunning) return
            val jobScheduler = getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler
            val componentName = ComponentName(this, StayJobService::class.java)
            val jobInfo = JobInfo.Builder(JOB_INFO_ID, componentName)
                .setMinimumLatency(30000)
                .build()
            jobScheduler.schedule(jobInfo)
        }

    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        return super.onStartCommand(intent, flags, startId)

    }

    private fun startForeground() {
        val notification = createPermanentNotification(applicationContext)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            startForeground(
                NOTIFICATION_PERMANENT_ID,
                notification,
                ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
            )
        } else {
            startForeground(NOTIFICATION_PERMANENT_ID, notification)
        }
        isRunning = true
    }

    private fun notifyForeground() {
        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        notificationManager.notify(
            NOTIFICATION_PERMANENT_ID,
            createPermanentNotification(applicationContext)
        )
    }

    override fun onDestroy() {
        isRunning = false
        super.onDestroy()
    }

    override fun onCreate() {
        LogEx.logDebug(TAG, "onCreate isRunning=$isRunning")
        if (!isRunning) {
            isRunning = true
            startForeground()
            Timer().start()
        }
        super.onCreate()
    }

    override fun onStartJob(params: JobParameters?): Boolean {
        return true
    }

    override fun onStopJob(params: JobParameters?): Boolean {
        return false
    }

    private inner class Timer() : CountDownTimer(30000, 1000) {
        override fun onTick(millisUntilFinished: Long) {

        }

        override fun onFinish() {
            notifyForeground()
            Timer().start()
        }

    }
}

