package com.test.basd.onecleanmaster.adapter

import android.app.Activity
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.test.basd.onecleanmaster.R
import com.test.basd.onecleanmaster.databinding.ItemAdBinding
import com.test.basd.onecleanmaster.databinding.ItemToolGridBinding
import com.test.basd.onecleanmaster.databinding.ItemToolsGridBinding
import com.test.basd.onecleanmaster.helps.ads.AdmobUtils
import com.test.basd.onecleanmaster.view.AFunOb.APP_MANAGER
import com.test.basd.onecleanmaster.view.AFunOb.BATTERY_INFO
import com.test.basd.onecleanmaster.view.AFunOb.JUNK_CLEANER
import com.test.basd.onecleanmaster.view.AFunOb.LARGE_FILE_CLEANER
import com.test.basd.onecleanmaster.view.AFunOb.NETWORK_TRAFFIC
import com.test.basd.onecleanmaster.view.AFunOb.NOTIFICATION_CLEANER
import com.test.basd.onecleanmaster.view.AFunOb.PHOTO_COMPRESS
import com.test.basd.onecleanmaster.view.AFunOb.RECENT_APP_USAGE
import com.test.basd.onecleanmaster.view.AFunOb.SIMILAR_PHOTOS
import com.test.basd.onecleanmaster.view.AFunOb.SPEAK_CLEANER
import com.test.basd.onecleanmaster.view.XmlEx.inflate

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

    private val list = listOf(
        ToolsUI(
            tittle = "Popular", tools = listOf(
                ToolUI(JUNK_CLEANER, context.getString(R.string.clean_junk), R.drawable.tool_cleanjunk),
//                ToolUI(VIRUS_SCAN, context.getString(R.string.virus_scan), R.drawable.virusscan_tools),
                ToolUI(LARGE_FILE_CLEANER, context.getString(R.string.large_file_cleaner), R.drawable.tool_large),
                ToolUI(PHOTO_COMPRESS, context.getString(R.string.photo_compress), R.drawable.tool_photo),
                ToolUI(SPEAK_CLEANER, context.getString(R.string.speaker_cleaner), R.drawable.tool_speaker),
                ToolUI(BATTERY_INFO, context.getString(R.string.battery_info), R.drawable.battery_tools),
                ToolUI(APP_MANAGER, context.getString(R.string.app_manager), R.drawable.tool_appmanager),
                ToolUI(SIMILAR_PHOTOS, "Similar Photos", R.drawable.tool_similar),
            )
        ),
//        ToolsUI(isAd = true),
        ToolsUI(
            tittle = "More",
            tools = listOf(
                ToolUI(NOTIFICATION_CLEANER, context.getString(R.string.notification_cleaner), R.drawable.tool_notification),
                ToolUI(NETWORK_TRAFFIC, context.getString(R.string.network_traffic), R.drawable.tool_network),
                ToolUI(RECENT_APP_USAGE, context.getString(R.string.recent_app_usage), R.drawable.tool_recentapp),
            )
        ),

        )

    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)
            binding.tvTittle.text = data.tittle
            data.tools.forEachIndexed { index, toolUI ->
                val gItem = R.layout.item_tool_grid.inflate(binding.grid)
                val toolBinding = ItemToolGridBinding.bind(gItem)
                val color = when (toolUI.kName) {
//                    JUNK_CLEANER -> R.color.color_f3f6ff
//                    VIRUS_SCAN -> R.color.color_fff1f1
//                    RECENT_APP_USAGE -> R.color.color_fff9ed
//                    LARGE_FILE_CLEANER -> R.color.color_fff9ed
//                    NOTIFICATION_CLEANER -> R.color.color_fff1f1
//                    NETWORK_TRAFFIC -> R.color.color_fff1f1
//                    APP_MANAGER -> R.color.color_ecfcff
//                    BATTERY_INFO -> R.color.color_ecfcf7
//                    SIMILAR_PHOTOS -> R.color.color_f5f4ff
//                    SPEAK_CLEANER -> R.color.color_f3f6ff
//                    PHOTO_COMPRESS -> R.color.color_f5f4ff
                    else -> R.color.color_f7faff
                }
                toolBinding.iv.setImageResource(toolUI.drawable)
                toolBinding.tvName.text = toolUI.fName
                toolBinding.root.setOnClickListener {
                    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
    )
}