package com.zj365.mime.adapter

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.text.Html
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.tamsiree.rxkit.RxTimeTool
import com.xty.base.adapter.BaseAdapter
import com.xty.common.setImageUser
import com.zj365.mime.R
import com.xty.network.model.MessageBean
import com.xty.network.model.MessageTypeDataBean
import de.hdodenhof.circleimageview.CircleImageView
import okhttp3.internal.filterList


class MessageAdapter():BaseAdapter<Any>(R.layout.item_message) {

    private var isChoiceMode:Boolean = false
    private val deletePos:MutableList<Int> = mutableListOf()

    override fun convert(holder: BaseViewHolder, item: Any) {
//        if(item is MessageBean.Msg){
//            holder.setGone(R.id.mRelMsg,true).setGone(R.id.mChat,false)
//            holder.getView<CircleImageView>(R.id.mImage).setImageUser(context,item.avatar_url)
//
//            holder.setText(R.id.mNickName,item.realname)
//                .setText(R.id.mContent,item.process_detail)
//                .setText(R.id.mTime,item.create_time)
//
//            holder.setGone(R.id.mDelete,true)
//            setBadge(item.count,holder.getView(R.id.msgCard),holder.getView(R.id.mBadge))
//        }else
        if(item is MessageTypeDataBean.MessageTypeBean){
            holder.setGone(R.id.mRelMsg,false).setGone(R.id.mChat,true)
            holder.setText(R.id.mContent, Html.fromHtml(item.notifyContent))
                .setText(R.id.mTvTime,RxTimeTool.milliseconds2String(item.notifyCreateTime))
                .setText(R.id.notifyTitle,item.notifyTitle)
//            changeStatus(holder.getView(R.id.mContent),item.notifyStatus)
            if(item.notifyStatus==0){
                holder.setGone(R.id.tv_remind,false)
            }else{
                holder.setGone(R.id.tv_remind,true)
            }
            holder.setGone(R.id.mDelete,false)
            //设置是否为选择模式
            holder.setGone(R.id.cbChoice,!isChoiceMode)

            holder.getView<CheckBox>(R.id.cbChoice).isChecked =   item.isChoice

        }

    }


    private fun changeStatus(tv: TextView,status:Int) {
        var draw =ContextCompat.getDrawable(context,R.drawable.shape_msg_status) as GradientDrawable
        if(status==0){
            draw.setColor(ContextCompat.getColor(context,R.color.col_e03))
        }else{
            draw.setColor(ContextCompat.getColor(context,R.color.white))
        }
        draw.mutate()
        draw.setBounds(0,0,draw.minimumWidth,draw.minimumHeight)
        tv.setCompoundDrawables(draw,null,null,null)
    }

    private fun setBadge(num:Int,carrdView:CardView,tv: TextView) {
            if (num > 0) {
                tv.visibility= View.VISIBLE
                tv.text = if (num> 99) "99+" else "$num"
                tv.post {
                    val layoutParams = tv.layoutParams as ViewGroup.LayoutParams
                    val text = tv.text.toString()
                    carrdView.radius=tv.height/2f
                    var textWidth =  tv.paint.measureText(text)
                    if (tv.height >= textWidth) {   //等宽高的圆形
                        layoutParams.width = tv.height

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


     fun setChoiceMode(mode:Boolean){
        this.isChoiceMode = mode
         notifyDataSetChanged()
    }

    fun choiceAll(){
        data.forEachIndexed { pos, any ->
           val item = any as MessageTypeDataBean.MessageTypeBean
            if (!item.isChoice){
                item.isChoice = true
                deletePos.add(pos)
        }else{
                deletePos.remove(pos)
            }
        }
        notifyDataSetChanged()
    }

   public fun getDeletePos():MutableList<Int>{
        return deletePos
    }

}


