package com.test.basd.smartjunkcleaner.view

import android.content.Context
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import com.test.basd.smartjunkcleaner.R
import com.test.basd.smartjunkcleaner.databinding.DialogFileDeleteBinding


object FileDeleteDialog {
    fun Context.showFileDeleteDialog(click: () -> Unit) {
        val binding = DialogFileDeleteBinding.inflate(LayoutInflater.from(this))
        val dialog = AlertDialog.Builder(this).setView(binding.root).create()

        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(true)
        dialog.show()

        //修改dialog的尺寸
        val lp = dialog.window?.attributes
        lp?.width = this.resources.getDimensionPixelOffset(R.dimen.dp_310)
        lp?.height = ViewGroup.LayoutParams.WRAP_CONTENT
        dialog.window?.attributes = lp
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        val text = "Selected files cannot be recovered after deleting, continue anyway?"
        val spannableString = SpannableString(text)
        val start = text.indexOf("cannot")
        if (start != -1) {
            val end = start + "cannot be recovered".length
            val colorSpan = ForegroundColorSpan(ContextCompat.getColor(this, R.color.color_f04949)) // 使用资源文件中的颜色
            spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        binding.tvDesc.text = spannableString
        binding.tvDelete.setOnClickListener {
            dialog.dismiss()
            click.invoke()
        }
        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }
    }
}