package pxds.iqiv.ioih.gxph

import android.app.PendingIntent
import android.app.PendingIntent.FLAG_MUTABLE
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.RemoteViews
import com.blankj.utilcode.util.SPUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.test.basd.supercleanermax.R
import pxds.etmo.lexn.ylyy.Anet
import pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.Lwfm
import pxds.lmmd.zmwf.Uawv
import pxds.lmmd.zmwf.Uloz
import pxds.lmmd.zmwf.Wspd
import pxds.vazp.Uenj.Companion.ID_APP_MANAGER
import pxds.vazp.Uenj.Companion.ID_BATTERY_PUSH
import pxds.vazp.Uenj.Companion.ID_CLEAN_NOTIFICATION
import pxds.vazp.Uenj.Companion.ID_JUNK_CLEAN_PUSH
import pxds.vazp.Uenj.Companion.ID_LARGE_FILE_PUSH
import pxds.vazp.Uenj.Companion.ID_NETWORK_TRAFFIC
import pxds.vazp.Uenj.Companion.ID_PHOTO_COMPRESS
import pxds.vazp.Uenj.Companion.ID_RECENT_USE_APP
import pxds.vazp.Uenj.Companion.ID_SIMILAR_IMAGE
import kotlin.random.Random

object Vmuc {

    val supportNotification = arrayListOf(
        ID_JUNK_CLEAN_PUSH, ID_LARGE_FILE_PUSH, ID_BATTERY_PUSH, ID_CLEAN_NOTIFICATION, ID_PHOTO_COMPRESS, ID_APP_MANAGER, ID_NETWORK_TRAFFIC, ID_CLEAN_NOTIFICATION, ID_RECENT_USE_APP, ID_SIMILAR_IMAGE, )

    val notificationId = 19086

    private val TAG = R.string.ޅޣޓޢފ_D.string()

    fun filterAction(pushCircleOrder: List<Int>): ArrayList<Int> {

        val filterList = arrayListOf<Int>()
        pushCircleOrder.forEach { action ->
            if (supportNotification.contains(action)) {
                filterList.add(action)
            }
        }
        return filterList
    }

    fun Context.postActionNotification(actionId: Int, extra: Int? = null, pushStayTime: Long? = null) {

        if (!Uawv.allNotification) {
            return
        }

        val remoteViews = RemoteViews(packageName, R.layout.tmozvr)
        when (actionId) {
            ID_JUNK_CLEAN_PUSH -> {
                remoteViews.setImageViewResource(R.id.yukpgfw, R.mipmap.jrszgjth)
                remoteViews.setTextViewText(R.id.iiyuoks, R.string.ޢޖސތއ_D.string())
                remoteViews.setTextViewText(R.id.kfkfptx, R.string.ޤރގޘދ_D.string())
            }

            ID_LARGE_FILE_PUSH -> {
                remoteViews.setImageViewResource(R.id.yukpgfw, R.mipmap.qkrgpvzn)
                remoteViews.setTextViewText(R.id.iiyuoks, R.string.ޚނޅޥދ_D.string())
                remoteViews.setTextViewText(R.id.kfkfptx, R.string.ޤރގޘދ_D.string())
            }

            ID_PHOTO_COMPRESS -> {
                remoteViews.setImageViewResource(R.id.yukpgfw, R.mipmap.onnpagee)
                remoteViews.setTextViewText(R.id.iiyuoks, R.string.ޝޙޥޑޛ_D.string())
                remoteViews.setTextViewText(R.id.kfkfptx, R.string.ޓލގޖޅ_D.string())
            }

            ID_APP_MANAGER -> {
                remoteViews.setImageViewResource(R.id.yukpgfw, R.mipmap.aokjzubh)
                remoteViews.setTextViewText(R.id.iiyuoks, R.string.މގތޞޡ_D.string())
                remoteViews.setTextViewText(R.id.kfkfptx, R.string.ޙޛއށޢ_D.string())
            }

            ID_NETWORK_TRAFFIC -> {
                remoteViews.setImageViewResource(R.id.yukpgfw, R.mipmap.vowqbazu)
                remoteViews.setTextViewText(R.id.iiyuoks, R.string.ސޖމތޅ_D.string())
                remoteViews.setTextViewText(R.id.kfkfptx, R.string.ޡޏޥޑޢ_D.string())
            }

            ID_CLEAN_NOTIFICATION -> {
                remoteViews.setImageViewResource(R.id.yukpgfw, R.mipmap.hxdylsvj)
                remoteViews.setTextViewText(R.id.iiyuoks, R.string.ޅނޞޑޒ_D.string())
                remoteViews.setTextViewText(R.id.kfkfptx, R.string.ޡޏޥޑޢ_D.string())
            }

            ID_RECENT_USE_APP -> {
                remoteViews.setImageViewResource(R.id.yukpgfw, R.mipmap.qdurhfnw)
                remoteViews.setTextViewText(R.id.iiyuoks, R.string.ޖވޣދޥ_D.string())
                remoteViews.setTextViewText(R.id.kfkfptx, R.string.ޙޛއށޢ_D.string())
            }

            ID_SIMILAR_IMAGE -> {
                remoteViews.setImageViewResource(R.id.yukpgfw, R.mipmap.nzyhblgp)
                remoteViews.setTextViewText(R.id.iiyuoks, R.string.ޏދށސޔ_D.string())
                remoteViews.setTextViewText(R.id.kfkfptx, R.string.ޤރގޘދ_D.string())
            }

            else -> {
                return
            }
        }

        val flag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) FLAG_MUTABLE else FLAG_UPDATE_CURRENT

        val cancelIntent = Intent(this, Fnmj::class.java)
        val cancelRequestCode = Random.nextInt(0, 1000)
        val cancelPendingIntent = PendingIntent.getBroadcast(Lwfm.context, cancelRequestCode, cancelIntent, flag)
        remoteViews.setOnClickPendingIntent(R.id.zwrmzpf, cancelPendingIntent)

        val intent = Intent(applicationContext, Anet::class.java)
        val title: String = R.string.ށޓސހކ_D.string()
        val desc: String = R.string.ށޓސހކ_D.string()
        intent.putExtra(R.string.ޔރނރޑ_D.string(), title)
        intent.putExtra(R.string.ޖޥޅހޥ_D.string(), desc)
        val customKey = R.string.ޥޚޅރޤ_D.string()
        intent.putExtra(customKey, actionId)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        val bundle = Bundle()
        bundle.putInt(customKey, actionId)
        intent.putExtras(bundle)
        val uri = Uri.parse(R.string.ޣހފމޝ_D.string((actionId).toString()))
        intent.data = uri

        val btnRequestCode = Random.nextInt(0, 1000)
        val btnPendingIntent = PendingIntent.getActivity(Lwfm.context, btnRequestCode, intent, flag)
        remoteViews.setOnClickPendingIntent(R.id.ueisict, btnPendingIntent)

        SPUtils.getInstance().put(actionId.toString(), System.currentTimeMillis())

        SPUtils.getInstance().put(R.string.ޤޛފގޖ_D.string(), System.currentTimeMillis())
        Wspd.event(
            R.string.ރދޟޚޛ_D.string(), R.string.މޒބޓޅ_D.string((actionId).toString())
        )
        val requestCode = Random.nextInt(0, 1000)

        val pendingIntent = PendingIntent.getActivity(applicationContext, requestCode, intent, FLAG_UPDATE_CURRENT or FLAG_MUTABLE)
        Ytdi.showNotification(
            applicationContext, notificationId, remoteViews, R.id.zwrmzpf, pendingIntent, pushStayTime ?: 0
        )

    }

    fun getPresentPushId(): Int {
        val pushCircleOrder = Uloz.getSpConfigBean().push_circle_order
        Log.d(TAG, R.string.ގޟގޓޕ_D.string((pushCircleOrder).toString()))
        val tempList = arrayListOf<Int>()

        val json = SPUtils.getInstance().getString(R.string.ޡކހއޒ_D.string(), R.string.ށޓސހކ_D.string())
        if ((json == R.string.ށޓސހކ_D.string()) or (json == "null")) {
            val filterList = filterAction(pushCircleOrder)
            val initJson = Gson().toJson(filterList)
            SPUtils.getInstance().getString(R.string.ޡކހއޒ_D.string(), initJson)
            tempList.addAll(filterList)
        } else {
            var array = emptyArray<Int>()
            runCatching {
                array = Gson().fromJson(json, object : TypeToken<Array<Int>>() {})
            }
            tempList.addAll(array)

            if (tempList.isEmpty()) {
                val filterList = filterAction(pushCircleOrder)
                val saveJson = Gson().toJson(filterList)
                SPUtils.getInstance().put(R.string.ޡކހއޒ_D.string(), saveJson)
                tempList.addAll(filterList)
            }
        }

        val i = tempList[0]
        tempList.remove(i)

        val usedJson = Gson().toJson(tempList)
        SPUtils.getInstance().put(R.string.ޡކހއޒ_D.string(), usedJson)
        return i
    }
}
