package com.ama.core.architecture.appBase

import android.os.Build
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.ama.core.architecture.base.BaseViewModel
import com.ama.core.architecture.base.views.BaseViewsActivity
import com.ama.core.architecture.util.eventbus.NotifyMan
import com.ama.core.architecture.util.state.createAppStateView
import com.ama.core.designsystem.widget.interfaces.LoadingDialog


abstract class AppViewsActivity<Binding : ViewBinding, UiState : Any, ViewModel : BaseViewModel<UiState>> :
    BaseViewsActivity<Binding, UiState, ViewModel>() {
    // 全部使用反射来实现
    //override val viewModel: ViewModel by reflectViewModels()

    private var mEventCallback: NotifyMan.ICallback? = null

    protected fun registerEvents(onEvents: ((NotifyMan.NotifyData<*>?)->Unit), vararg eventTypes: Int) {
        if (mEventCallback == null) {
            mEventCallback = object : NotifyMan.ICallback(true) {
                override fun onEvent(data: NotifyMan.NotifyData<*>?) {
                    onEvents.invoke(data)
                }
            }
        }

        NotifyMan.instance().register(mEventCallback, *eventTypes)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mEventCallback != null) {
            NotifyMan.instance().unregister(mEventCallback)
        }
    }



    override fun createStateView(stateViewParent: ViewGroup) = createAppStateView(stateViewParent)

    // 消息的收集者，默认为Toast，可覆写为Snakbar等。
    //    override val messageCollector: MessageCollector by lazy { ToastMessageCollector(applicationContext) }
    //    override val messageCollector: MessageCollector by lazy { SnakbarViewMessageCollector(binding.root) }

    override fun initEdgeToEdge() {
        super.initEdgeToEdge()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            window.isNavigationBarContrastEnforced = false  // 导航栏透明
        }
    }

    // 全部使用反射来实现
    //    override fun inflateViewBinding(inflater: LayoutInflater) = reflectInflateViewBinding(inflater)

    override fun initLoadingDialog(loadingDialog: LoadingDialog) {
        loadingDialog.dialog.setCancelable(false)
        loadingDialog.dialog.setCanceledOnTouchOutside(false)
    }
}