package com.yidianling.medical.im.view

import android.content.Context
import android.text.Html
import android.view.View
import android.widget.LinearLayout
import com.ydl.ydl_image.module.GlideApp
import com.yidianling.im.R
import com.yidianling.im.preference.ImTempData
import com.yidianling.im.ui.page.fragment.bean.ChatItemBean
import kotlinx.android.synthetic.main.medical_im_chat_item_view.view.*

/**
 * 互动列表普通itemView
 */
class MedicalChatItemView(mContext: Context) : LinearLayout(mContext) {

    init {
        val params = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams = params
        View.inflate(mContext, R.layout.medical_im_chat_item_view, this)
    }

    fun initData(bean: ChatItemBean?) {
        bean?.let {
            GlideApp.with(context)
                .load(bean.head)
                .circleCrop()
                .into(chat_message_img)
            chat_message_name.text = bean.toName
            if (bean.doctorTitle != "") {
                chat_message_tab.visibility = View.VISIBLE
                chat_message_tab.text = bean.doctorTitle
            } else {
                chat_message_tab.visibility = View.GONE
            }


            chat_message_time.text = bean.timeStr
            if (ImTempData.getInstance().getTempMsg(bean.toUid.toString()) != null && ImTempData.getInstance().getTempMsg(
                    bean.toUid.toString()
                ) != ""
            ) run {
                //if草稿箱有消息则显示
                val th =
                    "<font color='#EA0000' charset='utf-8'>[草稿]</font> " + ImTempData.getInstance().getTempMsg(
                        bean.toUid.toString()
                    )
                chat_message_content.setText(Html.fromHtml(th))
            }else {
                chat_message_content.text = bean.content
            }

            if (bean.unReadNum > 99) {
                chat_message_unread_num.visibility = View.VISIBLE
                chat_message_unread_num.text = "99+"
            } else if (bean.unReadNum > 0) {
                chat_message_unread_num.visibility = View.VISIBLE
                chat_message_unread_num.text = "${bean.unReadNum}"
            } else {
                chat_message_unread_num.visibility = View.GONE
            }

            if (bean.is_top == 1) {
                chat_global_ll.setBackgroundResource(R.drawable.im_chat_is_top_bg)
            } else {
                chat_global_ll.setBackgroundResource(0)
            }

        }
    }

}