package com.adealink.weparty.playmate.detail.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.getActivity
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.module.profile.Profile
import com.adealink.weparty.playmate.databinding.ItemPlaymateDetailCommentBinding
import com.adealink.weparty.playmate.detail.data.PlaymateDetailCommentItemData
import com.adealink.weparty.util.formatTimeStr

class PlaymateDetailCommentViewBinder() :
    ItemViewBinder<PlaymateDetailCommentItemData, PlaymateDetailCommentViewBinder.ViewHolder>() {

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

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

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

        private var item: PlaymateDetailCommentItemData? = 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: PlaymateDetailCommentItemData) {
            this.item = item
            binding.ivAvatar.setImageUrl(item.data.user.avatar)
            binding.ivAvatar.onClick {
                goProfile(it.context.getActivity(), item.data.user.uid)
            }
            binding.tvName.text = item.data.user.nickName
            binding.vEvaluate.setCurScore(item.data.star)
            binding.tvComment.text = item.data.comment
            binding.tvTime.text = item.data.ts.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
            }
        }

        private fun goProfile(context: Context?, uid: String) {
            context ?: return
            Router.build(context, Profile.UserProfile.PATH)
                .putExtra(Profile.Common.EXTRA_UID, uid)
                .start()
        }

    }

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

}