package com.base.scanqr.ui.widget

import android.app.AlertDialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import com.base.scanqr.databinding.DialogInputNameBinding
import com.base.scanqr.utils.ToastUtils.toast

object InputNameDialog {

    fun Context.showInputNameDialog(tempName: String, newNameAction: (name: String) -> Unit) {

        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogInputNameBinding.inflate(LayoutInflater.from(this))
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(false)

        dialog.show()

        val params = dialog.window?.attributes
//        params?.width = resources.getDimensionPixelOffset(R.dimen.dp_320)
//        params?.height = resources.getDimensionPixelOffset(R.dimen.dp_400)
        params?.gravity = Gravity.CENTER
//        params?.y = 50

        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        binding.edit.setText(tempName)
        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvConfirm.setOnClickListener {
            val flag = binding.edit.text.isEmpty()
            if (flag) {
                toast("File name is Empty", true)
                return@setOnClickListener
            }
            dialog.dismiss()
            newNameAction.invoke(binding.edit.text.toString())
        }
    }
}