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.SettingPreference
import com.zxhl.cms.net.model.box.RoomMemberEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils

class RoomMemberAdapter : BaseRecyclerAdapter<RoomMemberEntity, RoomMemberAdapter.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_room_member, holder, false)
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = mList[position]
        holder.id_tv_name.text = data.nickname
        if (TextUtils.equals(SettingPreference.getUid(), data.uid)) {
            //是他自己
            holder.id_tv_follow.visibility = View.GONE
        } else {
            holder.id_tv_follow.visibility = View.VISIBLE
            if (data.hasFollow == false) {
                holder.id_tv_follow.text = "关注"
            } else {
                holder.id_tv_follow.text = "已关注"
            }
        }
        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_tv_accompany.text = "${data.age}岁·陪伴${data.accompany}分钟"
        holder.id_tv_follow.setOnClickListener {
            if (data.hasFollow == false) {
                data.hasFollow = true
                listener.onItemClick(it, position, data)
                holder.id_tv_follow.text = "已关注"
            }

        }
        holder.id_rl_room_member_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_tv_follow: TextView
        var id_tv_accompany: TextView
        var id_rl_room_member_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_tv_follow = itemView.findViewById(R.id.id_tv_follow)
            id_tv_accompany = itemView.findViewById(R.id.id_tv_accompany)
            id_rl_room_member_item = itemView.findViewById(R.id.id_rl_room_member_item)

        }
    }
}