package com.gh.gamecenter.gamedetail.rating

import android.content.Context
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.text.SpannableStringBuilder
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.exposure.IExposable
import com.gh.common.util.*
import com.gh.common.util.NewFlatLogUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.viewholder.GameViewHolder
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.common.callback.ConfirmListener
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.view.DrawableView
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.core.utils.NumberUtils
import com.gh.gamecenter.core.utils.SpanBuilder
import com.gh.gamecenter.databinding.ItemArticleDetailCommentBinding
import com.gh.gamecenter.databinding.PieceArticleDetailCommentFilterBinding
import com.gh.gamecenter.databinding.RatingReplyHeadItemBinding
import com.gh.gamecenter.entity.RatingComment
import com.gh.gamecenter.entity.RatingReplyEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.gamedetail.rating.edit.RatingEditActivity
import com.gh.gamecenter.login.user.UserManager
import com.lightgame.utils.Utils

class RatingReplyAdapter(
    context: Context,
    val entrance: String,
    val viewModel: RatingReplyViewModel,
    private val mExposureSourceList: ArrayList<ExposureSource>?,
    val replyCallback: (RatingReplyEntity?) -> Unit
) : ListAdapter<RatingReplyItem>(context), IExposable {

    private val ITEM_COMMENT = 211
    private val ITEM_SECTION_TITLE = 212

    private val path = "评论详情"
    private var mExposureEvent: ExposureEvent? = null

    init {
        mExposureSourceList?.add(ExposureSource("评论详情", ""))
    }

    override fun setListData(updateData: MutableList<RatingReplyItem>?) {
        if (mEntityList.size > 0 && mEntityList[mEntityList.size - 1].reply == null
            && updateData != null && updateData.size > 0 && updateData[updateData.size - 1].reply != null
        ) {
            mEntityList = ArrayList(updateData)
            notifyDataSetChanged()
        } else {
            super.setListData(updateData)
        }
    }

    override fun areContentsTheSame(oldItem: RatingReplyItem?, newItem: RatingReplyItem?): Boolean {
        if (oldItem?.comment != null || newItem?.comment != null) return false
        if (oldItem?.replyCount != null || newItem?.replyCount != null) return false
        return oldItem?.reply == newItem?.reply
    }

    override fun areItemsTheSame(oldItem: RatingReplyItem?, newItem: RatingReplyItem?): Boolean {
        return oldItem?.reply?.id == newItem?.reply?.id
    }

    override fun getItemViewType(position: Int): Int {
        if (position == itemCount - 1) {
            return ItemViewType.ITEM_FOOTER
        }

        val entity = mEntityList[position]
        if (entity.game != null) {
            return ItemViewType.ITEM_HEADER
        }
        if (entity.comment != null) {
            return ITEM_COMMENT
        }
        if (entity.replyCount != null) {
            return ITEM_SECTION_TITLE
        }

        return ItemViewType.ITEM_BODY
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View
        return when (viewType) {
            ItemViewType.ITEM_HEADER -> {
                view = mLayoutInflater.inflate(R.layout.rating_reply_head_item, parent, false)
                RatingReplyHeadItemViewHolder(RatingReplyHeadItemBinding.bind(view))
            }

            ITEM_COMMENT -> {
                view = mLayoutInflater.inflate(R.layout.item_article_detail_comment, parent, false)
                RatingDetailCommentItemViewHolder(ItemArticleDetailCommentBinding.bind(view), entrance)
            }

            ITEM_SECTION_TITLE -> {
                view = mLayoutInflater.inflate(R.layout.piece_article_detail_comment_filter, parent, false)
                ListSectionItemViewHolder(PieceArticleDetailCommentFilterBinding.bind(view))
            }

            ItemViewType.ITEM_BODY -> {
                view = mLayoutInflater.inflate(R.layout.item_article_detail_comment, parent, false)
                RatingReplyItemViewHolder(ItemArticleDetailCommentBinding.bind(view))
            }

            ItemViewType.ITEM_FOOTER -> {
                view = mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false)
                FooterViewHolder(view)
            }

            else -> {
                throw IllegalStateException("item view type not found")
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList.size > 0) mEntityList.size + 1 else 0
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is RatingReplyHeadItemViewHolder -> {
                val game = mEntityList[position].game!!
                // 构造 exposureSource
                val exposureSourceList = mExposureSourceList
                    ?: listOf(ExposureSource("其他", ""), ExposureSource("评论详情", ""))
                mExposureEvent = ExposureEvent.createEvent(game, exposureSourceList)
                // 记录评论 id
                mExposureEvent?.payload?.controlLinkDesc = viewModel.commentId
                holder.binding.gameIcon.displayGameIcon(game)
                holder.binding.gameName.text = game.name
                holder.binding.gameInfo.text = if (!game.getApk()
                        .isNullOrEmpty()
                ) "V${game.getApk()[0].version} | ${game.getApk()[0].size}" else ""
                holder.binding.root.setRootBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface)
                holder.binding.gameName.setTextColor(com.gh.gamecenter.common.R.color.text_black.toColor(mContext))
                holder.itemView.setOnClickListener {
                    NewLogUtils.logCommentTabOrCommentDetailClick(
                        "click_game_comment_detail_game",
                        game.id,
                        game.gameType
                    )
                    logGameDetailCommentDetailCommentClick("游戏详情")
                    GameDetailActivity.startGameDetailActivity(
                        mContext,
                        game,
                        BaseActivity.mergeEntranceAndPath(entrance, path),
                        traceEvent = mExposureEvent
                    )
                }
                holder.binding.gameDownload.run {
                    DownloadItemUtils.setOnClickListener(
                        mContext, this,
                        game, 0, this@RatingReplyAdapter,
                        entrance,
                        location = BaseActivity.mergeEntranceAndPath(entrance, path),
                        traceEvent = mExposureEvent,
                        clickCallback = {
                            NewLogUtils.logDownloadBtnClick(
                                game.id,
                                game.gameType,
                                text
                            )
                        },
                        refreshCallback = null,
                        allStateClickCallback = { logGameDetailCommentDetailCommentClick("按钮") }
                    )
                    DownloadItemUtils.updateItem(
                        mContext,
                        game,
                        GameViewHolder(holder.binding.root).apply {
                            gameDownloadBtn = holder.binding.gameDownload
                            multiVersionDownloadTv = holder.binding.multiVersionDownloadTv
                            gameDownloadTips = holder.binding.downloadTipsLottie
                        },
                    )
                }
            }

            is RatingDetailCommentItemViewHolder -> {
                val commentData = mEntityList[position].comment!!
                holder.setContent(commentData, viewModel, position, entrance, path)
                holder.binding.run {
                    contentTv.setExpandMaxLines(Int.MAX_VALUE)
                    contentTv.setOnLongClickListener(View.OnLongClickListener {
                        commentData.content.replace(
                            RatingEditActivity.LABEL_REPLACE_REGEX.toRegex(),
                            ""
                        ).copyTextAndToast()
                        return@OnLongClickListener true
                    })
                    likeCountTv.setDebouncedClickListener {
                        likeCountTv.context.ifLogin("${entrance}-评论详情-点赞") {
                            if (!commentData.me.isVoted) {
                                viewModel.voteComment {
                                    likeCountTv.setDrawableStart(R.drawable.comment_vote_select)
                                    likeCountTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(likeCountTv.context))
                                    likeCountTv.text = (commentData.vote + 1).toString()
                                    commentData.vote = commentData.vote + 1
                                    commentData.me.isVoted = true
                                }
                                NewLogUtils.logCommentTabOrCommentDetailClick(
                                    "click_game_comment_detail_commet_like",
                                    viewModel.game?.id ?: "",
                                    viewModel.game?.gameType ?: ""
                                )
                                logGameDetailCommentDetailCommentClick("点赞评价")
                            } else {
                                viewModel.unvoteComment {
                                    likeCountTv.setDrawableStart(R.drawable.comment_vote_unselect)
                                    likeCountTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(likeCountTv.context))
                                    val count = commentData.vote - 1
                                    likeCountTv.text = if (count == 0) "" else count.toString()
                                    commentData.vote = count
                                    commentData.me.isVoted = false
                                }
                                logGameDetailCommentDetailCommentClick("取消点赞评价")
                            }
                        }
                    }
                }
            }

            is RatingReplyItemViewHolder -> {
                val replyEntity = mEntityList[position].reply!!
                holder.binding.run {
                    if (replyEntity.isHighlight) {
                        replyEntity.isHighlight = false
                        highlightBg.run {
                            visibility = View.VISIBLE
                            postDelayed({
                                tryWithDefaultCatch {
                                    visibility = View.GONE
                                }
                            }, 3000)
                        }
                    }
                    root.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(mContext))
                    if (replyEntity.weight == 1) {
                        commentBadge.visibility = View.VISIBLE
                        commentBadge.text = "客服"
                        commentBadge.background = DrawableView.getCornerGradientDrawable(
                            ContextCompat.getColor(mContext, com.gh.gamecenter.common.R.color.text_4BC7FF),
                            ContextCompat.getColor(mContext, com.gh.gamecenter.common.R.color.primary_theme),
                            5f
                        )
                    } else {
                        commentBadge.visibility = View.GONE
                    }
                    authTv.goneIf(replyEntity.user.auth == null) {
                        authTv.text = replyEntity.user.auth?.text
                    }
                    timeTv.visibility = View.GONE
                    time.visibility = View.VISIBLE
                    time.text = if (replyEntity.source != null && replyEntity.source.region.isNotEmpty()) {
                        "${NewsUtils.getFormattedTime(replyEntity.time)} · ${replyEntity.source.region}"
                    } else {
                        NewsUtils.getFormattedTime(replyEntity.time)
                    }
                    if (replyEntity.me.isVoted) {
                        likeCountTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(likeCountTv.context))
                        likeCountTv.setDrawableStart(R.drawable.comment_vote_select)
                    } else {
                        likeCountTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(likeCountTv.context))
                        likeCountTv.setDrawableStart(R.drawable.comment_vote_unselect)
                    }
                    likeCountTv.text = if (replyEntity.vote > 0) NumberUtils.transSimpleCount(replyEntity.vote) else ""
                    userIconIv.display(
                        replyEntity.user.border,
                        replyEntity.user.icon,
                        replyEntity.user.auth?.icon
                    )
                    userNameTv.text = replyEntity.user.name
                    if (replyEntity.user.badge != null) {
                        badgeIv.visibility = View.VISIBLE
                        ImageUtils.display(badgeIv, replyEntity.user.badge?.icon)
                        badgeTv.text = replyEntity.user.badge?.name
                    } else {
                        badgeIv.visibility = View.GONE
                        badgeTv.visibility = View.GONE
                    }
                    val parent = replyEntity.parent
                    var contentCharSequence: CharSequence = replyEntity.content
                    if (parent != null) {
                        val prefix = "回复"
                        val colon = " ："
                        val parentUserName = " @${parent.user.name} "

                        val prefixSpan = SpanBuilder(prefix).color(
                            root.context,
                            0,
                            prefix.length,
                            com.gh.gamecenter.common.R.color.text_primary
                        ).build()
                        val parentUserNameSpan = SpanBuilder(parentUserName)
                            .click(mContext, 0, parentUserName.length, com.gh.gamecenter.common.R.color.text_tertiary) {
                                DirectUtils.directToHomeActivity(
                                    root.context,
                                    parent.user.id,
                                    1,
                                    entrance,
                                    "游戏评论详情"
                                )
                                MtaHelper.onEvent("帖子详情", "引用回复区域", "用户名字")
                            }.build()
                        val colonSpan = SpanBuilder(colon).color(
                            root.context,
                            0,
                            colon.length,
                            com.gh.gamecenter.common.R.color.text_primary
                        ).build()

                        contentCharSequence = SpannableStringBuilder()
                            .append(prefixSpan)
                            .append(parentUserNameSpan)
                            .append(colonSpan)
                            .append(replyEntity.content)
                    }

                    contentTv.setTextWithHighlightedTextWrappedInsideWrapper(
                        text = contentCharSequence,
                        highlightedTextClickListener = TextHelper.DirectToWebViewHighlightedTextClick(
                            mContext,
                            entrance
                        )
                    )
                    badgeIv.setOnClickListener {
                        DialogUtils.showViewBadgeDialog(mContext, replyEntity.user.badge, object : ConfirmListener {
                            override fun onConfirm() {
                                MtaHelper.onEvent(
                                    "进入徽章墙_用户记录",
                                    "游戏评论详情",
                                    "${replyEntity.user.name}（${replyEntity.user.id}）"
                                )
                                MtaHelper.onEvent("徽章中心", "进入徽章中心", "游戏评论详情")
                                DirectUtils.directToBadgeWall(
                                    mContext,
                                    replyEntity.user.id,
                                    replyEntity.user.name,
                                    replyEntity.user.icon
                                )
                            }
                        })
                    }
                    badgeTv.setOnClickListener { badgeIv.performClick() }

                    likeCountTv.setDebouncedClickListener {
                        mContext.ifLogin("游戏详情-评分-评论详情-点赞评论") {
                            if (!replyEntity.me.isVoted) {
                                viewModel.voteReply(replyEntity.id, callback = {
                                    likeCountTv.setDrawableStart(R.drawable.comment_vote_select)
                                    likeCountTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(likeCountTv.context))
                                    likeCountTv.text = (replyEntity.vote).toString()
                                })
                                NewLogUtils.logCommentTabOrCommentDetailClick(
                                    "click_game_comment_detail_answer_like",
                                    viewModel.game?.id ?: "",
                                    viewModel.game?.gameType ?: "",
                                )
                                logGameDetailCommentDetailCommentClick("回复点赞")
                            } else {
                                viewModel.voteReply(replyEntity.id, false, callback = {
                                    likeCountTv.setDrawableStart(R.drawable.comment_vote_unselect)
                                    likeCountTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(likeCountTv.context))
                                    likeCountTv.text = if (replyEntity.vote == 0) "" else (replyEntity.vote).toString()
                                })
                                logGameDetailCommentDetailCommentClick("回复取消点赞")
                            }
                        }
                    }
                    commentCountTv.text = "回复"
                    commentCountTv.setOnClickListener {
                        if (replyEntity.user.id == UserManager.getInstance().userId) {
                            Utils.toast(mContext, "不能回复自己")
                        } else {
                            replyCallback.invoke(replyEntity)
                            NewLogUtils.logCommentTabOrCommentDetailClick(
                                "click_game_comment_detail_answer",
                                viewModel.game?.id ?: "",
                                viewModel.game?.gameType ?: "",
                            )
                            logGameDetailCommentDetailCommentClick("回复回复")
                        }
                    }
                    root.setOnClickListener {
                        if (replyEntity.user.id == UserManager.getInstance().userId) {
                            Utils.toast(mContext, "不能回复自己")
                        } else {
                            replyCallback.invoke(replyEntity)
                            NewLogUtils.logCommentTabOrCommentDetailClick(
                                "click_game_comment_detail_answer",
                                viewModel.game?.id ?: "",
                                viewModel.game?.gameType ?: "",
                            )
                            logGameDetailCommentDetailCommentClick("回复正文")
                        }
                    }

                    moreIv.setOnClickListener { view ->
                        showMorePopWindow(view, replyEntity.user.id == UserManager.getInstance().userId) { it ->
                            when (it) {
                                "复制" -> {
                                    replyEntity.content.replace(
                                        RatingEditActivity.LABEL_REPLACE_REGEX.toRegex(),
                                        ""
                                    ).copyTextAndToast()
                                    logGameDetailCommentDetailCommentClick("回复右上角-复制")
                                }

                                "投诉" -> {
                                    mContext.ifLogin("游戏详情-评分-评论详情- 投诉评论") {
                                        DialogUtils.showReportReasonDialog(
                                            mContext,
                                            Constants.REPORT_LIST.toList() as java.util.ArrayList<String>
                                        ) { reason, desc ->
                                            SimpleRequestHelper.reportGameCommentReply(
                                                mEntityList[0].game!!.id,
                                                mEntityList[1].comment!!.id,
                                                replyEntity.id,
                                                if (reason != "其他原因") reason else desc
                                            )
                                        }
                                    }
                                    logGameDetailCommentDetailCommentClick("回复右上角-投诉")
                                }

                                "删除" -> {
                                    DialogHelper.showDeleteGameCommentDialog(
                                        mContext,
                                        R.string.delete_game_comment_reply.toResString()
                                    ) {
                                        SimpleRequestHelper.deleteGameCommentReply(
                                            mEntityList[0].game!!.id,
                                            mEntityList[1].comment!!.id,
                                            replyEntity.id,
                                        ) {
                                            val index = mEntityList.indexOfFirst { item ->
                                                item.reply?.id == replyEntity.id
                                            }
                                            mEntityList.removeAt(index)
                                            notifyItemRemoved(index)

                                            //刷新 ITEM_FILTER
                                            viewModel.comment?.run { reply -= 1 }
                                            mEntityList[2].replyCount = mEntityList[2].replyCount?.minus(1)
                                            if (mEntityList[2].replyCount == 0) {
                                                mEntityList.removeAt(2)
                                                notifyItemRemoved(2)
                                            } else {
                                                notifyItemChanged(2)
                                            }
                                        }
                                    }
                                    logGameDetailCommentDetailCommentClick("回复右上角-删除")
                                }
                            }
                        }
                        logGameDetailCommentDetailCommentClick("回复展开右上角")
                    }

                    userIconIv.setOnClickListener {
                        DirectUtils.directToHomeActivity(
                            mContext,
                            replyEntity.user.id,
                            entrance,
                            path
                        )
                        NewLogUtils.logCommentTabOrCommentDetailClick(
                            "click_game_comment_detail_answer_profile_photo",
                            viewModel.game?.id ?: "",
                            viewModel.game?.gameType ?: "",
                            replyEntity.user.id ?: ""
                        )
                        logGameDetailCommentDetailCommentClick("回复者用户信息")
                    }
                    userNameTv.setOnClickListener {
                        DirectUtils.directToHomeActivity(
                            mContext,
                            replyEntity.user.id,
                            entrance,
                            path
                        )
                        NewLogUtils.logCommentTabOrCommentDetailClick(
                            "click_game_comment_detail_answer_nickname",
                            viewModel.game?.id ?: "",
                            viewModel.game?.gameType ?: "",
                            replyEntity.user.id ?: ""
                        )
                        logGameDetailCommentDetailCommentClick("回复者用户信息")
                    }
                }
            }

            is ListSectionItemViewHolder -> {
                holder.binding.run {
                    root.setRootBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface)
                    divider.visibility = View.GONE
                    commentHintTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(mContext))
                    commentHintCountTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext))
                    orderSfv.run {
                        setContainerBackground(com.gh.gamecenter.common.R.drawable.button_round_f5f5f5.toDrawable(mContext))
                        setIndicatorBackground(R.drawable.bg_game_collection_sfv_indicator.toDrawable(mContext))
                        setTextColor(
                            com.gh.gamecenter.common.R.color.text_secondary.toColor(mContext),
                            com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext)
                        )
                    }

                    commentHintTv.text = "全部回复"
                    commentHintCountTv.text = mEntityList[position].replyCount?.toNewSimpleCount()

                    if (orderSfv.getItemList().isEmpty()) {
                        orderSfv.setItemList(listOf("正序", "倒序"), 0)
                    }

                    orderSfv.setOnCheckedCallback { position ->
                        when (position) {
                            0 -> {
                                viewModel.sortList(RatingReplyViewModel.SORT_POSITIVE)
                                logGameDetailCommentDetailCommentClick("正序")
                            }

                            1 -> {
                                viewModel.sortList(RatingReplyViewModel.SORT_NEGATION)
                                logGameDetailCommentDetailCommentClick("倒序")
                            }
                        }
                    }
                }
            }

            is FooterViewHolder -> {
                holder.itemView.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(mContext))
                when {
                    mIsNetworkError -> {
                        holder.loading.visibility = View.GONE
                        holder.hint.setText(com.gh.gamecenter.common.R.string.loading_failed_retry)
                        holder.itemView.isClickable = true
                        holder.itemView.setOnClickListener { viewModel.load(LoadType.RETRY) }
                    }

                    mIsLoading -> {
                        holder.loading.visibility = View.VISIBLE
                        holder.hint.setText(com.gh.gamecenter.common.R.string.loading)
                        holder.itemView.isClickable = false
                    }

                    mIsOver && mEntityList.size > 2 && mEntityList[2].replyCount != null -> {
                        holder.loading.visibility = View.GONE
                        holder.hint.setText(com.gh.gamecenter.common.R.string.load_over_hint)
                        holder.itemView.isClickable = false
                    }

                    mIsOver -> {
                        holder.loading.visibility = View.GONE
                        holder.hint.text = "这里还没有回复，说点什么吧~"
                        holder.itemView.isClickable = false
                        if (viewModel.showKeyboardIfReplyIsEmpty) {
                            replyCallback.invoke(null)
                        }
                    }

                    else -> {
                        holder.loading.visibility = View.GONE
                        holder.hint.setText(com.gh.gamecenter.common.R.string.loading_more_hint)
                        holder.itemView.isClickable = false
                    }
                }
            }
        }
    }

    fun handleSyncData(requestCode: Int, intent: Intent?) {
        SyncDataBetweenPageHelper.resultHandle(intent, object : OnSyncCallBack<RatingComment> {
            override fun onData(dataPosition: Int): RatingComment? {
                if (dataPosition >= mEntityList.size) return null
                val comment = mEntityList[dataPosition].comment
                if (requestCode == RatingFragment.RATING_PATCH_REQUEST) {
                    comment?.ignore = viewModel.game?.ignoreComment ?: false
                }
                return comment
            }

            override fun onNotify(dataPosition: Int) {
                notifyItemChanged(dataPosition)
            }
        })
    }

    private fun showMorePopWindow(v: View, isMyRating: Boolean, clickListener: (String) -> Unit) {
        val contentList = if (isMyRating) arrayListOf("复制", "删除") else arrayListOf("复制", "投诉")

        val inflater = LayoutInflater.from(v.context)
        val layout = inflater.inflate(com.gh.gamecenter.common.R.layout.layout_popup_container, null)
        val popupWindow = PopupWindow(
            layout,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        popupWindow.apply {
            setBackgroundDrawable(ColorDrawable(0))
            isTouchable = true
            isFocusable = true
            isOutsideTouchable = true
        }

        val container = layout.findViewById<LinearLayout>(R.id.container)
        for (text in contentList) {
            val item = inflater.inflate(R.layout.layout_popup_option_item, container, false)
            container.addView(item)

            val hitText = item.findViewById<TextView>(R.id.hint_text)
            hitText.text = text

            item.setOnClickListener {
                clickListener.invoke(text)
                popupWindow.dismiss()
            }
        }

        popupWindow.showAutoOrientation(v)
    }

    private fun logGameDetailCommentDetailCommentClick(button: String) {
        viewModel.run {
            val location = when {
                lastPageData.contains(EntranceConsts.KEY_LOCATION) -> lastPageData[EntranceConsts.KEY_LOCATION]
                entrance.contains("安利墙") -> "安利墙"
                else -> "其他"
            }
            val tag = if (location == "全部评价") lastPageData[EntranceConsts.KEY_SORT] else ""
            val filter = if (location == "全部评价") lastPageData[EntranceConsts.KEY_FILTER] else ""
            NewFlatLogUtils.logGameDetailCommentDetailCommentClick(
                location ?: "",
                tag ?: "",
                filter ?: "",
                button,
                game?.name ?: "",
                game?.id ?: "",
                commentId ?: ""
            )
        }
    }

    override fun getEventByPosition(pos: Int): ExposureEvent? {
        return mExposureEvent
    }

    override fun getEventListByPosition(pos: Int): List<ExposureEvent>? {
        return null
    }

    inner class RatingReplyHeadItemViewHolder(val binding: RatingReplyHeadItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)

    inner class RatingReplyItemViewHolder(val binding: ItemArticleDetailCommentBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)

    inner class ListSectionItemViewHolder(val binding: PieceArticleDetailCommentFilterBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)
}