package com.base.filerecoveryrecyclebin.adapter

import android.annotation.SuppressLint
import android.app.Activity
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.filerecoveryrecyclebin.R
import com.base.filerecoveryrecyclebin.ads.AdmobMaxHelper
import com.base.filerecoveryrecyclebin.bean.ConstObject.APP_MANAGER
import com.base.filerecoveryrecyclebin.bean.ConstObject.APP_PROCESS
import com.base.filerecoveryrecyclebin.bean.ConstObject.BATTERY_INFO
import com.base.filerecoveryrecyclebin.bean.ConstObject.JUNK_CLEANER
import com.base.filerecoveryrecyclebin.bean.ConstObject.LARGE_FILE
import com.base.filerecoveryrecyclebin.bean.ConstObject.RECOVERY_AUDIOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.RECOVERY_DOCUMENTS
import com.base.filerecoveryrecyclebin.bean.ConstObject.RECOVERY_PHOTOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.RECOVERY_VIDEOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.REPEAT_PHOTOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.SCREENSHOT_CLEANER
import com.base.filerecoveryrecyclebin.bean.ConstObject.WHATSAPP_CLEANER
import com.base.filerecoveryrecyclebin.databinding.ItemLayoutReslut2Binding
import com.base.filerecoveryrecyclebin.utils.AppPreferences
import com.base.filerecoveryrecyclebin.view.XmlEx.inflate
import java.util.Collections

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

    val list = arrayListOf(
        Fun(JUNK_CLEANER, R.mipmap.r_laji, "Clean up the junk files for your phone", "Clean Up Now"),
        Fun(APP_MANAGER, R.mipmap.r_appjincheng, "Detect junk files ,cache,temp files.", "Try It Now"),
        Fun(BATTERY_INFO, R.mipmap.r_dianchi, "View battery usage and details", "Try It Now"),
        Fun(LARGE_FILE, R.mipmap.r_dawenjian, "Clean large fles to free up storage space", "Try It Now"),
        Fun(WHATSAPP_CLEANER, R.mipmap.r_what, "Free up space by cleaning up WhatsApp junk files", "Try It Now"),
        Fun(APP_PROCESS, R.mipmap.r_guanli, "Manage backend processes", "Try It Now"),
        Fun(REPEAT_PHOTOS, R.mipmap.r_xiangsi, "Check similar photos to release more space", "Try It Now"),
        Fun(SCREENSHOT_CLEANER, R.mipmap.r_jietu, "Check the screenshots to free up more space", "Try It Now"),
        Fun(RECOVERY_PHOTOS, R.mipmap.r_pic, "Recovery lost photos", "Try It Now"),
        Fun(RECOVERY_VIDEOS, R.mipmap.r_shiping, "Recovery lost videos", "Try It Now"),
        Fun(RECOVERY_AUDIOS, R.mipmap.r_yinping, "Recovery lost audios", "Try It Now"),
        Fun(RECOVERY_DOCUMENTS, R.mipmap.r_xiazai, "Recovery lost Documents", "Try It Now"),
    )

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


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JJJ {
        return JJJ(R.layout.item_layout_reslut2.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 = ItemLayoutReslut2Binding.bind(holder.itemView)
        if(position==0){
            binding.idFlNative.isVisible = true
            AdmobMaxHelper.admobMaxShowNativeAd(activity,binding.idFlNative)
        }else {
            binding.idFlNative.isVisible = false
        }

        binding.ivIcon.setImageDrawable(ContextCompat.getDrawable(context, data.icon))
        binding.tvTittle.text = data.name
        binding.idDesc.text=data.des
        binding.idBtn.text=data.button
//        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)
        }
    }

}