package com.zxhy.hfilemanagermaster.dialog

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import androidx.annotation.DrawableRes
import androidx.appcompat.app.AlertDialog
import com.example.hfilemanagermaster.R
import com.example.hfilemanagermaster.databinding.DialogOperationBinding

fun Context.showOperationDialog(
    @DrawableRes imageRes: Int,
    tips: String = "",
    dismissAction: () -> Unit
): Dialog {
    val binding = DialogOperationBinding.inflate(LayoutInflater.from(this))
    val dialog = AlertDialog.Builder(this).setView(binding.root).create()
    dialog.show()
    dialog.setCanceledOnTouchOutside(true)

    //修改dialog的尺寸
    val lp = dialog.window?.attributes
    lp?.width = this.resources.getDimensionPixelOffset(R.dimen.dp_150)
    lp?.height = this.resources.getDimensionPixelOffset(R.dimen.dp_120)
    dialog.window?.attributes = lp
    dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
    dialog.window?.setDimAmount(0f)

    binding.iv.setImageResource(imageRes)
    binding.tv.text = tips
    dialog.setOnCancelListener { dismissAction.invoke() }
    return dialog
}

