package com.remax.visualnovel.app.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.dylanc.loadingstateview.BgColorType
import com.dylanc.loadingstateview.Decorative
import com.dylanc.loadingstateview.LoadingState
import com.dylanc.loadingstateview.LoadingStateDelegate
import com.dylanc.loadingstateview.OnReloadListener
import com.dylanc.viewbinding.base.FragmentBinding
import com.dylanc.viewbinding.base.FragmentBindingDelegate
import com.remax.visualnovel.app.AbsView
import com.remax.visualnovel.app.widget.LoadingDialog
import com.remax.visualnovel.extension.getBgColor
import com.remax.visualnovel.extension.toast

/**
 * 基类Fragment
 */
abstract class BaseBindingFragment<VB : ViewBinding> : Fragment(), AbsView,
    LoadingState by LoadingStateDelegate(), OnReloadListener, Decorative,
    FragmentBinding<VB> by FragmentBindingDelegate() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        val view = createViewWithBinding(inflater, container).decorate(this, this)
        view.setBackgroundColor(requireContext().getBgColor(backgroundColorType()))
        return view
    }

    open fun backgroundColorType() = BgColorType.SPECIAL_MAP

    private val loadingDialog by lazy {
        createLoadingDialog()
    }

    private var isLoaded = false

    private fun createLoadingDialog(): LoadingDialog {
        val dialog = LoadingDialog()
        dialog.build(requireActivity())
        return dialog
    }

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

    override fun onResume() {
        super.onResume()
        if (!isLoaded && !isHidden) {
            lazyInit()
            isLoaded = true
        }
    }

    abstract fun onCreated(bundle: Bundle?)

    open fun lazyInit() {}

    open fun subscribeUi() {}

    override fun showLoading() {
        if (activity?.isDestroyed == false) {
            activity?.runOnUiThread {
                loadingDialog.show()
            }
        }
    }

    override fun hideLoading() {
        if (activity?.isDestroyed == false) {
            activity?.runOnUiThread {
                loadingDialog.dismiss()
            }
        }
    }

    override fun showToast(text: String?) {
        if (!isDetached) {
            activity?.let {
                if (!it.isDestroyed) {
                    it.toast(text)
                }
            }
        }
    }

    override fun showToast(resId: Int) {
        if (!isDetached) {
            activity?.let {
                if (!it.isDestroyed) {
                    it.toast(resId)
                }
            }
        }
    }

    override fun onDetach() {
        if (loadingDialog.getDialog().isShowing) {
            loadingDialog.dismiss()
        }
        super.onDetach()
    }
}