package com.zxhl.main.page.adapter

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.RelativeLayout
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.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.chat.FollowEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.main.R

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

    constructor(
        content: Context?,
        listener: OnRecycleItemClickListener<FollowEntity>
    ) : 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_fans, holder, false)
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = mList[position]
        holder.id_tv_name.text = data.nickname
        if (!TextUtils.isEmpty(data.headImage)) {
            Glide.with(AppContext.get()).load(data.headImage)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .into(holder.id_img_follow)
        } else {
            holder.id_img_follow.setImageResource(R.drawable.icon_user_photo_default)
        }
        holder.id_tv_look_time.text="${data.dateStr} 看过你"
        if(TextUtils.equals(data.hasFollows,"0")){
            holder.id_tv_if_follow.text="关注Ta"
            holder.id_tv_if_follow.setBackgroundResource(R.drawable.shape_aa60ff_r3)
            holder.id_tv_if_follow.setTextColor(AppContext.get().resources.getColor(R.color.white))
        }else{
            holder.id_tv_if_follow.text="互相关注"
            holder.id_tv_if_follow.setBackgroundResource(R.drawable.shape_999999_r3)
            holder.id_tv_if_follow.setTextColor(AppContext.get().resources.getColor(R.color.color_999999))
        }

    }

    inner class ViewHolder : RecyclerView.ViewHolder {


        var id_tv_name: TextView
        var id_tv_look_time: TextView
        var id_tv_if_follow: TextView
        var id_img_follow: ImageView
        var id_rl_item: RelativeLayout

        constructor(itemView: View) : super(itemView) {
            id_img_follow = itemView.findViewById(R.id.id_img_follow_head)
            id_tv_name = itemView.findViewById(R.id.id_tv_follow_name)
            id_tv_look_time = itemView.findViewById(R.id.id_tv_look_time)
            id_tv_if_follow = itemView.findViewById(R.id.id_tv_if_follow)
            id_rl_item = itemView.findViewById(R.id.id_rl_item)
            id_tv_if_follow.setOnClickListener {
                val pos = adapterPosition
                if (pos >= 0 && pos < mList.size) {
                    listener.onItemClick(it,pos,mList[pos])
                }
            }
            id_rl_item.setOnClickListener {
                val pos = adapterPosition
                if (pos >= 0 && pos < mList.size) {
                    listener.onItemClick(it,pos,mList[pos])
                }
            }

        }
    }
}