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

import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.view.children
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.module_base.R
import com.lukouguoji.module_base.databinding.ItemSearchBinding
import com.lukouguoji.module_base.ktx.getActivity
import com.lukouguoji.module_base.ktx.loge
import dev.utils.app.SizeUtils
import dev.utils.app.ViewUtils
import dev.utils.app.info.KeyValue

object SearchLayoutHelper {

    private const val maxWeight = 6

    fun createSearchLayoutBar(
        ll: LinearLayout,
        list: List<SearchLayoutModel>,
        iconList: List<SearchBarIcon> = listOf(),
    ) {
        val inflater = LayoutInflater.from(ll.context)

        // 空布局数量
        val emptyCount = maxWeight - list.size - 1

        // 添加搜索框
        list.forEach {
            val view = it.getLayout(inflater)
            ll.addView(view)
            ViewUtils.setWeight(view, 1f)
        }

        // 添加图标
        val llIcons = inflater.inflate(R.layout.ll_search_bar_icons, null, false) as LinearLayout
        ll.addView(llIcons)
        ViewUtils.setWeight(llIcons, 1f)
        iconList.forEach {
            val view = it.getView(inflater)
            llIcons.addView(view)
            ViewUtils.setMarginLeft(view, SizeUtils.dp2px(20f))
        }

        // 添加空闲布局
        if (emptyCount > 0) {
            for (i in 0 until emptyCount) {
                val view = inflater.inflate(R.layout.ll_search_bar_icons, null, false)
                ll.addView(view)
                ViewUtils.setWeight(view, 1f)
            }
        }

        ll.children.forEach {
            ViewUtils.setWeight(it, 1f)
        }

        ll.invalidate()
    }
}

class SearchLayoutModel(
    val value: MutableLiveData<String>,
    val hint: String = "",
    val type: SearchLayoutType = SearchLayoutType.INPUT,
    val list: MutableLiveData<List<KeyValue>> = MutableLiveData(listOf()),
    val required: Boolean = false,
    val icon: Any? = null,
    val enable: Boolean = true,
    val iconClickCallBack: () -> Unit = { loge("点击了") },
) {

    var binding: ItemSearchBinding? = null

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

}

class SearchBarIcon(
    private val icon: Int,
    private val clickCallBack: () -> Unit,
) {

    fun getView(inflate: LayoutInflater): View {
        val view = inflate.inflate(R.layout.iv_search_bar_icon, null, false) as ImageView
        view.setImageResource(icon)
        view.setOnClickListener { clickCallBack.invoke() }
        ViewUtils.setWidthHeight(view, SizeUtils.dp2px(40f), SizeUtils.dp2px(40f))
        return view
    }
}