package com.ez.ertc.call.ui

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.ez.basertc.RTCConstant
import com.ez.ertc.R
import com.ez.ertc.call.ui.UserAdapter.MyViewHolder
import com.ez.ertc.call.bean.RemoteUser
import com.ez.ertc.call.bean.UserStatusData

class UserAdapter internal constructor(private val localuserId: String, private var mRemoteUserList: ArrayList<UserStatusData>) :
    RecyclerView.Adapter<MyViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.list_client, parent, false)
        )
    }

    fun notifyDataSetChanged(mRemoteUserList: ArrayList<UserStatusData>){
        this.mRemoteUserList = mRemoteUserList
        notifyDataSetChanged()
    }
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val remoteUser = mRemoteUserList[position]
        holder.tvName?.text = "${remoteUser.userId}"
        holder.tvNetworkQuality?.text = "网络${networkQualityDesc[remoteUser.networkQuality]}"
        if(remoteUser.userId == localuserId) {
            holder.imgSelected?.visibility = View.VISIBLE
        } else {
            holder.imgSelected?.visibility =
                if (remoteUser.streamType == RTCConstant.ERTC_VIDEO_STREAM_TYPE_NULL) View.INVISIBLE else View.VISIBLE
        }
    }
    override fun getItemCount(): Int {
        return mRemoteUserList.size
    }

    class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
        var tvName: TextView? = itemView?.findViewById<View>(R.id.tv_user_id) as TextView
        var tvNetworkQuality: TextView? = itemView?.findViewById<View>(R.id.tv_quality) as TextView
        var imgSelected: ImageView? = itemView?.findViewById<View>(R.id.img_check) as ImageView
    }

    public companion object{
        val networkQualityDesc = arrayOf("未知", "非常好", "比较好", "一般", "较差", "很差", "不可用", "无法检测", "检测中")
    }
}