package rowedto.jockeyedto.sheetedto.dryedto.stableedto

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.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.graphics.drawable.IconCompat
import androidx.work.Configuration
import com.base.datarecovery.R
import rowedto.jockeyedto.sheetedto.atticedto.Moneyedto
import rowedto.jockeyedto.sheetedto.atticedto.fadeedto.Bottomedto
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Dishedto
import kotlin.random.Random


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

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

    private val TAG = "StayJobService"
    val channelName = "Photo Recovery Foreground Service Channel"
    val channelId = "Photo_Recovery_Service_Id"
    private val NOTIFICATION_PERMANENT_ID = 199

    companion object {

        var isRunning = false
        private const val JOB_INFO_ID: Int = 101
        fun Context.startJob() {
            isCancel = false
            if (isRunning) return
            val jobScheduler = getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler
            val componentName = ComponentName(this, Biggestedto::class.java)
            val jobInfo = JobInfo.Builder(JOB_INFO_ID, componentName)
                .setMinimumLatency(10000)
                .build()
            jobScheduler.schedule(jobInfo)
        }
        var isCancel: Boolean = false

        fun createPermanentNotification(context: Context): Notification {

            val channelName = "Permanent Foreground Service Channel"
            val channelId = "permanent_channel"
            val contentView = RemoteViews(context.packageName, R.layout.flyedto)
            val expendView = RemoteViews(context.packageName, R.layout.flyedto)

            val requestCode1 = Random.nextInt(1800)
            val intent0 = Intent(context, Bottomedto::class.java).apply {
                putExtra("actionId", Dynamicedto.ID_JUNK_CLEAN_PUSH)
            }
            val pendingIntent0 =
                PendingIntent.getActivity(context, requestCode1, intent0, PendingIntent.FLAG_IMMUTABLE)
            contentView.setOnClickPendingIntent(R.id.anythingedto, pendingIntent0)
            expendView.setOnClickPendingIntent(R.id.anythingedto, pendingIntent0)

            val requestCode2 = Random.nextInt(1800)
            val intent2 = Intent(context, Bottomedto::class.java).apply {
                putExtra("actionId", Dynamicedto.ID_RECOVERY_PHOTOS)
                putExtra("ScanType", Dynamicedto.SCAN_PHOTOS)
            }
            val pendingIntent2 =
                PendingIntent.getActivity(context, requestCode2, intent2, PendingIntent.FLAG_IMMUTABLE)
            contentView.setOnClickPendingIntent(R.id.wormedto, pendingIntent2)
            expendView.setOnClickPendingIntent(R.id.wormedto, pendingIntent2)

            val requestCode3 = Random.nextInt(1800)
            val intent3 = Intent(context, Bottomedto::class.java).apply {
                putExtra("actionId", Dynamicedto.ID_RECOVERY_VIDEOS)
                putExtra("ScanType", Dynamicedto.SCAN_VIDEOS)
            }
            val pendingIntent3 =
                PendingIntent.getActivity(context, requestCode3, intent3, PendingIntent.FLAG_IMMUTABLE)
            contentView.setOnClickPendingIntent(R.id.footballedto, pendingIntent3)
            expendView.setOnClickPendingIntent(R.id.footballedto, pendingIntent3)

            val requestCode4 = Random.nextInt(1800)
            val intent4 = Intent(context, Bottomedto::class.java).apply {
                putExtra("actionId", Dynamicedto.ID_RECOVERY_DOCUMENTS)
                putExtra("ScanType", Dynamicedto.SCAN_DOCUMENTS)
            }
            val pendingIntent4 =
                PendingIntent.getActivity(context, requestCode4, intent4, PendingIntent.FLAG_IMMUTABLE)
            contentView.setOnClickPendingIntent(R.id.stunningedto, pendingIntent4)
            expendView.setOnClickPendingIntent(R.id.stunningedto, pendingIntent4)


            val builder = NotificationCompat.Builder(context, channelId)
            val smallIcon = IconCompat.createFromIcon(
                context, Icon.createWithResource(
                    context, R.mipmap.senseedto
                )
            )

            smallIcon?.let {
                builder.setSmallIcon(smallIcon) //设置状态栏内的小图标
            }

            val nfIntent = Intent(context, Moneyedto::class.java)
            val pendingIntent =
                PendingIntent.getActivity(context, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE)
            builder.setLargeIcon(
                BitmapFactory.decodeResource(
                    context.resources,
                    R.mipmap.senseedto
                )
            )
            builder.setContentTitle(context.resources.getString(R.string.overchargeedto))
            builder.setContentIntent(pendingIntent) //设置PendingIntent
            builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) //设置通知公开可见
            builder.setAutoCancel(false)
            builder.setOngoing(true)
            builder.setPriority(NotificationCompat.PRIORITY_MAX) //优先级为：重要通知
            builder.setWhen(System.currentTimeMillis())
            builder.setCustomContentView(contentView)
            builder.setCustomBigContentView(expendView)

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel =
                    NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)
                channel.lockscreenVisibility = 1
                val notificationManager =
                    context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                notificationManager.createNotificationChannel(channel)
                builder.setChannelId(channelId)
            }
            return builder.build()
        }

    }

    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() {
        Dishedto.logDebug(TAG, "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() {
        Dishedto.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()
        }

    }


}

