package com.base.superfilemanager.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.blankj.utilcode.util.SPUtils
import com.base.superfilemanager.R
import com.base.superfilemanager.databinding.ItemResultFunBinding
import com.base.superfilemanager.view.AFunOb.APP_MANAGER
import com.base.superfilemanager.view.AFunOb.BATTERY_INFO
import com.base.superfilemanager.view.AFunOb.JUNK_CLEANER
import com.base.superfilemanager.view.AFunOb.LARGE_FILE_CLEANER
import com.base.superfilemanager.view.AFunOb.NETWORK_TRAFFIC
import com.base.superfilemanager.view.AFunOb.NOTIFICATION_CLEANER
import com.base.superfilemanager.view.AFunOb.PHOTO_COMPRESS
import com.base.superfilemanager.view.AFunOb.RECENT_APP_USAGE
import com.base.superfilemanager.view.AFunOb.SIMILAR_PHOTOS
import com.base.superfilemanager.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.drawable.jg_cleanjunk, "Clean junk regularly to free up space", "Clean Up"),
        Fun(RECENT_APP_USAGE, R.drawable.jg_recen, "Check and manage recently active apps", "View Now"),
        Fun(LARGE_FILE_CLEANER, R.drawable.jg_large, "Clean large files to free up storage space", "Clean Up"),
        Fun(PHOTO_COMPRESS, R.drawable.jg_photo, "Compress photos to save space", "Compress"),
        Fun(SIMILAR_PHOTOS, R.drawable.jg_similar, "Check similar photos to release more space", "Clean Up"),
        Fun(NOTIFICATION_CLEANER, R.drawable.jg_notification, "Too many annoying notifications? Block and clean", "Check Now"),
        Fun(NETWORK_TRAFFIC, R.drawable.jg_network, "View network traffic usage and stop traffic-consuming apps", "View Now"),
        Fun(APP_MANAGER, R.drawable.jg_appmanager, "Check apps size and uninstall some apps to release storage space", "Check Now"),
        Fun(BATTERY_INFO, R.drawable.jg_battery, "View battery usage and details", "Check Now"),
    )

    //修改顺序
    fun updateListPostion() {
        //本次进入结果页，判断使用垃圾的功能是否超过5分钟
        val lastUsejunkcleaner = SPUtils.getInstance().getLong("last_use_junk_cleaner", 0)
        // Log.e("MXL", "updateListPostion: " + (System.currentTimeMillis() - lastUsejunkcleaner))
        if ((System.currentTimeMillis() - lastUsejunkcleaner) >= 60 * 5 * 1000) {
            // Log.e("MXL", "updateListPostion: "+"超过5分钟不处理" )
        } else {
            //   Log.e("MXL", "updateListPostion: "+"5分钟之内" )
            Collections.rotate(list, list.size - 1)
        }
    }

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


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JJJ {
        return JJJ(R.layout.item_result_fun.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 = ItemResultFunBinding.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.tvButton.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 = "",
    )


}