package com.zxhy.hfilemanagermaster.dialog

import android.content.Context
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import com.example.hfilemanagermaster.R
import com.example.hfilemanagermaster.databinding.DialogFileNameBinding

fun Context.showFileNameDialog(
    tittle: String = "",
    sureAction: (name: String) -> Unit,
    dismissAction: () -> Unit
) {
    val binding = DialogFileNameBinding.inflate(LayoutInflater.from(this))
    val dialog = AlertDialog.Builder(this).setView(binding.root).create()
    dialog.show()
    dialog.setCanceledOnTouchOutside(false)

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

    binding.tvTittle.text = tittle
    binding.tvSure.setOnClickListener {
        sureAction.invoke(binding.etName.text.toString())
    }
    binding.ivCancel.setOnClickListener {
        dialog.dismiss()
    }

    dialog.setOnCancelListener { dismissAction.invoke() }
}
