package com.gh.gamecenter.minigame

import android.content.Context
import android.graphics.Typeface
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.SearchDefaultRankItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.SettingsEntity
import com.gh.gamecenter.feature.minigame.MiniGameItemHelper
import com.lightgame.adapter.BaseRecyclerAdapter
import com.lightgame.utils.Util_System_Keyboard

/**
 * 微信小游戏-搜索榜单列表
 */
class MiniGameSearchDefaultRankAdapter(
    context: Context,
    private val mRankList: SettingsEntity.Search.RankList,
) : BaseRecyclerAdapter<MiniGameSearchDefaultRankAdapter.MiniGameSearchDefaultRankItemViewHolder>(context) {

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

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

    override fun onBindViewHolder(holder: MiniGameSearchDefaultRankItemViewHolder, position: Int) {
        val rank = mRankList.content[position]


        holder.binding.run {
            val gameEntity = rank.linkGame?.toGameEntity()
            icon.goneIf(!mRankList.isShowIcon) {
                icon.display(if (rank.link.type == "game") gameEntity?.icon else rank.icon)
            }
            name.text = if (rank.link.type == "game") gameEntity?.name else rank.name.ifBlank { rank.link.text }
            index.run {
                typeface = Typeface.createFromAsset(mContext.assets, Constants.DIN_FONT_PATH)
                text = (position + 1).toString()
                setTextColor(
                    when (position + 1) {
                        1 -> com.gh.gamecenter.common.R.color.text_ff5151
                        2 -> com.gh.gamecenter.common.R.color.text_f67722
                        3 -> com.gh.gamecenter.common.R.color.text_ffbf00
                        else -> com.gh.gamecenter.common.R.color.text_tertiary
                    }.toColor()
                )
            }

            labelIv.visibility = View.GONE

            // 转成Game实体比较好处理数据
            val trackGame = GameEntity(
                _id = rank.id,
                mName = rank.name,
                miniGameAppId = rank.link.link ?: "",
                miniGameCategory = Constants.WECHAT_MINI_GAME,
                usage = rank.usage ?: 0
            )
            holder.binding.playCount.visibility = View.VISIBLE
            MiniGameItemHelper.setMiniGameUsage(holder.binding.playCount, trackGame)
            holder.binding.playContainer.visibility = View.VISIBLE

            val onWechatMiniGameClick = OnClickListener {
                MiniGameItemHelper.trackMiniGameClick(gameEntity = trackGame, location = "搜索榜单")
                // 通过link启动微信小游戏
                DirectUtils.directToLinkPage(
                    mContext,
                    rank.link,
                    "小游戏搜索-搜索榜单",
                    "${mRankList.title}-${rank.name}",
                    rank.exposureEvent
                )
                Util_System_Keyboard.hideSoftKeyboardByIBinder(mContext, it.windowToken)
            }
            holder.binding.playContainer.setOnClickListener(onWechatMiniGameClick)
            holder.binding.root.setOnClickListener(onWechatMiniGameClick)
        }
    }

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