package com.zxbw.modulemain.adapter

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zxbw.modulemain.R
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.box.MobileOpEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener

class OperatorAdapter : BaseRecyclerAdapter<MobileOpEntity, OperatorAdapter.ViewHolder> {
    private var mContext: Activity? = null
    private val listener: OnRecycleItemClickListener<MobileOpEntity>
    private val mSelectColor: Int
    private val mUnSelectColor: Int
    private var position = -1
    constructor(
        content: Activity?,
        listener: OnRecycleItemClickListener<MobileOpEntity>
    ) : super() {
        mContext = content
        this.listener = listener
        mSelectColor = AppContext.get().resources.getColor(R.color.color_f8e8cc)
        mUnSelectColor = AppContext.get().resources.getColor(R.color.color_666666)
    }
    fun setPosition(pos: Int) {
        position = pos
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: ViewHolder, pos: Int) {
        var bean = mList[pos]
        holder.id_tv_operator?.text=bean.name
        if(pos==position){
            holder.id_tv_operator?.setBackgroundResource(R.drawable.shape_323233_r6)
            holder.id_tv_operator?.setTextColor(mSelectColor)
            listener.onItemClick(holder.itemView, position, bean)
        }else{
            holder.id_tv_operator?.setBackgroundResource(R.drawable.shape_ebebeb_r10)
            holder.id_tv_operator?.setTextColor(mUnSelectColor)
        }

//        holder.id_ll_item?.setOnClickListener {
//            listener.onItemClick(it, position, bean)
//        }
    }

//    override fun getItemCount(): Int {
//        return 3
//    }

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

   inner class ViewHolder : RecyclerView.ViewHolder {

        var id_tv_operator: TextView?



        constructor(
            itemView: View
        ) : super(itemView) {

            id_tv_operator = itemView.findViewById<TextView>(R.id.id_tv_operator)

            id_tv_operator?.setOnClickListener {
                val pos = adapterPosition
                if (pos >= 0 && pos < mList.size) {
                    setPosition(pos)
                    notifyDataSetChanged()
                }
            }
        }
    }
}