package com.kaixed.kchat.view.widget

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import com.kaixed.kchat.R
import com.kaixed.kchat.databinding.ViewTitleBarBinding

/**
 * @Author: kaixed
 * @Date: 2024/10/10 22:13
 * @Description: 自定义标题栏控件
 */
class CustomTitleBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private var binding: ViewTitleBarBinding =
        ViewTitleBarBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        attrs?.let {
            val typedArray = context.obtainStyledAttributes(it, R.styleable.CustomTitleBar, 0, 0)
            val titleName = typedArray.getString(R.styleable.CustomTitleBar_titleName) ?: ""
            val titleIcon = typedArray.getResourceId(R.styleable.CustomTitleBar_titleIcon, -1)

            if (titleIcon != -1) {
                binding.ivSetting.visibility = View.VISIBLE
                binding.ivSetting.setImageResource(titleIcon)
            } else {
                binding.ivSetting.visibility = View.GONE
            }

            if (titleName.isNotEmpty()) {
                binding.tvTitleName.visibility = View.VISIBLE
                binding.tvTitleName.text = titleName
            } else {
                binding.tvTitleName.visibility = View.GONE
            }

            typedArray.recycle()

        }

        binding.ivBack.setOnClickListener {
            (context as? Activity)?.finish()
        }
    }

    fun setOnSettingClickListener(listener: OnClickListener?) {
        binding.ivSetting.setOnClickListener(listener)
    }

}