package com.dahe.mylibrary.utils

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

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


    companion object {
        val instance = Holder.holder
    }

    object Holder {
        val holder = LoadingUtils()
    }


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

        return loadingPopup
    }

    fun showLoading(activity: AppCompatActivity, message: String = "网络请求中......") {
        if (!activity.isDestroyed)
            loadingPopup = XPopup.Builder(activity)
                .dismissOnBackPressed(true)
                .isLightNavigationBar(true)
//                .asLoading(message, R.layout.custom_loading_popup,LoadingPopupView.Style.Spinner)
                .asLoading(message, LoadingPopupView.Style.Spinner)
                .show() as LoadingPopupView
    }

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

    fun isShow() = loadingPopup?.isShow

}