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

import android.text.InputFilter
import android.text.InputType
import android.view.Gravity
import android.view.View
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import androidx.databinding.InverseBindingListener
import com.lukouguoji.module_base.ktx.loge
import com.lukouguoji.module_base.ui.weight.search.layout.PadSearchLayout
import dev.utils.app.EditTextUtils
import dev.utils.app.SizeUtils
import dev.utils.app.ViewUtils
import dev.utils.app.info.KeyValue

@BindingAdapter(
    "type",
    "title",
    "titleLength",
    "hint",
    "required",
    "icon",
    requireAll = false
)
fun setDataLayoutData(
    dataLayout: PadDataLayout,
    type: DataLayoutType?,
    title: String?,
    titleLength: Int?,
    hint: String?,
    required: Boolean?,
    icon: Any?,
) {
    type?.let {
        dataLayout.type = it
    }
    title?.let {
        dataLayout.tvTitle.text = title
    }
    titleLength?.let {
        dataLayout.titleLength = titleLength
    }
    required?.let {
        dataLayout.required = it
    }
    hint?.let {
        dataLayout.hint = hint
    }
    dataLayout.icon = icon
}


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

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

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

@BindingAdapter(
    "inputHeight",
    requireAll = false
)
fun setSearchLayoutInputHeight(
    layout: PadDataLayout,
    inputHeight: Int?,
) {
    inputHeight?.let {
        ViewUtils.setHeight(layout.llContainer, SizeUtils.dp2px(it.toFloat()))
        layout.et.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE)
        layout.et.gravity = Gravity.TOP
        layout.et.isSingleLine = false
        layout.et.isHorizontalScrollBarEnabled = false
        ViewUtils.setPadding(layout.et, SizeUtils.dp2px(10f))
    }
}

/**
 * 设置最大长度
 * 仅对Input类型有效
 */
@BindingAdapter(
    "maxLength",
    requireAll = false
)
fun setSearchLayoutMaxLength(
    layout: PadDataLayout,
    maxLength: Int?,
) {
    maxLength?.let {
        EditTextUtils.setMaxLength(layout.et, maxLength)
    }
}

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

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

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

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

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

// ========== PadDataLayoutNew BindingAdapters ==========

@BindingAdapter(
    "type",
    "title",
    "titleLength",
    "hint",
    "required",
    "icon",
    requireAll = false
)
fun setDataLayoutDataNew(
    dataLayout: PadDataLayoutNew,
    type: DataLayoutType?,
    title: String?,
    titleLength: Int?,
    hint: String?,
    required: Boolean?,
    icon: Any?,
) {
    type?.let {
        dataLayout.type = it
    }
    title?.let {
        dataLayout.tvTitle.text = title
    }
    titleLength?.let {
        dataLayout.titleLength = titleLength
    }
    required?.let {
        dataLayout.required = it
    }
    hint?.let {
        dataLayout.hint = hint
    }
    dataLayout.icon = icon
}


@BindingAdapter(
    "value",
    requireAll = false
)
fun setSearchLayoutDataValueNew(
    layout: PadDataLayoutNew,
    value: String?,
) {
    value?.let {
        layout.value = value
    }
}
@BindingAdapter(
    "enable",
    requireAll = false
)
fun setSearchLayoutDataEnableNew(
    layout: PadDataLayoutNew,
    enable: Boolean?,
) {
    enable?.let {
        layout.enable = enable
    }
}

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

@BindingAdapter(
    "inputHeight",
    requireAll = false
)
fun setSearchLayoutInputHeightNew(
    layout: PadDataLayoutNew,
    inputHeight: Int?,
) {
    inputHeight?.let {
        ViewUtils.setHeight(layout.llContainer, SizeUtils.dp2px(it.toFloat()))
        layout.et.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE)
        layout.et.gravity = Gravity.TOP
        layout.et.isSingleLine = false
        layout.et.isHorizontalScrollBarEnabled = false
        ViewUtils.setPadding(layout.et, SizeUtils.dp2px(10f))
    }
}

/**
 * 设置最大长度
 * 仅对Input类型有效
 */
@BindingAdapter(
    "maxLength",
    requireAll = false
)
fun setSearchLayoutMaxLengthNew(
    layout: PadDataLayoutNew,
    maxLength: Int?,
) {
    maxLength?.let {
        EditTextUtils.setMaxLength(layout.et, maxLength)
    }
}

@InverseBindingAdapter(attribute = "value", event = "valueAttrChanged")
fun getDataLayoutValueNew(dataLayout: PadDataLayoutNew): String {
    return dataLayout.value
}

@BindingAdapter("valueAttrChanged", requireAll = false)
fun setDataLayoutValueAttrChangedNew(dataLayout: PadDataLayoutNew, listener: InverseBindingListener) {
    dataLayout.onChangeListener = listener
}

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

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

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