package com.gh.gamecenter.forum.detail

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.*
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.hexStringToIntColor
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.view.DrawableView
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.ForumTopLinkItemBinding
import com.gh.gamecenter.entity.ForumDetailEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class ForumTopLinkAdapter(
    context: Context,
    val links: ArrayList<ForumDetailEntity.TopLinkEntity>,
    val bbsId: String,
    val bbsName: String,
    val bbsType: String
) : BaseRecyclerAdapter<ForumTopLinkAdapter.ForumTopLinkViewHolder>(context) {

    override fun getItemCount() = links.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ForumTopLinkViewHolder {
        return ForumTopLinkViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(holder: ForumTopLinkViewHolder, position: Int) {
        val linkEntity = links[position]
        holder.binding.run {
            linkNameTv.text = linkEntity.title
            linkEntity.category?.run {
                tagView.visibility = View.VISIBLE
                tagView.text = title
                tagView.setTextColor(
                    if (style.fontColor.isNotBlank()) style.fontColor.hexStringToIntColor()
                    else Color.WHITE
                )
                if (style.color.isNotBlank()) {
                    tagView.background = DrawableView.getCornerGradientDrawable(
                        style.color.hexStringToIntColor(),
                        style.color.hexStringToIntColor(),
                        3F
                    )
                }
            } ?: run {
                tagView.visibility = View.GONE
            }

            root.setOnClickListener {
                MtaHelper.onEvent("论坛详情", "论坛置顶", linkEntity.text)
                DirectUtils.directToLinkPage(mContext, linkEntity, "论坛详情页置顶栏", "", "论坛详情-置顶公告栏")
                val bbsType = if (bbsType == "game_bbs") "游戏论坛" else "综合论坛"
                NewLogUtils.logForumDetailEnterOrClick("click_top_content", bbsId, bbsType)
                NewLogUtils.logForumLinkTopClick(bbsId, bbsType)
                NewFlatLogUtils.logClickForumTopBar(
                    linkEntity.type ?: "",
                    linkEntity.link ?: "",
                    position,
                    bbsId
                )
                SensorsBridge.trackForumDetailsAnnouncementBoardClick(
                    toppingType = linkEntity.type ?: "",
                    title = linkEntity.title ?: "",
                    forumName = bbsName,
                    forumType = bbsType
                )
            }
            if (position == 0) {
                val params = root.layoutParams as RecyclerView.LayoutParams
                params.topMargin = ForumDetailFragment.TOP_CONTENT_TOP_MARGIN
                root.layoutParams = params
            }
        }
    }

    class ForumTopLinkViewHolder(val binding: ForumTopLinkItemBinding) : RecyclerView.ViewHolder(binding.root)
}