package com.zxhy.fastfilemanagerpro.kokodialog

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import com.zxhy.fastfilemanagerpro.R
import com.zxhy.fastfilemanagerpro.databinding.DialogKokoFileNameBinding
import java.io.File

object FileNameDialog {
    fun Context.showFileNameDialog(
        file: File,
        sureAction: (name: String) -> Unit
    ): Dialog {
        val binding = DialogKokoFileNameBinding.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_260)
        lp?.height = this.resources.getDimensionPixelOffset(R.dimen.dp_190)
        dialog.window?.attributes = lp
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
        binding.apply {
            edit.setText(file.name.toString())
            tvSure.setOnClickListener {
                runCatching {
                    if (!edit.text.isNullOrEmpty()) {
                        dialog.dismiss()
                        sureAction.invoke(edit.text.toString())
                    }
                }
            }
            ivDelete.setOnClickListener {
                dialog.dismiss()
            }
        }

        return dialog
    }
}