package com.adealink.weparty.im.session.adapter.viewbinder

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.viewbinding.ViewBinding
import com.adealink.frame.aab.util.getCompatDimension
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.im.R
import com.adealink.weparty.im.databinding.LayoutSessionMessageBaseBinding
import com.adealink.weparty.util.getSessionTimeStr
import com.tencent.qcloud.tuikit.timcommon.bean.TUIMessageBean
import com.tencent.qcloud.tuikit.timcommon.interfaces.OnItemClickListener


/**
 * 对照: ImageMessageHolder
 */
abstract class BaseMessageViewBinder<T : TUIMessageBean, V : ViewBinding, MH : MessageViewHolder<T, V>>(
    protected var onItemClickListener: OnItemClickListener?
) : ItemViewBinder<T, MH>() {

    override fun onBindViewHolder(holder: MH, item: T) {
        initView(holder, item)
        setTimeTitle(holder, item)

        holder.initView(holder.messageBinding, item, onItemClickListener)
        if (item.isSelf) {
            applySelfStyle(holder, item)
            holder.onBindSelfMessage(holder.messageBinding, item)
        } else {
            applyOtherStyle(holder, item)
            holder.onBindOtherMessage(holder.messageBinding, item)
        }
    }

    @CallSuper
    open fun initView(
        holder: MH, msg: T
    ) {
        holder.binding.root.onClick {
            onItemClickListener?.onMessageClick(holder.binding.root, msg)
            true
        }
        if (msg.status == TUIMessageBean.MSG_STATUS_SEND_FAIL) {
            //消息发送失败触发长按
            holder.messageBinding.root.onClick {
                onItemClickListener?.onMessageLongClick(holder.binding.root, msg)
                true
            }
        } else {
            holder.messageBinding.root.onClick {
                onItemClickListener?.onMessageClick(holder.binding.root, msg)
                true
            }
        }
    }

    private fun applySelfStyle(holder: MH, msg: T) {
        holder.binding.llContent.gravity = Gravity.END
        holder.messageBinding.root.setBackgroundResource(R.drawable.im_message_bubble_self_bg)
        holder.messageBinding.root.setPaddingRelative(
            getCompatDimension(R.dimen.im_message_bubble_self_padding_start).toInt(),
            getCompatDimension(R.dimen.im_message_bubble_self_padding_top).toInt(),
            getCompatDimension(R.dimen.im_message_bubble_self_padding_end).toInt(),
            getCompatDimension(R.dimen.im_message_bubble_self_padding_bottom).toInt(),
        )

        //--------通用消息状态
        setStatusImage(holder, msg)

    }

    private fun applyOtherStyle(holder: MH, msg: T) {
        holder.binding.llContent.gravity = Gravity.START
        holder.messageBinding.root.setBackgroundResource(R.drawable.im_message_bubble_other_bg)
        holder.messageBinding.root.setPaddingRelative(
            getCompatDimension(R.dimen.im_message_bubble_other_padding_start).toInt(),
            getCompatDimension(R.dimen.im_message_bubble_other_padding_top).toInt(),
            getCompatDimension(R.dimen.im_message_bubble_other_padding_end).toInt(),
            getCompatDimension(R.dimen.im_message_bubble_other_padding_bottom).toInt(),
        )

        //--------通用消息状态
        holder.binding.ivMessageStatus.gone()
    }


    protected fun setStatusImage(holder: MH, msg: T) {
        if (msg.hasRiskContent()) {
            holder.binding.ivMessageStatus.show()
        } else if (msg.status == TUIMessageBean.MSG_STATUS_SEND_FAIL) {
            holder.binding.ivMessageStatus.show()
            holder.binding.ivMessageStatus.onClick {
                onItemClickListener?.onSendFailBtnClick(holder.binding.ivMessageStatus, msg)
            }
        } else {
            holder.binding.ivMessageStatus.gone()
        }
    }

    private fun setTimeTitle(holder: MH, msg: T) {
        val position = holder.layoutPosition
        val last: TUIMessageBean? = adapter.items.getOrNull(position - 1) as? TUIMessageBean
        val timeStr = getSessionTimeStr(msg.messageTime * 1000, (last?.messageTime ?: 0) * 1000)
        if (timeStr.isNullOrEmpty()) {
            holder.binding.tvTime.gone()
        } else {
            holder.binding.tvTime.show()
            holder.binding.tvTime.text = timeStr
        }
    }

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): MH {
        val rootBinding = LayoutSessionMessageBaseBinding.inflate(inflater, parent, false)
        return onCreateMessageHolder(rootBinding, inflater, rootBinding.messageContent)
    }

    abstract fun onCreateMessageHolder(
        rootBinder: LayoutSessionMessageBaseBinding,
        inflater: LayoutInflater,
        parent: ViewGroup
    ): MH

}

abstract class MessageViewHolder<T : TUIMessageBean, V : ViewBinding>(
    rootBinding: LayoutSessionMessageBaseBinding,
    val messageBinding: V
) : BindingViewHolder<LayoutSessionMessageBaseBinding>(rootBinding) {

    open fun initView(binding: V, msg: T, onItemClickListener: OnItemClickListener?) {}

    abstract fun onBindSelfMessage(binding: V, msg: T)

    abstract fun onBindOtherMessage(binding: V, msg: T)

}