package com.xhly.manageapp

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.RecyclerView
import com.hjq.toast.Toaster
import com.xhly.corelib.bean.AppInfo
import com.xhly.corelib.utils.LogShow
import com.xhly.manageapp.databinding.ItemAppBinding
import java.util.Calendar

class AppAdapter(var context: Context, var data: ArrayList<AppInfo>) :
    RecyclerView.Adapter<AppAdapter.AppViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppViewHolder {
        val inflate = ItemAppBinding.inflate(LayoutInflater.from(context), parent, false)
        return AppViewHolder(inflate)
    }

    override fun getItemCount(): Int {
        return data.size+2
    }


    override fun onBindViewHolder(holder: AppViewHolder, position: Int) {
        if (position==data.size){
            AppCompatResources.getDrawable(context,R.drawable.icon_speed)
                ?.let { holder.mBinding.itemCv.setImageDrawable(it) }
            holder.mBinding.itemCv.setText(context.getString(R.string.speed))
            holder.mBinding.itemCv.setOnClickListener {
              try {
                  val service : ActivityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
                  val runningAppProcesses = service.runningAppProcesses
                  runningAppProcesses.forEach {process->
                      LogShow("当前运行应用名称"+process.processName+"|||"+process?.importanceReasonComponent?.packageName)
                      data.filter { it.packageName.equals(process.processName) }.forEach {
                          ManageApplication.killApplicationProcess(it.packageName)
                      }
                  }
                  Toaster.show("加速完成。")
              }catch (e:Exception){

              }
            }
        }else if (position==data.size+1){
            AppCompatResources.getDrawable(context,R.drawable.icon_clear)
                ?.let { holder.mBinding.itemCv.setImageDrawable(it) }
            holder.mBinding.itemCv.setText(context.getString(R.string.clear))
            holder.mBinding.itemCv.setOnClickListener {

            }
        }else{
            val appInfo = data[position]
            holder.mBinding.itemCv.setImageDrawable(appInfo.ico)
            holder.mBinding.itemCv.setText(appInfo.name)
            holder.mBinding.itemCv.setOnClickListener {
                var currentTimeMillis = System.currentTimeMillis()
                var instance = Calendar.getInstance()
                instance.set(Calendar.HOUR_OF_DAY, 11)
                instance.set(Calendar.MINUTE, 0)
                var timeInMillis = instance.timeInMillis
                context.startActivity(appInfo.intent)
                /* if (currentTimeMillis>=timeInMillis){
                    context.startActivity(appInfo.intent)
                }else{
                    Toaster.showLong("开放时间为"+timeInMillis.toFormat())
                }*/
            }
        }
    }


    class AppViewHolder(binding: ItemAppBinding) : RecyclerView.ViewHolder(binding.root) {
        var mBinding = binding
    }

}