package com.gh.gamecenter.qa.comment

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.OnCommentCallBackListener
import com.gh.gamecenter.databinding.StairsCommentItemBinding
import com.gh.gamecenter.feature.entity.CommentEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class StairsCommentReplyAdapter(
    context: Context,
    val mViewModel: NewCommentViewModel,
    val commentEntity: CommentEntity,
    val mEntrance: String,
    val mIsShowingConversation: Boolean,
    val mCommentOptionClickListener: OnCommentOptionClickListener?,
    val mCommentCallBackListener: OnCommentCallBackListener
) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {
    val replyComments = ArrayList<CommentEntity>()

    var page = 0

    fun getCommentReply(resultCount: (count: Int) -> Unit) {
        mViewModel.getCommentReply(mViewModel.videoId, commentEntity.id
            ?: "", page, resultCallback = {
            val lastPosition = replyComments.size
            replyComments.addAll(it)
            notifyItemInserted(lastPosition)
            resultCount.invoke(it.size)
        })
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = mLayoutInflater.inflate(R.layout.stairs_comment_item, parent, false)
        return StairsCommentViewHolder(StairsCommentItemBinding.bind(view), mEntrance, true)
    }

    override fun getItemCount(): Int = replyComments.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val replyCommentEntity = replyComments[position]
        if (holder is StairsCommentViewHolder) {
            holder.initCommentViewHolder(
                holder,
                mViewModel,
                replyCommentEntity,
                mEntrance,
                mIsShowingConversation,
                mCommentOptionClickListener,
                mCommentCallBackListener
            )

            val commentContainerParams = holder.binding.commentContainer.layoutParams as LinearLayout.LayoutParams
            commentContainerParams.topMargin = 16f.dip2px()
            holder.binding.commentContainer.layoutParams = commentContainerParams

            holder.binding.commentUserIcon.setAvatarWidth(22f.dip2px().toFloat())
            holder.binding.commentUserIcon.setBadgeWidth(6f.dip2px().toFloat())

            holder.binding.sdvUserBadge.visibility = View.GONE
            holder.binding.commentUserName.textSize = 12f

            if (commentEntity.reply == 0) {
                val params = holder.binding.commentUsernameAndTime.layoutParams as RelativeLayout.LayoutParams
                params.bottomMargin = 0
                holder.binding.commentUsernameAndTime.layoutParams = params
            }
        }
    }
}