package com.adealink.weparty.profile.dynamic.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.profile.databinding.LayoutDynamicCommentBinding
import com.adealink.weparty.profile.dynamic.fragment.data.DynamicDetailCommentItem
import com.adealink.weparty.util.formatTimeStr

class DynamicCommentViewBinder() :
    ItemViewBinder<DynamicDetailCommentItem, DynamicCommentViewBinder.ViewHolder>() {

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): ViewHolder {
        return ViewHolder(LayoutDynamicCommentBinding.inflate(inflater, parent, false)).apply {
            initView()
        }
    }

    override fun onBindViewHolder(
        holder: ViewHolder,
        item: DynamicDetailCommentItem
    ) {
        holder.update(item)
    }

    class ViewHolder(
        rootBinding: LayoutDynamicCommentBinding,
    ) : BindingViewHolder<LayoutDynamicCommentBinding>(rootBinding) {

        private var item: DynamicDetailCommentItem? = null

        private var checkEllipsis = false
        private val onEllipsisListeners: (Boolean) -> Unit = object : (Boolean) -> Unit {

            override fun invoke(p1: Boolean) {
                if (!checkEllipsis) {
                    return
                }
                if (item == null) {
                    binding.tvExpand.gone()
                    return
                }
                if (showExpand(p1)) {
                    binding.tvExpand.show()
                } else {
                    binding.tvExpand.gone()
                }
            }
        }

        private fun showExpand(isTextEllipsis: Boolean): Boolean {
            val itemData = item
            if (itemData == null) {
                return false
            }
            if (!isTextEllipsis) {
                return false
            }
            return !itemData.isExpand
        }

        fun initView() {
            binding.tvComment.addEllipsisListener(onEllipsisListeners)
        }

        fun update(item: DynamicDetailCommentItem) {
            this.item = item
            binding.ivAvatar.setImageUrl(item.comment.avatar)
            binding.tvName.text = item.comment.nickname
            binding.tvComment.show(!item.comment.comment.isNullOrEmpty())
            binding.tvComment.text = item.comment.comment
            binding.tvTime.text = item.comment.createdAt.formatTimeStr()

            val commentText = binding.tvComment
            val expandView = binding.tvExpand
            if (item.isExpand) {
                //评论已经点击展开后,不做显示
                commentText.maxLines = Int.MAX_VALUE
                expandView.gone()
            } else {
                //评论未点击展开,计算是否超出行数限制
                commentText.maxLines = DEFAULT_COMMENT_LINE
                expandView.gone()
                checkEllipsis = true
                commentText.checkEllipsis()
            }
            binding.tvExpand.onClick { expandView ->
                expandView.gone()
                commentText.maxLines = Int.MAX_VALUE
                item.isExpand = true
            }
        }

    }

    companion object {
        private const val DEFAULT_COMMENT_LINE = 3 //默认显示3行
    }

}