package com.gh.gamecenter.search

import android.content.Context
import android.graphics.Typeface
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.SearchType
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.eventbus.EBSearch
import com.gh.gamecenter.feature.entity.SettingsEntity
import com.lightgame.adapter.BaseRecyclerAdapter
import com.lightgame.utils.Util_System_Keyboard
import org.greenrobot.eventbus.EventBus
import org.json.JSONException
import org.json.JSONObject

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

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

    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 {
            val gameEntity = rank.linkGame?.toGameEntity()
            icon.goneIf(!mRankList.isShowIcon) {
                icon.display(if (rank.link.type == "game") gameEntity?.icon else rank.icon)
            }
            val labelName = if (rank.link.type == "game") gameEntity?.name else rank.name.ifBlank { rank.link.text }
            name.text = labelName
            name.requestLayout()
            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()
                )
            }
            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
            }

            labelIv.goneIf(labelIcon == -1)
            if (labelIcon != -1) {
                labelIv.setImageDrawable(labelIcon.toDrawable())
            }
            root.setOnClickListener {
                val linkEntity = rank.link

                if (linkEntity.type == "game_search") {
                    mViewModel.add(linkEntity.text ?: "")
                    EventBus.getDefault().post(EBSearch(SearchType.RANK.value, linkEntity.text))
                } else {
                    DirectUtils.directToLinkPage(
                        mContext,
                        linkEntity,
                        "游戏搜索-搜索榜单",
                        "${mRankList.title}-${rank.name}",
                        rank.exposureEvent
                    )
                }

                Util_System_Keyboard.hideSoftKeyboardByIBinder(mContext, it.windowToken)

                // 是否来源于游戏搜索
                if (mIsGameSearch) {
                    val searchType = if (linkEntity.type == "game_search") {
                        SearchType.RANK.toChinese()
                    } else {
                        ""
                    }
                    NewFlatLogUtils.logSearchClickRankDetail(
                        linkEntity.link ?: "",
                        searchType,
                        rank.name,
                        (position + 1).toString(),
                        linkEntity.link ?: "",
                        linkEntity.type ?: "",
                        linkEntity.text ?: ""
                    )
                } else {
                    NewFlatLogUtils.logCommunitySearchClickRankDetail(
                        rank.name,
                        linkEntity.link ?: "",
                        linkEntity.type ?: "",
                        linkEntity.text ?: ""
                    )
                }

                val trackEvent = JSONObject()
                try {
                    trackEvent.put("text", labelName)
                    trackEvent.put("search_content", linkEntity.link)
                    trackEvent.put("game_name", gameEntity?.name)
                    trackEvent.put("game_id", gameEntity?.id)
                    trackEvent.put("list_name", mRankList.title)
                    trackEvent.put("position", position + 1)
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                SensorsBridge.trackEvent("SearchListlClick", trackEvent)
            }
        }
    }

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