package com.dahe.mylibrary.utils

import androidx.appcompat.app.AppCompatActivity
import com.dahe.mylibrary.base.SingletonHolder
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.impl.LoadingPopupView

/**
 * @ClassName LoadingUtils
 * @Author john
 * @Date 2024/2/29 11:10
 * @Description TODO
 */
class LoadingUtils3 private constructor(activity: AppCompatActivity) {
    var loadingPopup: LoadingPopupView? = null
    lateinit var activity: AppCompatActivity

    companion object : SingletonHolder<LoadingUtils3, AppCompatActivity>(::LoadingUtils3)

    init {
        this.activity = activity
    }

    fun init(message: String = "网络请求中......"): LoadingPopupView? {
        if (loadingPopup == null) {
            loadingPopup = XPopup.Builder(activity)
                .dismissOnBackPressed(false)
                .isLightNavigationBar(true)
                .asLoading(message, LoadingPopupView.Style.ProgressBar)
        }

        return loadingPopup
    }

    fun showLoading(message: String = "网络请求中......") {
        loadingPopup = XPopup.Builder(activity)
            .dismissOnBackPressed(false)
            .isLightNavigationBar(true)
            .asLoading(message, LoadingPopupView.Style.ProgressBar)
            .show() as LoadingPopupView
    }

    fun dissLoading() {
        if (loadingPopup != null && loadingPopup!!.isShow) {
            loadingPopup!!.dismiss()
        }
    }

    fun isShow() = loadingPopup?.isShow

}