package com.zxhy.solarmasterace.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import com.zxhy.solarmasterace.R
import com.zxhy.solarmasterace.data.FileBean
import com.zxhy.solarmasterace.databinding.DialogFolderDetailBinding
import com.zxhy.solarmasterace.databinding.DialogFolderNameBinding

fun Context.showFolderDialog(
    fileName: String = "",
    tittle: String = "",
    newFolder: ((name: String) -> Unit)? = null,
    reName: ((name: String) -> Unit)? = null,
) {

    val binding = DialogFolderNameBinding.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_271)
    lp?.height = this.resources.getDimensionPixelOffset(R.dimen.dp_181)
    dialog.window?.attributes = lp
    dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
    binding.ivCancel.setOnClickListener {
        dialog.dismiss()
    }
    binding.etName.setText(fileName)
    binding.tvSure.setOnClickListener {
        val name = binding.etName.text.toString()
        if (name.isEmpty() or name.isBlank()) return@setOnClickListener
        when (tittle) {
            "New Folder" -> {
                newFolder?.invoke(name)
            }

            "Rename" -> {
                reName?.invoke(name)
            }
        }
        dialog.dismiss()
    }

}


@SuppressLint("SetTextI18n")
fun Context.showFolderDetailDialog(
    context: Context,
    fileBean: FileBean,
) {

    val binding = DialogFolderDetailBinding.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_271)
    lp?.height = ViewGroup.LayoutParams.WRAP_CONTENT
    dialog.window?.attributes = lp
    dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
    fileBean.bindIconImage(binding.ivFile)
    binding.tvName.text = fileBean.name
    binding.tvSub.text = fileBean.file().list()?.size.toString() + " items"
    binding.tvTime.text = "Time:" + fileBean.time
    binding.tvSize.text = "Size:" + fileBean.sizeFormat(context)
    binding.tvPosition.text = fileBean.path
    binding.tvCancel.setOnClickListener {
        dialog.dismiss()
    }

}
