package com.test.basd.smartjunkcleaner.adapter

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.test.basd.smartjunkcleaner.R
import com.test.basd.smartjunkcleaner.databinding.ItemAdBinding
import com.test.basd.smartjunkcleaner.databinding.ItemToolGridBinding
import com.test.basd.smartjunkcleaner.databinding.ItemToolsGridBinding
import com.test.basd.smartjunkcleaner.helps.ads.AdmobUtils
import com.test.basd.smartjunkcleaner.view.AFunOb.APP_MANAGER
import com.test.basd.smartjunkcleaner.view.AFunOb.JUNK_CLEANER
import com.test.basd.smartjunkcleaner.view.AFunOb.LARGE_FILE_CLEANER
import com.test.basd.smartjunkcleaner.view.AFunOb.NETWORK_TRAFFIC
import com.test.basd.smartjunkcleaner.view.AFunOb.NOTIFICATION_CLEANER
import com.test.basd.smartjunkcleaner.view.AFunOb.PHOTO_COMPRESS
import com.test.basd.smartjunkcleaner.view.AFunOb.RECENT_APP_USAGE
import com.test.basd.smartjunkcleaner.view.AFunOb.SIMILAR_PHOTOS
import com.test.basd.smartjunkcleaner.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.mipmap.cleanjunk_tools),
                ToolUI(PHOTO_COMPRESS, context.getString(R.string.photo_compress), R.mipmap.photo_tools),
                ToolUI(SIMILAR_PHOTOS, "Similar Photos", R.mipmap.similar_tools),
                ToolUI(LARGE_FILE_CLEANER, context.getString(R.string.large_file_cleaner), R.mipmap.large_tools),
                ToolUI(APP_MANAGER, context.getString(R.string.app_manager), R.mipmap.appmanager_tools)
            )
        ),
//        ToolsUI(isAd = true),
        ToolsUI(
            tittle = "More",
            tools = listOf(
                ToolUI(NOTIFICATION_CLEANER, context.getString(R.string.notification_cleaner), R.mipmap.notification_tools),
                ToolUI(NETWORK_TRAFFIC, context.getString(R.string.network_traffic), R.mipmap.network_tools),
                ToolUI(RECENT_APP_USAGE, context.getString(R.string.recent_app_usage), R.mipmap.recentapp_tools),
            )
        ),

        )

    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
//                    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.white
//                }
                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
    )
}