package com.gh.gamecenter.gamecollection.detail.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.R
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.ItemArticleDetailCommentBinding
import com.gh.gamecenter.entity.CommentEntity
import com.gh.gamecenter.gamecollection.detail.GameCollectionDetailAdapter
import com.gh.gamecenter.gamecollection.detail.GameCollectionDetailViewModel

class GameCollectionCommentConversationAdapter(
    context: Context,
    mViewModel: GameCollectionDetailViewModel,
    type: AdapterType,
    mEntrance: String,
    commentClosure: ((CommentEntity) -> Unit)? = null
) : GameCollectionDetailAdapter(context, type, mEntrance, mViewModel, 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 CommentFilterViewHolder -> {
                holder.bindView(commentDetail = true)
            }

            is TopCommentItemViewHolder -> {
                holder.bindView(mEntityList[position].commentTop!!)
            }

            is CommentFooterViewHolder -> {
                holder.itemView.setBackgroundColor(R.color.background_white.toColor(mContext))
                holder.bindView(mIsLoading, mIsNetworkError, mIsOver)
            }

            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
                floorHintTv.text = if (comment.floor != 0) "${comment.floor}楼" else ""
                contentTv.text = comment.content
                contentTv.maxLines = Int.MAX_VALUE
                commentTopTimeTv.goneIf(mViewModel.topCommentId.isBlank())
                commentTopTimeTv.text = CommentUtils.getCommentTime(comment.time)
                timeTv.goneIf(mViewModel.topCommentId.isNotBlank())
                originalTv.goneIf(mViewModel.topCommentId.isBlank())
                originalTv.text = "查看游戏单"
                originalTv.setOnClickListener {
                    DirectUtils.directToGameCollectionDetail(
                        mContext,
                        mViewModel.gameCollectionId,
                        mEntrance,
                        "评论详情-查看游戏单"
                    )
                }
            }

            bindNormalComment(binding, comment)
        }
    }

}