package com.base.easyfilemanager.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.base.easyfilemanager.R
import com.base.easyfilemanager.databinding.ItemAdBinding
import com.base.easyfilemanager.databinding.ItemToolGrid1Binding
import com.base.easyfilemanager.databinding.ItemToolsGrid1Binding
import com.base.easyfilemanager.helps.ads.AdmobUtils
import com.base.easyfilemanager.view.AFunOb.APP_MANAGER
import com.base.easyfilemanager.view.AFunOb.DEVICE_SCAN
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.SCREENSHOT_CLEANER
import com.base.easyfilemanager.view.AFunOb.SIMILAR_PHOTOS
import com.base.easyfilemanager.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.t_cleanjunk),
                ToolUI(LARGE_FILE_CLEANER, context.getString(R.string.large_file_cleaner), R.mipmap.t_large),
                ToolUI(PHOTO_COMPRESS, context.getString(R.string.photo_compress), R.mipmap.t_photo),
                ToolUI(SIMILAR_PHOTOS, context.getString(R.string.similar_photos), R.mipmap.similar),
                ToolUI(APP_MANAGER, context.getString(R.string.app_manager), R.mipmap.t_appmanager),
            )
        ),
        ToolsUI(isAd = true),
        ToolsUI(
            tittle = "More",
            tools = listOf(
                ToolUI(NOTIFICATION_CLEANER, context.getString(R.string.notification_cleaner), R.mipmap.t_notification),
                ToolUI(NETWORK_TRAFFIC, context.getString(R.string.network_traffic), R.mipmap.t_network),
                ToolUI(RECENT_APP_USAGE, context.getString(R.string.recent_app_usage), R.mipmap.t_recentapp),
                ToolUI(DEVICE_SCAN, context.getString(R.string.device_scan), R.mipmap.devicescan),
                ToolUI(SCREENSHOT_CLEANER, context.getString(R.string.screenshot_cleaner), R.mipmap.t_screenshot)
            )
        ),

        )

    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_1
        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 = ItemToolsGrid1Binding.bind(holder.itemView)
            binding.tvTittle.text = data.tittle
            data.tools.forEachIndexed { index, toolUI ->
                val gItem = R.layout.item_tool_grid_1.inflate(binding.ll)
                val toolBinding = ItemToolGrid1Binding.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.ll.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
    )
}