package com.zxhl.main.page.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.chat.RoomTypeEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.main.R

class RoomTypeAdapter : BaseRecyclerAdapter<RoomTypeEntity, RoomTypeAdapter.ViewHolder> {
    private var position = -1
    private val mSelectColor: Int
    private val mUnSelectColor: Int
    private var mContext: Context? = null
    private val listener: OnRecycleItemClickListener<RoomTypeEntity>

    constructor(
        content: Context?,
        listener: OnRecycleItemClickListener<RoomTypeEntity>
    ) : super() {
        this.listener = listener
        mContext = content
        mSelectColor = AppContext.get().resources.getColor(com.zxhl.cms.R.color.white)
        mUnSelectColor = AppContext.get().resources.getColor(com.zxhl.cms.R.color.color_333333)
    }

    fun setPosition(pos: Int) {
        position = pos
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(holder: ViewGroup, p1: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(holder.context)
                .inflate(R.layout.layout_item_roomtype, holder, false)
        )
    }

    override fun getItemCount(): Int {
        val size = mList.size
        if (size > 0) {
            return size - 1
        } else {
            return size
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, pos: Int) {
        val data = mList[pos+1]
        holder.id_tv_name.text = data.name
        if (pos == position) {
            holder.id_tv_name.setBackgroundResource(R.drawable.shape_7a9cf_ae71dd_r25)
            holder.id_tv_name.setTextColor(mSelectColor)
            listener.onItemClick(holder.id_tv_name, pos, data)
        } else {
            holder.id_tv_name.setBackgroundResource(R.drawable.shape_efefef_r25)
            holder.id_tv_name.setTextColor(mUnSelectColor)
        }
    }

    inner class ViewHolder : RecyclerView.ViewHolder {


        var id_tv_name: TextView


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

            id_tv_name = itemView.findViewById(R.id.id_tv_type_name)
            id_tv_name.setOnClickListener {
                val pos = adapterPosition
                if (pos >= 0 && pos < mList.size) {
                    setPosition(pos)
                    notifyDataSetChanged()
                }
            }
        }
    }
}