package com.kaixed.kchat.view.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import com.kaixed.kchat.R
import com.kaixed.kchat.databinding.PreferenceCustomLayoutBinding

class CustomPreference @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : Preference(context, attrs, defStyleAttr) {

    private var isShowTopDivider = false
    private var isShowBottomDivider = false

    init {
        layoutResource = R.layout.preference_custom_layout

        attrs?.let {
            val typedArray = context.obtainStyledAttributes(it, R.styleable.CustomPreference, 0, 0)
            val isShowTopDivider =
                typedArray.getBoolean(R.styleable.CustomPreference_isShowTopDivider, false)
            val isShowBottomDivider =
                typedArray.getBoolean(R.styleable.CustomPreference_isShowBottomDivider, false)
            typedArray.recycle()

            this.isShowTopDivider = isShowTopDivider
            this.isShowBottomDivider = isShowBottomDivider
        }
    }


    override fun onBindViewHolder(holder: PreferenceViewHolder) {
        super.onBindViewHolder(holder)
        holder.isDividerAllowedAbove = false
        holder.isDividerAllowedBelow = false

        val binding = PreferenceCustomLayoutBinding.bind(holder.itemView)


        binding.tvItemName.text = title

        binding.decorationTop.visibility = if (isShowTopDivider) {
            View.VISIBLE
        } else {
            View.GONE
        }

        binding.decorationBottom.visibility = if (isShowBottomDivider) {
            View.VISIBLE
        } else {
            View.GONE
        }
    }


}
