package com.gh.gamecenter.search

import android.content.Context
import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.display
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.databinding.SearchDefaultRankItemBinding
import com.gh.gamecenter.entity.SettingsEntity
import com.lightgame.adapter.BaseRecyclerAdapter
import com.lightgame.utils.Util_System_Keyboard

class SearchDefaultRankAdapter(
    context: Context,
    private val mRankList: SettingsEntity.Search.RankList,
) : BaseRecyclerAdapter<SearchDefaultRankAdapter.SearchDefaultRankItemViewHolder>(context) {

    override fun getItemCount() = minOf(mRankList.content.size, 10)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        SearchDefaultRankItemViewHolder(parent.toBinding())

    override fun onBindViewHolder(holder: SearchDefaultRankItemViewHolder, position: Int) {
        val rank = mRankList.content[position]
        holder.binding.run {
            if (mRankList.isShowIcon) {
                icon.visibility = View.VISIBLE
                icon.display(rank.icon)
            } else {
                icon.visibility = View.GONE
            }
            name.text = rank.name.ifBlank { rank.link.text }
            index.run {
                typeface = Typeface.createFromAsset(mContext.assets, "fonts/d_din_bold.ttf")
                text = (position + 1).toString()
                setTextColor(
                    when (position + 1) {
                        1 -> R.color.text_ff5151
                        2 -> R.color.text_f67722
                        3 -> R.color.text_ffbf00
                        else -> R.color.text_subtitleDesc
                    }.toColor()
                )
            }
            val labelIcon = when (rank.recommendType) {
                "hot" -> R.drawable.ic_search_hot
                "new" -> R.drawable.ic_search_new
                "surge", "rise" -> R.drawable.ic_search_rise
                "update" -> R.drawable.ic_search_update
                else -> -1
            }
            if (labelIcon != -1) {
                labelIv.setImageDrawable(labelIcon.toDrawable())
            }
            root.setOnClickListener {
                DirectUtils.directToLinkPage(
                    mContext,
                    rank.link,
                    "游戏搜索-搜索榜单",
                    "${mRankList.title}-${rank.name}",
                    rank.exposureEvent
                )
                Util_System_Keyboard.hideSoftKeyboardByIBinder(mContext, it.windowToken)
                NewFlatLogUtils.logSearchClickRank(rank.name, (position + 1).toString())
            }
        }
    }

    inner class SearchDefaultRankItemViewHolder(val binding: SearchDefaultRankItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)
}