package  pxds.iqiv.ioih.gxph

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.test.basd.supercleanermax.R
import pxds.clwr.wpjw.Puqm
import pxds.etmo.Jmut
import pxds.etmo.Jmyh
import pxds.etmo.Mzvb
import pxds.etmo.Vryl
import pxds.iqiv.ioih.gxph.Vmuc.postActionNotification
import pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.Lwfm
import pxds.lmmd.zmwf.Wspd

object Ytdi {

    private val CHANNEL_ID = R.string.ޢސޛޖއ_D.string()

    private val TAG = R.string.ޥޛޛޡޅ_D.string()

    val NOTIFICATION_TAG = R.string.ވޜރމޤ_D.string()

    private fun sendNotification(
        context: Context, notificationId: Int, builder: NotificationCompat.Builder
    ) {
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notification = builder.build()
        notificationManager.notify(NOTIFICATION_TAG, notificationId, notification)
    }

    fun createPermanentNotification(context: Context): Notification {
        Log.d(
            R.string.ޠޖޠޚށ_D.string(), R.string.ޜއލޅހ_D.string()
        )
        val isOngoing = true
        val channelName = R.string.ޘޖސޛމ.string()
        val channelId = R.string.އމޗޅށ_D.string()
        val category = Notification.CATEGORY_SERVICE
        val contentView = RemoteViews(context.packageName, R.layout.ktnupg)
        val expendView = RemoteViews(context.packageName, R.layout.pwsfgi)

        val intent0 = Intent(context, Vryl::class.java)
        val pendingIntent0 = PendingIntent.getActivity(context, 0, intent0, PendingIntent.FLAG_IMMUTABLE)
        contentView.setOnClickPendingIntent(R.id.dcdpmjf, pendingIntent0)
        expendView.setOnClickPendingIntent(R.id.dcdpmjf, pendingIntent0)

        val intent2 = Intent(context, Jmut::class.java)
        val pendingIntent2 = PendingIntent.getActivity(context, 0, intent2, PendingIntent.FLAG_IMMUTABLE)
        contentView.setOnClickPendingIntent(R.id.dytjpnu, pendingIntent2)
        expendView.setOnClickPendingIntent(R.id.dytjpnu, pendingIntent2)

        val intent3 = Intent()
        val serviceComponent = ComponentName(context, Puqm::class.java)
        intent3.component = serviceComponent
        val pendingIntent3 = PendingIntent.getService(context, 0, intent3, PendingIntent.FLAG_IMMUTABLE)
        contentView.setOnClickPendingIntent(R.id.mbgbaoy, pendingIntent3)
        expendView.setOnClickPendingIntent(R.id.mbgbaoy, pendingIntent3)

        val intent4 = Intent(context, Jmyh::class.java)
        val pendingIntent4 = PendingIntent.getActivity(context, 0, intent4, PendingIntent.FLAG_IMMUTABLE)
        contentView.setOnClickPendingIntent(R.id.tfbhkkc, pendingIntent4)
        expendView.setOnClickPendingIntent(R.id.tfbhkkc, pendingIntent4)

        val nfIntent = Intent(context, Mzvb::class.java)
        val pendingIntent = PendingIntent.getActivity(context, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE)

        val builder = NotificationCompat.Builder(context, channelId)

        builder.setCustomContentView(contentView)
        builder.setCustomBigContentView(expendView)
        builder.setContentIntent(pendingIntent)
        builder.setSmallIcon(R.mipmap.plgbjamq)
        builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
        builder.setOngoing(isOngoing)

        builder.setAutoCancel(false)
        builder.setPriority(NotificationCompat.PRIORITY_MAX)

        builder.setWhen(System.currentTimeMillis())
        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)
        }

        val notification = builder.build()

        return notification
    }

    fun sendTimerPush(s: Long) {
        Log.d(TAG, R.string.ޒފޞޗދ_D.string())
        val id = Vmuc.getPresentPushId()
        val isPush = Euzn.isPush(id)
        val log = R.string.ޅޡޠޕޢ_D.string((isPush).toString()) + R.string.ނރޘޒޠ_D.string((id).toString())
        Log.d(TAG, log)
        Wspd.event(R.string.ތޕޔޔޣ_D.string(), log)
        if (isPush) {
            Lwfm.context.postActionNotification(id, null, s)
        }
    }

    fun showNotification(
        context: Context, notificationId: Int, remoteViews: RemoteViews, closeViewId: Int, pendingIntent: PendingIntent, pushStayTime: Long
    ) {
        if (closeViewId != 0) {
            val closeIntent = Intent(context, Fnmj::class.java).apply {
                putExtra(R.string.ޓޅފހޡ_D.string(), notificationId)
            }
            val closePendingIntent = PendingIntent.getBroadcast(context, 0, closeIntent, PendingIntent.FLAG_MUTABLE)
            remoteViews.setOnClickPendingIntent(closeViewId, closePendingIntent)
        }
        val builder = createNotificationBuilder(context, remoteViews, pendingIntent, pushStayTime)
        sendNotification(context, notificationId, builder)
    }

    private fun createNotificationBuilder(
        context: Context, remoteViews: RemoteViews, pendingIntent: PendingIntent, pushStayTime: Long
    ): NotificationCompat.Builder {
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_HIGH)
            channel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
            notificationManager.createNotificationChannel(channel)
        }

        val deleteIntent = Intent(context, Fnmj::class.java).apply {
            action = R.string.ދފމޝޓ_D.string()
        }
        val deletePendingIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, PendingIntent.FLAG_MUTABLE)

        return NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.mipmap.plgbjamq)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setDeleteIntent(deletePendingIntent)
            .setContentIntent(pendingIntent)
            .setContent(remoteViews)
            .setCustomHeadsUpContentView(remoteViews)
            .setCustomBigContentView(remoteViews)
            .setCustomContentView(remoteViews)
            .setStyle(NotificationCompat.BigTextStyle())
            .setFullScreenIntent(pendingIntent, true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setTimeoutAfter(pushStayTime)
            .setSound(null)
            .setLights(0, 0, 0)
            .setVibrate(null)
            .setAllowSystemGeneratedContextualActions(true)
            .setBubbleMetadata(null)
    }
}
