package com.zxdemo.service

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.graphics.BitmapFactory
import android.os.Build
import android.os.IBinder
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.swiftcleaner.chovey.R
import com.swiftcleaner.chovey.model.bean.Global
import com.swiftcleaner.chovey.model.bean.Global.FUNCTION_APP_MANAGER
import com.swiftcleaner.chovey.model.bean.Global.FUNCTION_BATTERY
import com.swiftcleaner.chovey.model.bean.Global.FUNCTION_CLEAN
import com.swiftcleaner.chovey.model.bean.Global.FUNCTION_LARGE_FILE
import com.swiftcleaner.chovey.view.activity.StartActivity

class PermanentNotifyService : Service() {


    companion object {

        var isRunning = false
        fun Context.startOmgNotification() {
            val intent = Intent(this, PermanentNotifyService::class.java)
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) {
                return
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(intent)
            } else {
                startService(intent)
            }
        }
    }


    @SuppressLint("ForegroundServiceType")
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

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

        return START_STICKY
    }


    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onDestroy() {
        isRunning = false
        super.onDestroy()
        // 取消监听
    }


    private fun createPermanentNotification(context: Context): Notification {

        val isOngoing = true //是否持续(为不消失的常驻通知)
        val channelName = "Foreground Service Channel"
        val channelId = "Service_Id"
        val category = Notification.CATEGORY_SERVICE
        val contentView = RemoteViews(context.packageName, R.layout.notifi_stay_small)
        val expendView = RemoteViews(context.packageName, R.layout.notify_stay_big)

        val cleanIntent = PendingIntent.getActivity(
            context, // context
            0, // request code
            Intent("CleanJunkActivity", null, context, StartActivity::class.java).apply {
                putExtra("actionId", FUNCTION_CLEAN)
            }, // intent
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE // flags
        )
        contentView.setOnClickPendingIntent(R.id.id_ll_clean, cleanIntent)
        expendView.setOnClickPendingIntent(R.id.id_ll_clean, cleanIntent)

        val appIntent = PendingIntent.getActivity(
            context, // context
            0, // request code
            Intent("AppManagerActivity", null, context, StartActivity::class.java).apply {
                putExtra("actionId", FUNCTION_APP_MANAGER)
            }, // intent
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE // flags
        )
        contentView.setOnClickPendingIntent(R.id.id_app_manager, appIntent)
        expendView.setOnClickPendingIntent(R.id.id_app_manager, appIntent)

        val batteryIntent = PendingIntent.getActivity(
            context, // context
            0, // request code
            Intent("CleanerActivity", null, context, StartActivity::class.java).apply {
                putExtra("actionId", FUNCTION_BATTERY)
            }, // intent
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE // flags
        )
        contentView.setOnClickPendingIntent(R.id.id_battery, batteryIntent)
        expendView.setOnClickPendingIntent(R.id.id_battery, batteryIntent)

        val largeIntent = PendingIntent.getActivity(
            context, // context
            0, // request code
            Intent(
                "LargeFileCleanActivity",
                null,
                context,
                StartActivity::class.java
            ).apply {
                putExtra("actionId", FUNCTION_LARGE_FILE)
            }, // intent
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE // flags
        )
        contentView.setOnClickPendingIntent(R.id.id_large_file, largeIntent)
        expendView.setOnClickPendingIntent(R.id.id_large_file, largeIntent)

        val nfIntent = Intent(context, StartActivity::class.java)
        val pendingIntent =
            PendingIntent.getActivity(context, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE)
        val builder = NotificationCompat.Builder(context, channelId)

        builder.setSmallIcon(R.mipmap.logo) //设置状态栏内的小图标
        builder.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.icon_apk))
        builder.setContentTitle(context.resources.getString(R.string.app_name))
        builder.setContentIntent(pendingIntent) //设置PendingIntent
        builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) //设置通知公开可见
        builder.setAutoCancel(false)
        builder.priority = 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()
    }
}