package com.lukouguoji.module_base.ui.weight.search.layout

import android.graphics.drawable.Drawable
import android.text.InputFilter
import android.text.InputType
import android.view.View
import androidx.core.widget.doOnTextChanged
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import androidx.databinding.InverseBindingListener
import dev.utils.app.EditTextUtils
import dev.utils.app.info.KeyValue


@BindingAdapter(
    "type",
    "hint",
    "required",
    "icon",
    requireAll = false
)
fun setSearchLayoutData(
    layout: PadSearchLayout,
    type: SearchLayoutType?,
    hint: String?,
    required: Boolean?,
    icon: Any?,
) {
    type?.let {
        layout.type = type
    }
    required?.let {
        layout.required = required
    }
    hint?.let {
        layout.hint = hint
    }
    icon?.let {
        layout.icon = icon
    }
}

@BindingAdapter(
    "value",
    requireAll = false
)
fun setSearchLayoutDataValue(
    layout: PadSearchLayout,
    value: String?,
) {
    value?.let {
        layout.value = value
    }
}

@BindingAdapter(
    "enable",
    requireAll = false
)
fun setSearchLayoutDataEnable(
    layout: PadSearchLayout,
    enable: Boolean?,
) {
    enable?.let {
        layout.enable = enable
    }
}

@BindingAdapter(
    "list",
    requireAll = false
)
fun setSearchLayoutDataList(
    layout: PadSearchLayout,
    list: List<KeyValue>?,
) {
    list?.let {
        layout.list = list
    }
}

@BindingAdapter(
    "bgDrawable",
    requireAll = false
)
fun setSearchLayoutDataBackground(
    layout: PadSearchLayout,
    bgDrawable: Drawable,
) {
    layout.ll.setBackgroundDrawable(bgDrawable)
}

@InverseBindingAdapter(attribute = "value", event = "valueAttrChanged")
fun getDataLayoutValue(layout: PadSearchLayout): String {
    return layout.value
}

@BindingAdapter("valueAttrChanged", requireAll = false)
fun setDataLayoutValueAttrChanged(layout: PadSearchLayout, listener: InverseBindingListener) {
    layout.onChangeListener = listener
}

@BindingAdapter("setOnIconClickListener", requireAll = false)
fun setOnIconClickListener(layout: PadSearchLayout, listener: View.OnClickListener?) {
    layout.iv.setOnClickListener(listener)
}

@BindingAdapter("setRefreshCallBack", requireAll = false)
fun setRefreshCallBack(layout: PadSearchLayout, listener: (() -> Unit)?) {
    layout.refreshCallBack = listener
}

@BindingAdapter("setSearchListRefresh")
fun setListRefreshCallBack(layout: PadSearchLayout, listener: (() -> Unit)?) {
    layout.listRefreshCallBack = listener
}

@BindingAdapter("setTextAllCaps", requireAll = false)
fun setTextAllCaps(layout: PadSearchLayout, textAllCaps: Boolean) {
    if (textAllCaps) {
        layout.et.filters = arrayOf<InputFilter>(InputFilter.AllCaps())
    } else {
        layout.et.filters = emptyArray<InputFilter>()
    }
}

@BindingAdapter("setInputWaybill", requireAll = false)
fun setInputWaybill(layout: PadSearchLayout, isWaybill: Boolean) {
    if (isWaybill) {
        EditTextUtils.setMaxLength(layout.et, 11)
        EditTextUtils.setInputType(layout.et, InputType.TYPE_CLASS_NUMBER)
        layout.et.doOnTextChanged { text, _, _, _ ->
            if (text.toString().length == 11) {
                layout.refreshCallBack?.invoke()
            }
            if (text.toString().length in 4..8){
                layout.listRefreshCallBack?.invoke()
            }
        }
    }
}

/**
 * 设置搜索监听器（回车键或搜索按钮触发）
 */
@BindingAdapter("setOnSearchListener", requireAll = false)
fun setSearchLayoutNewOnSearchListener(layout: PadSearchLayoutNew, listener: (() -> Unit)?) {
    layout.searchCallBack = listener
}

/**
 * 设置搜索图标点击为搜索功能
 */
@BindingAdapter("setSearchIconClickListener", requireAll = false)
fun setSearchLayoutNewSearchIconClickListener(layout: PadSearchLayoutNew, listener: (() -> Unit)?) {
    layout.iv.setOnClickListener {
        listener?.invoke()
    }
}

///////////////////////////////////////////////////////////////////////////
// PadSearchLayoutNew 的绑定适配器
///////////////////////////////////////////////////////////////////////////

@BindingAdapter(
    "type",
    "hint",
    "required",
    "icon",
    requireAll = false
)
fun setSearchLayoutNewData(
    layout: PadSearchLayoutNew,
    type: SearchLayoutType?,
    hint: String?,
    required: Boolean?,
    icon: Any?,
) {
    type?.let {
        layout.type = type
    }
    required?.let {
        layout.required = required
    }
    hint?.let {
        layout.hint = hint
    }
    icon?.let {
        layout.icon = icon
    }
}

@BindingAdapter(
    "value",
    requireAll = false
)
fun setSearchLayoutNewDataValue(
    layout: PadSearchLayoutNew,
    value: String?,
) {
    value?.let {
        layout.value = value
    }
}

@BindingAdapter(
    "enable",
    requireAll = false
)
fun setSearchLayoutNewDataEnable(
    layout: PadSearchLayoutNew,
    enable: Boolean?,
) {
    enable?.let {
        layout.enable = enable
    }
}

@BindingAdapter(
    "list",
    requireAll = false
)
fun setSearchLayoutNewDataList(
    layout: PadSearchLayoutNew,
    list: List<KeyValue>?,
) {
    list?.let {
        layout.list = list
    }
}

@BindingAdapter(
    "bgDrawable",
    requireAll = false
)
fun setSearchLayoutNewDataBackground(
    layout: PadSearchLayoutNew,
    bgDrawable: Drawable,
) {
    layout.ll.setBackgroundDrawable(bgDrawable)
}

@InverseBindingAdapter(attribute = "value", event = "valueAttrChanged")
fun getSearchLayoutNewValue(layout: PadSearchLayoutNew): String {
    return layout.value
}

@BindingAdapter("valueAttrChanged", requireAll = false)
fun setSearchLayoutNewValueAttrChanged(layout: PadSearchLayoutNew, listener: InverseBindingListener) {
    layout.onChangeListener = listener
}

@BindingAdapter("setOnIconClickListener", requireAll = false)
fun setSearchLayoutNewOnIconClickListener(layout: PadSearchLayoutNew, listener: View.OnClickListener?) {
    layout.iv.setOnClickListener(listener)
}

@BindingAdapter("setRefreshCallBack", requireAll = false)
fun setSearchLayoutNewRefreshCallBack(layout: PadSearchLayoutNew, listener: (() -> Unit)?) {
    layout.refreshCallBack = listener
}

@BindingAdapter("setSearchListRefresh")
fun setSearchLayoutNewListRefreshCallBack(layout: PadSearchLayoutNew, listener: (() -> Unit)?) {
    layout.listRefreshCallBack = listener
}

@BindingAdapter("setTextAllCaps", requireAll = false)
fun setSearchLayoutNewTextAllCaps(layout: PadSearchLayoutNew, textAllCaps: Boolean) {
    if (textAllCaps) {
        layout.et.filters = arrayOf<InputFilter>(InputFilter.AllCaps())
    } else {
        layout.et.filters = emptyArray<InputFilter>()
    }
}

@BindingAdapter("setInputWaybill", requireAll = false)
fun setSearchLayoutNewInputWaybill(layout: PadSearchLayoutNew, isWaybill: Boolean) {
    if (isWaybill) {
        EditTextUtils.setMaxLength(layout.et, 11)
        EditTextUtils.setInputType(layout.et, InputType.TYPE_CLASS_NUMBER)
        layout.et.doOnTextChanged { text, _, _, _ ->
            if (text.toString().length == 11) {
                layout.refreshCallBack?.invoke()
            }
            if (text.toString().length in 4..8){
                layout.listRefreshCallBack?.invoke()
            }
        }
    }
}