package com.lukouguoji.module_base.adapter

import android.view.View
import android.widget.EditText
import androidx.databinding.BindingAdapter
import com.lukouguoji.module_base.interfaces.IOnFocusChangeListener

@BindingAdapter("visible")
fun visible(view: View, visible: Any?) {
    if (visible == null) {
        view.visibility = View.GONE
        return
    }
    when (visible) {
        is Boolean ->
            view.visibility = if (visible) View.VISIBLE else View.GONE

        is Int ->
            view.visibility = if (visible == 0) View.GONE else View.VISIBLE

        is String ->
            view.visibility = if (visible.isEmpty()) View.GONE else View.VISIBLE

        else -> {
            view.visibility = View.VISIBLE
        }
    }
}

@BindingAdapter("setFocusChangedListener")
fun setOnFocusChangeListener(et: EditText, listener: IOnFocusChangeListener) {
    et.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
        listener.onFocusChange(hasFocus)
    }
}

@BindingAdapter("setFocusLoseListener")
fun setOnFocusLoseListener(et: EditText, listener: () -> Unit) {
    et.setOnFocusChangeListener { v, hasFocus ->
        if (!hasFocus) {
            listener.invoke()
        }
    }
}