package com.lukouguoji.module_base.ui.weight.phone

import android.view.View
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import androidx.databinding.InverseBindingListener
import dev.utils.app.info.KeyValue

/**
 * 5.5 寸手持端公共组件的 DataBinding 适配器集合
 *
 * 所有属性均为无命名空间写法（与平板端 PadSearchLayout / PadDataLayoutNew 的用法一致）。
 * 同名属性（hint / value / title / list 等）按控件类型解析，与平板端组件互不冲突。
 */

///////////////////////////////////////////////////////////////////////////
// PhoneSearchBar
///////////////////////////////////////////////////////////////////////////

@BindingAdapter("hint", "showScan", "showFilter", "upperCase", requireAll = false)
fun setPhoneSearchBarAttrs(
    bar: PhoneSearchBar,
    hint: String?,
    showScan: Boolean?,
    showFilter: Boolean?,
    upperCase: Boolean?
) {
    hint?.let { bar.hint = it }
    showScan?.let { bar.showScan = it }
    showFilter?.let { bar.showFilter = it }
    upperCase?.let { bar.upperCase = it }
}

@BindingAdapter("value", requireAll = false)
fun setPhoneSearchBarValue(bar: PhoneSearchBar, value: String?) {
    value?.let { bar.value = it }
}

@InverseBindingAdapter(attribute = "value", event = "valueAttrChanged")
fun getPhoneSearchBarValue(bar: PhoneSearchBar): String = bar.value

@BindingAdapter("valueAttrChanged", requireAll = false)
fun setPhoneSearchBarValueAttrChanged(bar: PhoneSearchBar, listener: InverseBindingListener) {
    bar.onChangeListener = listener
}

@BindingAdapter("setOnScanClickListener", requireAll = false)
fun setPhoneSearchBarScanClick(bar: PhoneSearchBar, listener: View.OnClickListener?) {
    bar.setOnScanClick(listener)
}

@BindingAdapter("setOnFilterClickListener", requireAll = false)
fun setPhoneSearchBarFilterClick(bar: PhoneSearchBar, listener: View.OnClickListener?) {
    bar.setOnFilterClick(listener)
}

@BindingAdapter("setRefreshCallBack", requireAll = false)
fun setPhoneSearchBarRefreshCallBack(bar: PhoneSearchBar, callBack: (() -> Unit)?) {
    bar.searchCallBack = callBack
}

///////////////////////////////////////////////////////////////////////////
// PhoneStatusTab
///////////////////////////////////////////////////////////////////////////

@BindingAdapter("tabs", requireAll = false)
fun setPhoneStatusTabTabs(tab: PhoneStatusTab, tabs: List<KeyValue>?) {
    tabs?.let { tab.tabs = it }
}

@BindingAdapter("counts", requireAll = false)
fun setPhoneStatusTabCounts(tab: PhoneStatusTab, counts: List<String>?) {
    tab.counts = counts ?: emptyList()
}

@BindingAdapter("value", requireAll = false)
fun setPhoneStatusTabValue(tab: PhoneStatusTab, value: String?) {
    value?.let { tab.value = it }
}

@InverseBindingAdapter(attribute = "value", event = "valueAttrChanged")
fun getPhoneStatusTabValue(tab: PhoneStatusTab): String = tab.value

@BindingAdapter("valueAttrChanged", requireAll = false)
fun setPhoneStatusTabValueAttrChanged(tab: PhoneStatusTab, listener: InverseBindingListener) {
    tab.onChangeListener = listener
}

@BindingAdapter("setOnTabChanged", requireAll = false)
fun setPhoneStatusTabChanged(tab: PhoneStatusTab, callBack: (() -> Unit)?) {
    tab.tabChangedCallBack = callBack
}

///////////////////////////////////////////////////////////////////////////
// PhoneDataLayout
///////////////////////////////////////////////////////////////////////////

@BindingAdapter("title", "hint", "type", "required", "enable", requireAll = false)
fun setPhoneDataLayoutAttrs(
    layout: PhoneDataLayout,
    title: String?,
    hint: String?,
    type: PhoneDataLayoutType?,
    required: Boolean?,
    enable: Boolean?
) {
    // type 必须先于 value/hint 生效，否则控件形态与取值分支不匹配
    type?.let { layout.type = it }
    title?.let { layout.title = it }
    hint?.let { layout.hint = it }
    required?.let { layout.required = it }
    enable?.let { layout.enable = it }
}

@BindingAdapter("value", requireAll = false)
fun setPhoneDataLayoutValue(layout: PhoneDataLayout, value: String?) {
    value?.let { layout.value = it }
}

@InverseBindingAdapter(attribute = "value", event = "valueAttrChanged")
fun getPhoneDataLayoutValue(layout: PhoneDataLayout): String = layout.value

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

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

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

///////////////////////////////////////////////////////////////////////////
// PhoneFilterPanel
///////////////////////////////////////////////////////////////////////////

@BindingAdapter("title", "panelVisible", requireAll = false)
fun setPhoneFilterPanelAttrs(panel: PhoneFilterPanel, title: String?, panelVisible: Boolean?) {
    title?.let { panel.title = it }
    panelVisible?.let { panel.panelVisible = it }
}

@BindingAdapter("setOnResetClick", requireAll = false)
fun setPhoneFilterPanelResetClick(panel: PhoneFilterPanel, listener: View.OnClickListener?) {
    panel.tvReset.setOnClickListener(listener)
}

@BindingAdapter("setOnConfirmClick", requireAll = false)
fun setPhoneFilterPanelConfirmClick(panel: PhoneFilterPanel, listener: View.OnClickListener?) {
    panel.tvConfirm.setOnClickListener(listener)
}

@BindingAdapter("setOnDismissClick", requireAll = false)
fun setPhoneFilterPanelDismissClick(panel: PhoneFilterPanel, listener: View.OnClickListener?) {
    panel.setOnDismiss(listener)
}

///////////////////////////////////////////////////////////////////////////
// PhoneBottomBar
///////////////////////////////////////////////////////////////////////////

@BindingAdapter("allChecked", "countText", "actionText", requireAll = false)
fun setPhoneBottomBarAttrs(
    bar: PhoneBottomBar,
    allChecked: Boolean?,
    countText: String?,
    actionText: String?
) {
    allChecked?.let { bar.allChecked = it }
    countText?.let { bar.countText = it }
    actionText?.let { bar.actionText = it }
}

@BindingAdapter("setOnAllCheckClick", requireAll = false)
fun setPhoneBottomBarAllCheckClick(bar: PhoneBottomBar, listener: View.OnClickListener?) {
    bar.llCheckAll.setOnClickListener(listener)
}

@BindingAdapter("setOnActionClick", requireAll = false)
fun setPhoneBottomBarActionClick(bar: PhoneBottomBar, listener: View.OnClickListener?) {
    bar.tvAction.setOnClickListener(listener)
}

///////////////////////////////////////////////////////////////////////////
// PhoneStatBox
///////////////////////////////////////////////////////////////////////////

@BindingAdapter(
    "label1", "value1", "label2", "value2",
    "label3", "value3", "label4", "value4",
    requireAll = false
)
fun setPhoneStatBoxAttrs(
    box: PhoneStatBox,
    label1: String?, value1: String?,
    label2: String?, value2: String?,
    label3: String?, value3: String?,
    label4: String?, value4: String?
) {
    box.setLabel(0, label1)
    box.setValue(0, value1)
    box.setLabel(1, label2)
    box.setValue(1, value2)
    box.setLabel(2, label3)
    box.setValue(2, value3)
    box.setLabel(3, label4)
    box.setValue(3, value4)
}

///////////////////////////////////////////////////////////////////////////
// PhoneKvItem
///////////////////////////////////////////////////////////////////////////

@BindingAdapter("title", "value", "tag", "tagColor", requireAll = false)
fun setPhoneKvItemAttrs(
    item: PhoneKvItem,
    title: String?,
    value: String?,
    tag: Boolean?,
    tagColor: String?
) {
    title?.let { item.title = it }
    // tag/tagColor 需先于 value 生效，确保样式切换后再填内容
    tagColor?.let { item.tagColor = it }
    tag?.let { item.tag = it }
    item.value = value.orEmpty()
}

///////////////////////////////////////////////////////////////////////////
// PhoneFormRow
///////////////////////////////////////////////////////////////////////////

@BindingAdapter("title", "hint", "type", "required", "enable", "numeric", requireAll = false)
fun setPhoneFormRowAttrs(
    row: PhoneFormRow,
    title: String?,
    hint: String?,
    type: PhoneFormRowType?,
    required: Boolean?,
    enable: Boolean?,
    numeric: Boolean?
) {
    // type 先于其余属性生效，确保 value/hint 写进正确的子控件
    type?.let { row.type = it }
    title?.let { row.title = it }
    hint?.let { row.hint = it }
    required?.let { row.required = it }
    enable?.let { row.enable = it }
    numeric?.let { row.numeric = it }
}

@BindingAdapter("value", requireAll = false)
fun setPhoneFormRowValue(row: PhoneFormRow, value: String?) {
    if (row.value != value.orEmpty()) row.value = value.orEmpty()
}

@InverseBindingAdapter(attribute = "value", event = "valueAttrChanged")
fun getPhoneFormRowValue(row: PhoneFormRow): String = row.value

@BindingAdapter("valueAttrChanged", requireAll = false)
fun setPhoneFormRowValueAttrChanged(row: PhoneFormRow, listener: InverseBindingListener) {
    row.onChangeListener = listener
}

@BindingAdapter("list", requireAll = false)
fun setPhoneFormRowList(row: PhoneFormRow, list: List<KeyValue>?) {
    row.list = list ?: emptyList()
}

@BindingAdapter("setRefreshCallBack", requireAll = false)
fun setPhoneFormRowRefreshCallBack(row: PhoneFormRow, callBack: (() -> Unit)?) {
    row.refreshCallBack = callBack
}

///////////////////////////////////////////////////////////////////////////
// PhoneBottomBar（追加：危险操作样式）
///////////////////////////////////////////////////////////////////////////

@BindingAdapter("actionDanger", requireAll = false)
fun setPhoneBottomBarActionDanger(bar: PhoneBottomBar, danger: Boolean?) {
    danger?.let { bar.actionDanger = it }
}
