package com.zxhyis.toolss.ui.page.result

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.zxhyis.toolss.R
import com.zxhyis.toolss.databinding.ItemResultFunBinding
import com.zxhyis.toolss.logic.XmlEx.inflate
import com.zxhyis.toolss.logic.setOnClick
import com.zxhyis.toolss.ui.AFunOb.APP_MANAGER
import com.zxhyis.toolss.ui.AFunOb.Antivirus
import com.zxhyis.toolss.ui.AFunOb.BATTERY_INFO
import com.zxhyis.toolss.ui.AFunOb.JUNK_CLEANER
import com.zxhyis.toolss.ui.AFunOb.LARGE_FILE_CLEANER
import com.zxhyis.toolss.ui.AFunOb.NETWORK_TRAFFIC
import com.zxhyis.toolss.ui.AFunOb.NOTIFICATION_CLEANER
import com.zxhyis.toolss.ui.AFunOb.PHOTO_COMPRESS
import com.zxhyis.toolss.ui.AFunOb.RECENT_APP_USAGE
import com.zxhyis.toolss.ui.AFunOb.SIMILAR_PHOTOS
import java.util.Collections

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

    val list = arrayListOf(
        Fun(JUNK_CLEANER, R.drawable.cleanjunk_r, "Clean junk regularly to free up space", "Clean Up"),
        Fun(Antivirus, R.drawable.antivirus, "Scan your phone for viruses now to protect your security and pr", "Scan Now"),
        Fun(LARGE_FILE_CLEANER, R.drawable.large_r, "Clean large files to free up storage space", "Clean Up"),
        Fun(PHOTO_COMPRESS, R.drawable.jphoto, "Compress photos to save space", "Compress"),
        Fun(SIMILAR_PHOTOS, R.drawable.similar29871, "Check similar photos to release more space", "Clean Up"),
        Fun(APP_MANAGER, R.drawable.appmanager_r, "Check apps size and uninstall some apps to release storage space", "Check Now"),
        Fun(NETWORK_TRAFFIC, R.drawable.notification_r, "View network traffic usage and stop traffic-consuming apps", "View Now"),
        Fun(BATTERY_INFO, R.drawable.battery, "View battery usage and details", "Check Now"),
        Fun(NOTIFICATION_CLEANER, R.drawable.notification_r, "Too many annoying notifications? Block and clean", "Check Now"),
        Fun(RECENT_APP_USAGE, R.drawable.recen, "Check and manage recently active apps", "View 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)
        }
        //本次进入结果页，判断使用病毒扫描功能是否超过5分钟
        val lastUseAntivirus = SPUtils.getInstance().getLong("last_use_antivirus", 0)
        if ((System.currentTimeMillis() - lastUseAntivirus) >= 60 * 5 * 1000) {
          //  Log.e("MXL", "updateListPostion: "+"超过5分钟不处理" )
        }else{
          //  Log.e("MXL", "updateListPostion: "+"5分钟之内" )
//            moveItem(Antivirus)
            Collections.rotate(list.subList(1, 10), -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.setOnClick(javaClass.simpleName) {
            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 = "",
    )


}