package com.nivesh.production.partnerOnBoarding.util

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.Log
import android.view.Window
import android.widget.ProgressBar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@SuppressLint("StaticFieldLeak")
object ProgressUtil{

    private lateinit var dialogBuilder: AlertDialog.Builder
    private lateinit var alertDialog: AlertDialog
    private lateinit var pDialog: ProgressBar


    fun showLoading(ctx: Context){
        hideLoading()
        dialogBuilder= AlertDialog.Builder(ctx)
        pDialog= ProgressBar(ctx)
        dialogBuilder.setCancelable(false)
        dialogBuilder.setView(pDialog)
        alertDialog=dialogBuilder.create()
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        alertDialog.window?.setBackgroundDrawable(ColorDrawable(Color.parseColor("#00141414")))
        alertDialog.show()

    }


    fun hideLoading(){
        try {
            CoroutineScope(IO).launch {
                delay(1000)
                if(alertDialog.isShowing){
                    alertDialog.dismiss()
                }
            }
        } catch (e: UninitializedPropertyAccessException) {
              Log.e("TAG","AlertDialog UninitializedPropertyAccessException")
        }
    }
}