package com.zxhl.cms.widget

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.zxhl.cms.AppContext
import com.zxhl.cms.R
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.box.RoomMemberEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener

class ChatFriendAdapter : BaseRecyclerAdapter<RoomMemberEntity, ChatFriendAdapter.ViewHolder> {
    private var mContext: Context? = null
    private val listener: OnRecycleItemClickListener<RoomMemberEntity>

    constructor(
        content: Context?,
        listener: OnRecycleItemClickListener<RoomMemberEntity>
    ) : super() {
        this.listener = listener
        mContext = content
    }

    override fun onCreateViewHolder(holder: ViewGroup, p1: Int): ViewHolder {


        return ViewHolder(
            LayoutInflater.from(holder.context)
                .inflate(R.layout.item_layout_chat_friend, holder, false)
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = mList[position]
        holder.id_tv_name.text = data.nickname
        if (TextUtils.equals(data.sex, "1")) {
            //男
            holder.id_img_sex.setImageResource(R.drawable.icon_nan_da)
        } else {
            holder.id_img_sex.setImageResource(R.drawable.icon_nv_da)
        }


        if (!TextUtils.isEmpty(data.image)) {
            Glide.with(AppContext.get()).load(data.image)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .into(holder.id_img_head)
        } else {
            holder.id_img_head.setImageResource(R.drawable.icon_user_photo_default)
        }

        holder.id_item.setOnClickListener {
            listener.onItemClick(it, position, data)
        }
    }

    inner class ViewHolder : RecyclerView.ViewHolder {
        var id_tv_name: TextView
        var id_img_head: CircleImageView
        var id_img_sex: ImageView
        var id_item: LinearLayout

        constructor(itemView: View) : super(itemView) {
            id_tv_name = itemView.findViewById(R.id.id_tv_name)
            id_img_head = itemView.findViewById(R.id.id_img_head)
            id_img_sex = itemView.findViewById(R.id.id_img_sex)
            id_item = itemView.findViewById(R.id.id_item)

        }
    }
}