package com.dlc.chemical.ui.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.dlc.chemical.databinding.WidgetTitleBarBinding
import com.dlc.chemical.sp.ConfLogoSp

/**
 * info
 *
 * @author ReiChin_
 */
class TitleBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
        ConstraintLayout(context, attrs, defStyleAttr) {

    private val viewBinding =
        WidgetTitleBarBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        viewBinding.home.setOnClickListener { mTitleListener?.onHomeViewClicked(it) }
        viewBinding.back.setOnClickListener { mTitleListener?.onBackViewClicked(it) }
    }

    fun setTitleInfoFromSp() {
        ConfLogoSp.loadConfLogo(context)?.let { conf ->
            Glide.with(this)
                .load(conf.rectangleLogo)
                .into(viewBinding.sysLogo)
        }
    }

    private var mTitleListener: TitleListener? = null

    fun setTitleListener(listener: TitleListener) {
        this.mTitleListener = listener
    }

    interface TitleListener {
        fun onHomeViewClicked(view: View)
        fun onBackViewClicked(view: View)
    }

}