package com.easy.clean.ui.dialog

import android.view.Gravity
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.easy.clean.R
import com.easy.clean.databinding.DialogConfirmBinding

class ConfirmDialog(
    val activity: AppCompatActivity
) {
    val dialog = AlertDialog.Builder(activity).create()
    val binding = DialogConfirmBinding.inflate(LayoutInflater.from(activity))

    var action: (() -> Unit)? = null

    fun showDialog(type: String) {
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()

        val params = dialog.window?.attributes
        params?.width = LinearLayout.LayoutParams.MATCH_PARENT
        params?.height = LinearLayout.LayoutParams.WRAP_CONTENT
        params?.gravity = Gravity.CENTER
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        when (type) {
            "delete" -> {
                binding.tvTitle.text = activity.getString(R.string.delete_title)
                binding.tvContent.text = activity.getString(R.string.delete_content)
                binding.tvConfirm.text = activity.getString(R.string.delete)
            }
        }
        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvConfirm.setOnClickListener {
            dialog.dismiss()
            action?.invoke()
        }

    }
}