package com.adealink.weparty.commonui.widget

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.base.fastLazy
import com.adealink.frame.commonui.widget.binder.CommonBottomListItemBinder

import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.weparty.R
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.databinding.DialogCommonBottomListBinding

class CommonBottomListDialog : BottomDialogFragment(R.layout.dialog_common_bottom_list) {
    private val binding by viewBinding(DialogCommonBottomListBinding::bind)
    private val listAdapter by fastLazy { MultiTypeListAdapter<CommonBottomListItem<*>>() }

    private var title: CharSequence? = null
    private var list: List<CommonBottomListItem<*>>? = null
    private var onSwitchListener: ((checked: Boolean?, data: CommonBottomListItem<*>) -> Unit)? = null
    private var binderList: List<ItemViewBinder<CommonBottomListItem<*>, *>>? = null

    override fun initViews() {
        super.initViews()

        binding.tvTitle.text = title
        binding.rvList.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvList.adapter = listAdapter
        listAdapter.register(CommonBottomListItemBinder(onSwitchListener))
        //自定义binder
        binderList?.forEach {
            listAdapter.register(it)
        }
        list?.let { listAdapter.submitList(it) }
    }

    fun updateList(list: List<CommonBottomListItem<*>>) {
        listAdapter.submitList(list)
    }

    fun getAdapter(): MultiTypeListAdapter<CommonBottomListItem<*>> {
        return listAdapter
    }

    companion object {
        @JvmStatic
        private fun newInstance(
            title: CharSequence?,
            list: List<CommonBottomListItem<*>>,
            onSwitchListener: ((checked: Boolean?, data: CommonBottomListItem<*>) -> Unit)? = null
        ): CommonBottomListDialog {
            val dialog = CommonBottomListDialog()
            dialog.title = title
            dialog.list = list
            dialog.onSwitchListener = onSwitchListener
            return dialog
        }
    }

    class Builder<T : Any> {
        private var title: CharSequence? = null
        private var list: List<CommonBottomListItem<T>>? = null
        private var onSwitchListener: ((checked: Boolean?, data: CommonBottomListItem<T>, dialog: CommonBottomListDialog?) -> Unit)? = null
        private var binderList: List<ItemViewBinder<CommonBottomListItem<T>, *>>? = null
        private var dialog: CommonBottomListDialog? = null

        fun build(): CommonBottomListDialog? {
             dialog = newInstance(title, list ?: emptyList()) { checked, data ->
                onSwitchListener?.invoke(checked, data as CommonBottomListItem<T>, dialog)
            }
            return dialog
        }

        fun setTitle(title: CharSequence?): Builder<T> {
            this.title = title
            return this
        }

        fun setList(list: List<CommonBottomListItem<T>>): Builder<T> {
            this.list = list
            return this
        }

        fun setOnSwitchListener(onSwitchListener: ((checked: Boolean?, data: CommonBottomListItem<T>, dialog: CommonBottomListDialog?) -> Unit)? = null): Builder<T> {
            this.onSwitchListener = onSwitchListener
            return this
        }

        fun registerBinderList(binderList: List<ItemViewBinder<CommonBottomListItem<T>, *>>): Builder<T> {
            this.binderList = binderList
            return this
        }

    }


}

data class CommonBottomListItem<T : Any>(
    val title: String,
    var itemChecked: Boolean? = false,
    val data: T? = null
)