package pxds.hpls.xkxo

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.CyvafqBinding
import com.test.basd.supercleanermax.databinding.OtskfhBinding
import pxds.dlpf.buoz.fico.Ipnk.inflate
import pxds.lmmd.zmwf.Glro
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.Onaw
import pxds.vazp.Rorl

class Fito(val activity: Activity, val modeUI: String = UI_LAUNCHES_MODE) : RecyclerView.Adapter<Fito.DDD>() {

    private var appBeans = arrayListOf<Rorl>()

    private var numberMode = UI_MODE_ALL

    override fun getItemCount(): Int {
        return appBeans.size
    }

    fun setNumberMode(mode: Int) {
        numberMode = mode
    }

    companion object {
        const val UI_MODE_ALL = 5
        const val UI_MODE_FOREGROUND = 25
        const val UI_MODE_BACKGROUND = 35

        val UI_LAUNCHES_MODE = R.string.ގރނޖޛ_D.string()
        val UI_SCREEN_TIME_MODE = R.string.ޢޏވޢނ_D.string()
    }

    fun setData(dataList: List<Rorl>) {
        appBeans.clear()
        appBeans.addAll(dataList)
        if (modeUI == UI_LAUNCHES_MODE) {
            when (numberMode) {
                UI_MODE_ALL -> appBeans.sortByDescending { it.launchTimes }
                UI_MODE_FOREGROUND -> appBeans.sortByDescending { it.foregroundTimes }
                UI_MODE_BACKGROUND -> appBeans.sortByDescending { it.backgroundTimes }
            }
        } else {
            appBeans.sortByDescending { it.screenTime }
        }
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DDD {
        return if (modeUI == UI_LAUNCHES_MODE) {
            DDD(R.layout.cyvafq.inflate(parent))
        } else {
            DDD(R.layout.otskfh.inflate(parent))
        }
    }

    override fun onBindViewHolder(holder: DDD, position: Int) {

        val context = holder.itemView.context
        val data = appBeans[position]

        if (modeUI == UI_LAUNCHES_MODE) {
            val binding = CyvafqBinding.bind(holder.itemView)

            binding.yukpgfw.setImageDrawable(data.icIcon)
            binding.ivrxehl.text = data.appName
            val number = when (numberMode) {
                UI_MODE_ALL -> data.launchTimes
                UI_MODE_FOREGROUND -> data.foregroundTimes
                UI_MODE_BACKGROUND -> data.backgroundTimes
                else -> 0
            }
            binding.gxuourp.text = R.string.ބމޅޖޑ_D.string((number).toString())
            binding.ufcjixb.background = if (data.isRunning) {
                ContextCompat.getDrawable(context, R.drawable.bmiobmwj)
            } else {
                ContextCompat.getDrawable(context, R.drawable.bkoyiheu)
            }
            binding.ufcjixb.setTrackedOnClickListener {
                if (Glro.canStop(activity, data.pkg)) {

                    val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)

                    intent.setData(Uri.parse(R.string.ޙތޛޥބ_D.string((data.pkg).toString())))
                    context.startActivity(intent)
                }
            }

            val bg = if (Glro.canStop(activity, data.pkg)) {
                R.drawable.bmiobmwj
            } else {
                R.drawable.bkoyiheu
            }
            binding.ufcjixb.background = ContextCompat.getDrawable(context, bg)
        }

        if (modeUI == UI_SCREEN_TIME_MODE) {
            val binding = OtskfhBinding.bind(holder.itemView)
            binding.yukpgfw.setImageDrawable(data.icIcon)
            binding.ivrxehl.text = data.appName
            binding.csxnlzs.setTrackedOnClickListener {

                val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)

                intent.setData(Uri.parse(R.string.ޙތޛޥބ_D.string((data.pkg).toString())))
                context.startActivity(intent)
            }
            binding.smokwld.text = Onaw.longToHoursAndMinutes(data.screenTime)
            val max = appBeans.maxOf { it.screenTime }
            binding.ekrhjrn.progress = ((data.screenTime.toFloat() / max.toFloat()) * 100).toInt()
        }
    }

    inner class DDD(view: View) : ViewHolder(view)
}