package com.kaixed.kchat.view.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceViewHolder
import com.kaixed.kchat.R
import com.kaixed.kchat.databinding.PreferenceCategoryCustomLayoutBinding

/**
 * @Author: kaixed
 * @Date: 2024/10/10 14:45
 * @Description: TODO
 */
class CustomPreferenceCategory @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : PreferenceCategory(context, attrs, defStyleAttr) {

    private var title = ""

    init {
        layoutResource = R.layout.preference_category_custom_layout

        attrs?.let {
            val typedArray =
                context.obtainStyledAttributes(it, R.styleable.CustomPreferenceCategory, 0, 0)
            val title = typedArray.getString(R.styleable.CustomPreferenceCategory_itemTitle)
            typedArray.recycle()
            if (title != null) {
                Log.d("haha", title)
                this.title = title
            }
        }
    }

    override fun onBindViewHolder(holder: PreferenceViewHolder) {
        super.onBindViewHolder(holder)

        val binding = PreferenceCategoryCustomLayoutBinding.bind(holder.itemView)

        Log.d("haha1", title)

        if (title.isNotEmpty()) {
            binding.tvTitle.text = title
            binding.decoration.visibility = View.GONE
            binding.tvTitle.visibility = View.VISIBLE
        } else {
            binding.decoration.visibility = View.VISIBLE
            binding.tvTitle.visibility = View.GONE
        }
    }
}