package com.example.defenseapplication.view

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import com.example.defenseapplication.databinding.DialogLoadingBinding

class LoadingDialog(context: Context) {

    private val dialog: Dialog = Dialog(context)
    private val binding: DialogLoadingBinding

    init {
        binding = DialogLoadingBinding.inflate(LayoutInflater.from(context))
        dialog.setContentView(binding.root)
        dialog.window?.apply {
            setGravity(Gravity.CENTER)
            setLayout(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT
            )
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            setDimAmount(0.5f)
        }
        dialog.setCancelable(false)
    }

    fun setMessage(message: String): LoadingDialog {
        binding.tvLoading.text = message
        return this
    }

    fun show() {
        if (!dialog.isShowing) {
            dialog.show()
        }
    }

    fun dismiss() {
        if (dialog.isShowing) {
            dialog.dismiss()
        }
    }

    fun isShowing(): Boolean {
        return dialog.isShowing
    }
}