package com.lukouguoji.module_base.model

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.module_base.R
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.databinding.DialogFilterBinding
import com.lukouguoji.module_base.databinding.ItemFilterDialogBinding
import com.lukouguoji.module_base.ktx.getActivity
import com.lukouguoji.module_base.ui.weight.search.layout.SearchLayoutType
import dev.utils.app.info.KeyValue

class PadFilterDialogModel(
    private val context: Context,
    val list: List<PadFilterModel>,
    private val callBack: () -> Unit
) : BaseDialogModel<DialogFilterBinding>(DIALOG_TYPE_FULL) {

    private val inflate = LayoutInflater.from(context)

    override fun layoutId(): Int {
        return R.layout.dialog_filter
    }

    override fun onDialogCreated(context: Context) {
        binding.model = this

        binding.root.findViewById<View>(R.id.tool_back).setOnClickListener { dismiss() }
        binding.root.findViewById<TextView>(R.id.title_name).text = "搜索"

        val titleLength = list.maxOfOrNull { it.title.length } ?: 1
        list.forEach {
            it.titleLength = titleLength
            binding.ll.addView(it.getLayout(inflate))
        }
    }

    fun onResetClick() {
        list.forEach {
            it.binding?.psl?.reset()
        }
    }

    fun onConfirmClick() {
        dismiss()
        callBack.invoke()
    }

}

class PadFilterModel(
    val data: MutableLiveData<String>,
    val title: String = "",
    val hint: String = "",
    val type: SearchLayoutType = SearchLayoutType.INPUT,
    val list: MutableLiveData<List<KeyValue>> = MutableLiveData(listOf()),
    // 强制大写,仅Input有效
    val forcedCap: Boolean = false
) {

    var titleLength = 1

    var binding: ItemFilterDialogBinding? = null

    fun getLayout(inflate: LayoutInflater): View {
        if (binding == null) {
            binding = ItemFilterDialogBinding.inflate(inflate)
            binding!!.lifecycleOwner = inflate.context.getActivity() as LifecycleOwner
            binding!!.model = this
        }
        return binding!!.root
    }
}
