package com.rc.core.ui.dialog

import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.rc.core.R
import com.rc.core.ui.common.AbsUIDelegate
import com.rc.core.ui.common.IUIListener
import io.reactivex.disposables.Disposable

/**
 * info
 *
 * @author ReiChin_
 */
abstract class RcBaseDialog<VB : ViewBinding> :
    DialogFragment(), IUIListener {

    private lateinit var _viewBinding: VB

    protected val viewBinding get() = _viewBinding

    private lateinit var mUIDelegate: AbsUIDelegate

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mUIDelegate = AbsUIDelegate.create()

        setStyle(STYLE_NO_TITLE, R.style.LoadingDialog)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        initWindowConfig()
        _viewBinding = createViewBinding(inflater, container)
        return _viewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        initDialogSize()
    }

    protected open fun initDialogSize() {
        dialog?.window?.apply {
            attributes.width = WindowManager.LayoutParams.MATCH_PARENT
            attributes.height = WindowManager.LayoutParams.WRAP_CONTENT
        }
    }

    protected open fun initWindowConfig() {
//        dialog?.window?.apply {
//            setWindowAnimations(R.style.BottomDialog_Animation)
//            setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL)
//        }
//
//        dialog?.window?.apply {
//            attributes.gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL
//            attributes.windowAnimations = R.style.BottomDialog_Animation
//            requestFeature(Window.FEATURE_NO_TITLE)
//            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
//        }
//        dialog?.setCanceledOnTouchOutside(true)
    }

    protected abstract fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): VB

    protected abstract fun initViews(savedInstanceState: Bundle?)

    override fun showLoading(message: String?, cancelable: Boolean) {
        mUIDelegate.showLoading(requireContext(), message, cancelable)
    }

    override fun dismissLoading() {
        mUIDelegate.dismissLoading()
    }

    override fun showToast(message: String) {
        mUIDelegate.showToast(context, message)
    }

    override fun showNetError(throwable: Throwable) {
        mUIDelegate.showNetError(context, throwable)
    }

    override fun addDisposable(disposable: Disposable) {
        mUIDelegate.addDisposable(disposable)
    }

    override fun onDestroyView() {
        mUIDelegate.clearDisposable()
        super.onDestroyView()
    }

    override fun createItemDecoration(): RecyclerView.ItemDecoration? {
        return mUIDelegate.createItemDecoration(context)
    }

}