package com.base.datarecovery.adapter

import android.annotation.SuppressLint
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.base.datarecovery.R
import com.base.datarecovery.bean.ConstObject.APP_MANAGER
import com.base.datarecovery.bean.ConstObject.APP_PROCESS
import com.base.datarecovery.bean.ConstObject.BATTERY_INFO
import com.base.datarecovery.bean.ConstObject.JUNK_CLEANER
import com.base.datarecovery.bean.ConstObject.LARGE_FILE
import com.base.datarecovery.bean.ConstObject.PRIVACY_SPACE
import com.base.datarecovery.bean.ConstObject.RECOVERY_AUDIOS
import com.base.datarecovery.bean.ConstObject.RECOVERY_DOCUMENTS
import com.base.datarecovery.bean.ConstObject.RECOVERY_PHOTOS
import com.base.datarecovery.bean.ConstObject.RECOVERY_VIDEOS
import com.base.datarecovery.bean.ConstObject.REPEAT_PHOTOS
import com.base.datarecovery.bean.ConstObject.SCREENSHOT_CLEANER
import com.base.datarecovery.bean.ConstObject.WHATSAPP_CLEANER
import com.base.datarecovery.databinding.ItemResultFun2Binding
import com.base.datarecovery.utils.AppPreferences
import com.base.datarecovery.view.XmlEx.inflate
import java.util.Collections

class AppFunctionAdapter(val click: (name: String) -> Unit) :
    RecyclerView.Adapter<AppFunctionAdapter.JJJ>() {

    val list = arrayListOf(
        Fun(JUNK_CLEANER, R.mipmap.clean),
        Fun(RECOVERY_PHOTOS, R.mipmap.photos_ss),
        Fun(RECOVERY_VIDEOS, R.mipmap.videos_ss),
        Fun(RECOVERY_DOCUMENTS, R.mipmap.documents_ss),
        Fun(RECOVERY_AUDIOS, R.mipmap.h_audio),
        Fun(PRIVACY_SPACE, R.mipmap.space),
        Fun(REPEAT_PHOTOS, R.mipmap.repeatedphotos),
        Fun(SCREENSHOT_CLEANER, R.mipmap.screenshots),
        Fun(WHATSAPP_CLEANER, R.mipmap.whatsapp),
        Fun(LARGE_FILE, R.mipmap.large),
        Fun(APP_PROCESS, R.mipmap.h_appprocess),
        Fun(APP_MANAGER, R.mipmap.h_appmanager),
        Fun(BATTERY_INFO, R.mipmap.h_battery),
    )

    class JJJ(view: View) : ViewHolder(view)


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JJJ {
        return JJJ(R.layout.item_result_fun_2.inflate(parent))
    }

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

    override fun onBindViewHolder(holder: JJJ, position: Int) {
        val data = list[position]
        val context = holder.itemView.context
        val binding = ItemResultFun2Binding.bind(holder.itemView)
        binding.ivIcon.setImageDrawable(ContextCompat.getDrawable(context, data.icon))
        binding.tvTittle.text = data.name
//        binding.tvDes.text = data.des
//        binding.tvButton.text = data.button
        binding.root.setOnClickListener {
            click.invoke(data.name)
        }
    }


    @SuppressLint("NotifyDataSetChanged")
    fun removeItem(name: String) {
        list.removeIf { it.name == name }
        notifyDataSetChanged()
    }

    data class Fun(
        val name: String = "",
        val icon: Int = 0,
        val des: String = "",
        val button: String = "",
    )

    fun updateListPosition() {
        //本次进入结果页，判断使用垃圾的功能是否超过5分钟
        val lastUseJunkCleaner = AppPreferences.getInstance().getLong("last_use_junk_cleaner", 0)
        if ((System.currentTimeMillis() - lastUseJunkCleaner) >= 60 * 5 * 1000) {
        } else {
            Collections.rotate(list, list.size - 1)
        }
    }

}