package com.gh.gamecenter.qa.comment.conversation

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.CommentUtils
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.common.utils.TextHelper
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.setTextWithHighlightedTextWrappedInsideWrapper
import com.gh.gamecenter.databinding.ItemArticleDetailCommentBinding
import com.gh.gamecenter.feature.entity.CommentEntity
import com.gh.gamecenter.qa.comment.base.BaseCommentAdapter

class CommentConversationAdapter(
    context: Context,
    var mViewModelCommunity: CommentConversationViewModel,
    type: AdapterType,
    private var mEntrance: String,
    commentClosure: ((CommentEntity) -> Unit)? = null
) : BaseCommentAdapter(context, mViewModelCommunity, type, mEntrance, commentClosure) {

    var topCommentVH: TopCommentItemViewHolder? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ITEM_COMMENT_TOP -> {
                val binding: ItemArticleDetailCommentBinding =
                    ItemArticleDetailCommentBinding.inflate(mLayoutInflater, parent, false)
                TopCommentItemViewHolder(binding).apply { topCommentVH = this }
            }

            else -> super.onCreateViewHolder(parent, viewType)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is TopCommentItemViewHolder -> {
                holder.bindView(mEntityList[position].commentTop!!)
            }

            is CommentFilterViewHolder -> {
                holder.bindView(commentDetail = true)
            }

            else -> super.onBindViewHolder(holder, position)
        }
    }

    inner class TopCommentItemViewHolder(var binding: ItemArticleDetailCommentBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bindView(comment: CommentEntity) {
            ConstraintSet().apply {
                clone(binding.contentTv.parent as ConstraintLayout)
                clear(binding.contentTv.id, ConstraintSet.START)
                connect(binding.contentTv.id, ConstraintSet.START, binding.userIconIv.id, ConstraintSet.START)

                clear(binding.commentPictureRv.id, ConstraintSet.START)
                connect(binding.commentPictureRv.id, ConstraintSet.START, binding.userIconIv.id, ConstraintSet.START)

                clear(binding.bottomContainer.id, ConstraintSet.START)
                connect(binding.bottomContainer.id, ConstraintSet.START, binding.userIconIv.id, ConstraintSet.START)

                applyTo(binding.contentTv.parent as ConstraintLayout)
            }
            (binding.contentTv.layoutParams as ConstraintLayout.LayoutParams).apply {
                topMargin = 16F.dip2px()
                leftMargin = 9F.dip2px()
                binding.contentTv.layoutParams = this
            }

            (binding.commentPictureRv.layoutParams as ConstraintLayout.LayoutParams).apply {
                leftMargin = 9F.dip2px()
                binding.commentPictureRv.layoutParams = this
            }

            (binding.bottomContainer.layoutParams as ConstraintLayout.LayoutParams).apply {
                leftMargin = 9F.dip2px()
                binding.bottomContainer.layoutParams = this
            }

            binding.run {
                moreIv.visibility = View.GONE
                divider.visibility = View.VISIBLE
                commentCountTv.visibility = View.GONE
                bottomFloorHintTv.goneIf(mViewModelCommunity.topCommentId.isNotBlank()) {
                    bottomFloorHintTv.text = if (comment.floor != 0) "${comment.floor}楼" else ""
                }
                contentTv.setTextWithHighlightedTextWrappedInsideWrapper(
                    text = comment.content ?: "",
                    highlightedTextClickListener = TextHelper.DirectToWebViewHighlightedTextClick(
                        binding.root.context,
                        mEntrance
                    )
                )
                contentTv.maxLines = Int.MAX_VALUE
                commentTopTimeTv.goneIf(mViewModelCommunity.topCommentId.isBlank())
                commentTopTimeTv.text = if (comment.source != null && comment.source!!.region.isNotEmpty()) {
                    "${CommentUtils.getCommentTime(comment.time)} · ${comment.source!!.region}"
                } else {
                    CommentUtils.getCommentTime(comment.time)
                }

                timeTv.goneIf(mViewModelCommunity.topCommentId.isNotBlank())
                originalTv.goneIf(mViewModelCommunity.topCommentId.isBlank())
                originalTv.text = "查看原文"
                originalTv.setOnClickListener {
                    mViewModelCommunity.run {
                        val path = "评论详情-查看原文"
                        when {
                            articleId.isNotEmpty() -> {
                                DirectUtils.directToCommunityArticle(
                                    mContext,
                                    articleId,
                                    communityId,
                                    mEntrance,
                                    path
                                )
                            }
                            videoId.isNotEmpty() -> {
                                DirectUtils.directToVideoDetail(
                                    mContext,
                                    videoId,
                                    mEntrance,
                                    path
                                )
                            }
                            questionId.isNotEmpty() -> {
                                DirectUtils.directToQuestionDetail(
                                    mContext,
                                    questionId,
                                    mEntrance,
                                    path
                                )
                            }
                            gameCollectionId.isNotEmpty() -> {
                                DirectUtils.directToGameCollectionDetail(
                                    mContext,
                                    gameCollectionId,
                                    mEntrance,
                                    path
                                )
                            }
                        }
                    }
                }
            }

            CommentItemViewHolder.bindComment(binding, mViewModelCommunity, comment, mEntrance, null)

            if (mViewModelCommunity.topCommentId.isNotBlank()) {
                val floorText = if (comment.floor != 0) "${comment.floor}楼" else ""
                binding.time.text = "$floorText  ${binding.time.text}"
            }
        }
    }

}