package com.base.easyfilemanager.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.easyfilemanager.R
import com.base.easyfilemanager.databinding.ItemAdBinding
import com.base.easyfilemanager.databinding.ItemResultFunBinding
import com.base.easyfilemanager.helps.ads.AdmobUtils
import com.base.easyfilemanager.utils.SPUtils
import com.base.easyfilemanager.view.AFunOb.APP_MANAGER
import com.base.easyfilemanager.view.AFunOb.JUNK_CLEANER
import com.base.easyfilemanager.view.AFunOb.LARGE_FILE_CLEANER
import com.base.easyfilemanager.view.AFunOb.NETWORK_TRAFFIC
import com.base.easyfilemanager.view.AFunOb.NOTIFICATION_CLEANER
import com.base.easyfilemanager.view.AFunOb.PHOTO_COMPRESS
import com.base.easyfilemanager.view.AFunOb.RECENT_APP_USAGE
import com.base.easyfilemanager.view.AFunOb.SIMILAR_PHOTOS
import com.base.easyfilemanager.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.h_clean, "Clean junk regularly to free up space", "Clean Up"),
        Fun(PHOTO_COMPRESS, R.mipmap.h_photo, "Compress photos to save space", "Compress"),
        Fun(LARGE_FILE_CLEANER, R.mipmap.h_large, "Clean large files to free up storage space", "Clean Up"),
        Fun().apply { isAd = true },
        Fun(
            APP_MANAGER,
            R.mipmap.h_manager,
            "Check apps size and uninstall some apps to release storage space",
            "Check Now"
        ),
        Fun(
            SIMILAR_PHOTOS,
            R.mipmap.h_similar,
            "Check similar photos to release more space",
            "Clean Up"
        ),
        Fun(
            NOTIFICATION_CLEANER,
            R.mipmap.h_notification,
            "Too many annoying notifications? Block and clean",
            "Check Now"
        ),
        Fun(RECENT_APP_USAGE, R.mipmap.h_recentapp, "Check and manage recently active apps", "View Now"),
        Fun(
            NETWORK_TRAFFIC,
            R.mipmap.h_network,
            "View network traffic usage and stop traffic-consuming 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)
        }
    }

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

    override fun getItemViewType(position: Int): Int {
        val data = list[position]
        return if (data.isAd) 0 else 1

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JJJ {
        val layout = if (viewType == 0) R.layout.item_ad else R.layout.item_result_fun
        return JJJ(layout.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
        if (data.isAd) {
            val binding = ItemAdBinding.bind(holder.itemView)
            AdmobUtils.showNativeAd(null, binding.flAd)
        } else {
            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 = "",
    ) {
        var isAd = false
    }


}