package kr.co.vividnext.sodalive.mypage.service_center

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import kr.co.vividnext.sodalive.R

class FaqCategoryAdapter(
    private val context: Context
) : BaseAdapter() {

    val items = mutableListOf<String>()
    private var selectedCategory = ""

    override fun getCount(): Int {
        return items.count()
    }

    override fun getItem(position: Int): Any {
        return items[position]
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    override fun getView(position: Int, view: View?, parent: ViewGroup?): View {
        var convertView = view
        val viewHolder: ViewHolder

        if (convertView == null) {
            convertView =
                LayoutInflater.from(context).inflate(R.layout.item_faq_category, parent, false)
            viewHolder = ViewHolder()
            viewHolder.tvCategory = convertView!!.findViewById(R.id.tv_category)
            convertView.tag = viewHolder
        } else {
            viewHolder = convertView.tag as ViewHolder
        }

        val item = items[position]
        viewHolder.tvCategory.text = item
        viewHolder.tvCategory.setBackgroundResource(
            if (item == selectedCategory) {
                R.drawable.bg_round_corner_4_7_9970ff
            } else {
                R.drawable.bg_round_corner_4_7_222222
            }
        )

        return convertView
    }

    fun selectCategory(position: Int): String {
        selectedCategory = items[position]
        notifyDataSetChanged()

        return selectedCategory
    }

    inner class ViewHolder {
        lateinit var tvCategory: TextView
    }
}
