package com.develop.food.base.ui

import android.content.Context
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.develop.base.R
import com.develop.food.base.utils.GlobalApp

open class BaseFragment: Fragment() {

    lateinit var hostActivity: AppCompatActivity
    private val plainDialogView by lazy {
        PlainDialogView(hostActivity, R.layout.dialog_easy_view)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        hostActivity = context as AppCompatActivity
    }

    fun showDialog(cancelable: Boolean = false) {
        if (hostActivity is BaseActivity<*>) {
            (hostActivity as BaseActivity<*>).showDialog(cancelable)
        }else {
            plainDialogView.showDialog(cancelable)
        }
    }

    fun dismissDialog() {
        if (hostActivity is BaseActivity<*>) {
            (hostActivity as BaseActivity<*>).dismissDialog()
        }else {
            plainDialogView.hideDialog()
        }
    }

    fun showToast(message: String) {
        val useContext = if (context == null) {
            GlobalApp()
        }else {
            requireContext()
        }
        Toast.makeText(useContext, message, Toast.LENGTH_SHORT).show()
    }
}