package kr.co.vividnext.sodalive.common

import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import kr.co.vividnext.sodalive.databinding.DialogLoadingBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class LoadingDialog(
    activity: Activity,
    layoutInflater: LayoutInflater
) {
    private val alertDialog: AlertDialog
    private val dialogView = DialogLoadingBinding.inflate(layoutInflater)

    init {
        val dialogBuilder = AlertDialog.Builder(activity)
        dialogBuilder.setView(dialogView.root)

        alertDialog = dialogBuilder.create()
        alertDialog.setCancelable(false)
        alertDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }

    fun show(width: Int, message: String = "") {
        alertDialog.show()
        dialogView.tvLoading.text = message

        val lp = WindowManager.LayoutParams()
        lp.copyFrom(alertDialog.window?.attributes)
        lp.width = width - (26.7f.dpToPx()).toInt()
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT

        alertDialog.window?.attributes = lp
    }

    fun dismiss() {
        alertDialog.dismiss()
    }
}
