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

import android.text.SpannableStringBuilder
import android.text.Spanned
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.im.databinding.LayoutSessionMessageBaseBinding
import com.adealink.weparty.im.databinding.LayoutSessionMessageTipsBinding
import com.adealink.weparty.im.databinding.LayoutSessionMessageUnsupportBinding
import com.adealink.weparty.im.session.adapter.data.UnSupportMessageBean
import com.adealink.weparty.util.formatTimeTo
import com.tencent.qcloud.tuikit.timcommon.R
import com.tencent.qcloud.tuikit.timcommon.TIMCommonService
import com.tencent.qcloud.tuikit.timcommon.interfaces.OnItemClickListener
import com.tencent.qcloud.tuikit.timcommon.util.TextUtil.ForegroundColorClickableSpan
import com.tencent.qcloud.tuikit.tuichat.bean.message.TipsMessageBean

/**
 * 对照: TipsMessageHolder
 */

class TipsMessageViewHolder(
    rootBinding: LayoutSessionMessageBaseBinding,
    binding: LayoutSessionMessageTipsBinding
) : MessageViewHolder<TipsMessageBean, LayoutSessionMessageTipsBinding>(rootBinding, binding) {
    override fun onBindSelfMessage(
        binding: LayoutSessionMessageTipsBinding,
        msg: TipsMessageBean
    ) {
        setTips(binding, msg)
        setMessageTime(binding, msg)
        setReadStatus(binding, msg, true)
    }

    override fun onBindOtherMessage(
        binding: LayoutSessionMessageTipsBinding,
        msg: TipsMessageBean
    ) {
        setTips(binding, msg)
        setMessageTime(binding, msg)
        setReadStatus(binding, msg, false)
    }

    private fun setTips(
        binding: LayoutSessionMessageTipsBinding,
        msg: TipsMessageBean
    ) {
        val targetUserNameMap = msg.targetUserMap
        val operationUserPair = msg.operationUserPair
        val text = msg.text
        val builder = SpannableStringBuilder(msg.text)
        val color = TIMCommonService.getAppContext().getResources()
            .getColor(R.color.common_quote_user_name_color)
        if (!targetUserNameMap.isEmpty()) {
            for (entry in targetUserNameMap.entries) {
                val start = text.indexOf(entry.value!!)
                if (start != -1) {
                    val end = start + entry.value!!.length
                    val span = ForegroundColorClickableSpan(color, object : View.OnClickListener {
                        override fun onClick(v: View?) {
                            // TODO:
//                            onUserClick(entry.key)
                        }
                    })
                    builder.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                }
            }
        }
        if (operationUserPair != null) {
            val start = text.indexOf(operationUserPair.second!!)
            if (start != -1) {
                val end = start + operationUserPair.second!!.length
                val span = ForegroundColorClickableSpan(color, object : View.OnClickListener {
                    override fun onClick(v: View?) {
                        // TODO:
//                        onUserClick(operationUserPair.first)
                    }
                })
                builder.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
        }
        binding.tvText.text = builder
    }

    private fun setMessageTime(
        binding: LayoutSessionMessageTipsBinding,
        item: TipsMessageBean
    ) {
        binding.tvTime.text = formatTimeTo((item.messageTime * 1000), "HH:mm")
    }

    private fun setReadStatus(
        binding: LayoutSessionMessageTipsBinding,
        msg: TipsMessageBean,
        isSelf: Boolean
    ) {
        if (!isSelf) {
            binding.ivReadStatus.gone()
            return
        }
        binding.ivReadStatus.show()
        if (msg.isPeerRead) {
            binding.ivReadStatus.setImageResource(com.adealink.weparty.im.R.drawable.im_session_message_read_ic)
        } else {
            binding.ivReadStatus.setImageResource(com.adealink.weparty.im.R.drawable.im_session_message_unread_ic)
        }
    }
}

class TipsMessageViewBinder(
    onItemClickListener: OnItemClickListener
) : BaseMessageViewBinder<TipsMessageBean, LayoutSessionMessageTipsBinding, TipsMessageViewHolder>(
    onItemClickListener
) {

    override fun onCreateMessageHolder(
        rootBinder: LayoutSessionMessageBaseBinding,
        inflater: LayoutInflater,
        parent: ViewGroup
    ): TipsMessageViewHolder {
        return TipsMessageViewHolder(
            rootBinder,
            LayoutSessionMessageTipsBinding.inflate(
                inflater,
                parent,
                true
            )
        )
    }

}