package com.zxhl.main.page.view

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
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.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.box.HomeListEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.main.R

class AccostAdapter : BaseRecyclerAdapter<HomeListEntity, AccostAdapter.ViewHolder> {
    private var mContext: Context? = null
    private val listener: OnRecycleItemClickListener<Boolean>
    private val checkStatus: MutableMap<Int, Boolean> = HashMap()
    constructor(
        content: Context?,
        listener: OnRecycleItemClickListener<Boolean>
    ) : super() {
        mContext = content
        this.listener = listener
        initCheck(true)
    }
    //更改集合内部存储的状态
    fun initCheck(flag: Boolean) {
        for (i in 0 until mList.size) {
            //更改指定位置的数据
            checkStatus.put(i, flag)
        }
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var bean = mList[position]
        holder.id_tv_names?.text = bean.nickname
        if (!TextUtils.isEmpty(bean.headImage)) {
            Glide.with(AppContext.get()).load(bean.headImage)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .skipMemoryCache(true)
                .dontAnimate()
                .into(holder.id_img_head_accost!!)
        } else {
            holder.id_img_head_accost?.setImageResource(R.drawable.icon_user_photo_default)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(AppContext.get())
                .inflate(R.layout.item_layout_accost, parent, false)
        )
    }

  inner  class ViewHolder : RecyclerView.ViewHolder {
        var id_img_head_accost: ImageView?
        var id_tv_names: TextView?
        var id_img_choose: CheckBox?
        var item_rl: LinearLayout?

        constructor(
            itemView: View
        ) : super(itemView) {
            id_img_head_accost = itemView.findViewById<ImageView>(R.id.id_img_head_accost)
            id_img_choose = itemView.findViewById<CheckBox>(R.id.id_img_choose)
            id_tv_names = itemView.findViewById<TextView>(R.id.id_tv_names)
            item_rl = itemView.findViewById<LinearLayout>(R.id.id_rl_item)
            id_img_choose?.setOnCheckedChangeListener { compoundButton, b ->
                val pos = adapterPosition
                if (pos >= 0 && pos < mList.size) {
                    if (b) {
                        id_img_choose?.setBackgroundResource(R.drawable.icon_xuanzhong_dashan)
                    } else {
                        id_img_choose?.setBackgroundResource(R.drawable.icon_weixuanzhong_dashan)
                    }
                    checkStatus.put(position, b)
                   listener.onItemClick(item_rl, position, b)
                }
            }
            item_rl?.setOnClickListener {
                id_img_choose?.isChecked = !(id_img_choose?.isChecked ?: true)
            }
        }

    }
}