package com.zj365.dc.adapter

import android.text.Html
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.cardview.widget.CardView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.xty.common.Dateutils
import com.xty.network.model.SystemMsgBean
import com.zj365.dc.R

class HomeMessageAdapter : BaseAdapter<SystemMsgBean>(R.layout.item_home_message) {
    override fun convert(holder: BaseViewHolder, item: SystemMsgBean) {
        if (item.type == 1){
            holder.getView<ImageView>(R.id.img_msg).setImageDrawable(context.resources.getDrawable(R.mipmap.icon_msg_system))
        }else if (item.type == 2){
            holder.getView<ImageView>(R.id.img_msg).setImageDrawable(context.resources.getDrawable(R.mipmap.icon_msg_assistant))

        }else{
            holder.getView<ImageView>(R.id.img_msg).setImageDrawable(context.resources.getDrawable(R.mipmap.icon_msg_welfare))
        }
        holder.setText(R.id.tv_msg_title,item.name)
        holder.setText(R.id.tv_msg_content, Html.fromHtml(item.msg))
        if(!TextUtils.isEmpty(item.time)){
            holder.setText(R.id.tv_msg_time,Dateutils.formatTime(item.time.toLong(),Dateutils.YYYY_MM_DD_HH_MM))
        }

        var num =  holder.getView<TextView>(R.id.mBadge)
        var card = holder.getView<CardView>(R.id.mCard)
        if(item.unReadCount > 0){
            card.visibility = View.VISIBLE
            num.text = if (item.unReadCount > 99) "99+" else "${item.unReadCount}"

            num.post{
                var layoutParams = num.layoutParams as ViewGroup.LayoutParams
                var numTxt = num.text.toString()
                card.radius = num.height / 2f
                var textWidth = num.paint.measureText(numTxt)
                if (num.height >= textWidth) {   //等宽高的圆形
                    layoutParams.width = num.height

                } else {  //宽高不等 将宽度两边加10
                    layoutParams.width = textWidth.toInt() + 20
                }
                num.layoutParams = layoutParams
            }

        }else{
            card.visibility = View.GONE
        }
    }
}