package com.zxhyis.toolss.ui.home

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.zxhyis.toolss.R
import com.zxhyis.toolss.ad.AdmobUtils
import com.zxhyis.toolss.databinding.ItemAdBinding
import com.zxhyis.toolss.databinding.ItemToolGridBinding
import com.zxhyis.toolss.databinding.ItemToolsGridBinding
import com.zxhyis.toolss.logic.XmlEx.inflate
import com.zxhyis.toolss.logic.setOnClick
import com.zxhyis.toolss.ui.AFunOb
import com.zxhyis.toolss.ui.AFunOb.Antivirus
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 com.zxhyis.toolss.ui.AFunOb.SPEAK_CLEANER

class ToolsAdapter(
    val context: Activity,
    val itemClick: (kName: String) -> Unit
) : RecyclerView.Adapter<ToolsAdapter.VVV>() {

    private val list = listOf(
        ToolsUI(
            tools = listOf(
                ToolUI(LARGE_FILE_CLEANER, context.getString(R.string.large_file_clean), R.drawable.large_tools),
                ToolUI(JUNK_CLEANER, context.getString(R.string.clean_junk), R.drawable.junk_tools),
                ToolUI(SIMILAR_PHOTOS, context.getString(R.string.similar_photos), R.drawable.similar_tools),
                ToolUI(SPEAK_CLEANER, context.getString(R.string.speaker_cleaner), R.drawable.speaker_tools),
                ToolUI(Antivirus, context.getString(R.string.virus_scan), R.drawable.virus_tools),
                ToolUI(AFunOb.APP_MANAGER, context.getString(R.string.app_manager), R.drawable.appmanager_tools),
                ToolUI(NOTIFICATION_CLEANER, context.getString(R.string.notification_cleaner), R.drawable.notification_tools),
                ToolUI(RECENT_APP_USAGE, context.getString(R.string.recent_app_usage), R.drawable.recnent_tools),
                ToolUI(NETWORK_TRAFFIC, context.getString(R.string.network_traffic), R.drawable.network_tools),
                ToolUI(PHOTO_COMPRESS, context.getString(R.string.photo_compress), R.drawable.compress_tools),
//                ToolUI(APPP_LOCK, context.getString(R.string.app_lock), R.drawable.applock),

            )
        ),
        ToolsUI(isAd = true)
    )

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

    inner class VVV(view: View) : ViewHolder(view)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VVV {
        val viewId = if (viewType == 0) R.layout.item_ad else R.layout.item_tools_grid
        return VVV(viewId.inflate(parent))
    }

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

    override fun onBindViewHolder(holder: VVV, position: Int) {
        val data = list[position]

        if (data.isAd) {
            val binding = ItemAdBinding.bind(holder.itemView)
            AdmobUtils.showNativeAd(context, binding.flAd)
        } else {
            val binding = ItemToolsGridBinding.bind(holder.itemView)
            data.tools.forEachIndexed { index, toolUI ->
                val gItem = R.layout.item_tool_grid.inflate(binding.grid)
                val toolBinding = ItemToolGridBinding.bind(gItem)
//                val rand = index % 2 == 0
//                if (rand) {
//                    gItem.setBackgroundColor(context.getColor(R.color.color_4772ff))
//                } else {
//                    gItem.setBackgroundColor(context.getColor(R.color.color_999999))
//                }
                toolBinding.iv.setImageResource(toolUI.drawable)
                toolBinding.tvName.text = toolUI.fName
                toolBinding.root.setOnClick(javaClass.simpleName) {
                    itemClick.invoke(toolUI.kName)
                }
                binding.grid.addView(gItem)
            }
        }

    }

    data class ToolsUI(

        val tittle: String? = null,
        val tools: List<ToolUI> = listOf(),
        val isAd: Boolean = false,//是否是广告
    )

    data class ToolUI(
        val kName: String = "",
        val fName: String = "",
        val drawable: Int = 0
    )
}