package com.gh.gamecenter.subject.tile

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
import com.lightgame.adapter.BaseRecyclerAdapter

class SubjectTypeListAdapter(
    context: Context,
    private val mItemClickListener: (type: String) -> Unit,
    private val mGameType: List<String>
) : BaseRecyclerAdapter<SubjectTypeListAdapter.SubjectTypeViewHolder>(context) {

    private var mCurType = "全部"

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubjectTypeViewHolder {
        return SubjectTypeViewHolder(mLayoutInflater.inflate(R.layout.item_filter_size, parent, false))
    }

    override fun onBindViewHolder(holder: SubjectTypeViewHolder, position: Int) {
        holder.itemView.layoutParams = holder.itemView.layoutParams.apply {
            width = ViewGroup.LayoutParams.MATCH_PARENT
        }
        holder.type.text = mGameType[position]

        if (!TextUtils.isEmpty(mCurType) && mCurType == mGameType[position]) {
            holder.type.background = ContextCompat.getDrawable(mContext, R.drawable.bg_tag_text)
            holder.type.setTextColor(Color.WHITE)
        } else {
            holder.type.background = null
            holder.type.setTextColor(ContextCompat.getColor(mContext, R.color.text_757575))
        }

        holder.type.setOnClickListener {
            if (holder.adapterPosition == -1) return@setOnClickListener

            mCurType = mGameType[holder.adapterPosition]
            mItemClickListener.invoke(mGameType[holder.adapterPosition])
            notifyDataSetChanged()
        }
    }

    override fun getItemCount(): Int {
        return mGameType.size
    }

    inner class SubjectTypeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val type by lazy { itemView.findViewById<TextView>(R.id.size_tv) }
    }
}