package com.doverfuelingsolutions.issp.view.fragment

import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.doverfuelingsolutions.issp.view.widget.FragmentToolbar
import com.doverfuelingsolutions.issp.utils.StringUtil
import com.doverfuelingsolutions.issp.view.util.LoadingDialogBuilder

abstract class FragmentBasic : Fragment() {

    private var mToolbar: FragmentToolbar? = null

    fun setToolBar(toolbar: FragmentToolbar) {
        mToolbar = toolbar
        toolbar.setBackHandler { back() }
        toolbar.setOnCountdownFinish { home() }
        toolbar.setHomeHandler { home() }
    }

    fun loading(@StringRes string: Int) = loading(StringUtil.get(string))
    private fun loading(msg: String): AlertDialog {
        return LoadingDialogBuilder(requireContext())
            .setLoadingText(msg)
            .setCancelable(true)
            .show()
    }

    fun home() {
        val fragmentManager = requireActivity().supportFragmentManager
        val deep = fragmentManager.backStackEntryCount
        if (deep > 0)
            fragmentManager.popBackStack(
                fragmentManager.getBackStackEntryAt(0).id,
                FragmentManager.POP_BACK_STACK_INCLUSIVE
            )
    }

    fun back() {
        requireActivity().supportFragmentManager.popBackStack()

    }
}