package com.gh.gamecenter.help

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.ItemQaFeedbackReasonBinding
import com.lightgame.adapter.BaseRecyclerAdapter

class QaFeedbackReasonAdapter(
    context: Context,
    val reasonList: ArrayList<String>,
    val callback: (String) -> Unit
) :
    BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    private var mSelectPosition = -1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return QaFeedbackReasonViewHolder(
            ItemQaFeedbackReasonBinding.inflate(LayoutInflater.from(mContext), parent, false)
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val reason = reasonList[position]
        if (holder is QaFeedbackReasonViewHolder) {
            holder.binding.reasonTv.apply {
                text = reason
                background =
                    if (mSelectPosition == position) R.drawable.qa_feedback_rg_button_checked.toDrawable(mContext)
                    else R.drawable.qa_feedback_rg_button_normal.toDrawable(mContext)
                setTextColor(
                    if (mSelectPosition == position) R.color.theme_font.toColor(mContext)
                    else R.color.text_title.toColor(mContext)
                )
            }
            holder.binding.reasonTv.setOnClickListener {
                mSelectPosition = position
                callback.invoke(holder.binding.reasonTv.text.toString())
                notifyDataSetChanged()
            }
        }
    }

    override fun getItemCount(): Int = reasonList.size

    fun getSelectReason(): String {
        return if (mSelectPosition >= 0) reasonList[mSelectPosition] else ""
    }

}

class QaFeedbackReasonViewHolder(val binding: ItemQaFeedbackReasonBinding) :
    RecyclerView.ViewHolder(binding.root)
