package com.test.basd.smartjunkcleaner.view

import android.annotation.SuppressLint
import android.content.Context
import android.text.format.Formatter
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import com.test.basd.smartjunkcleaner.R
import com.test.basd.smartjunkcleaner.bean.FileBean
import com.test.basd.smartjunkcleaner.databinding.DialogFileDetailBinding
import java.text.SimpleDateFormat
import java.util.Date

object FileDetailDialog {

    @SuppressLint("SetTextI18n")
    fun Context.showFileDetailDialog(fileBean: FileBean) {
        val binding = DialogFileDetailBinding.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 = resources.getDimensionPixelOffset(R.dimen.dp_300)
        lp?.height = ViewGroup.LayoutParams.WRAP_CONTENT
        dialog.window?.attributes = lp
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        binding.tvName.text = fileBean.name
        val dateFormat = SimpleDateFormat("MMM dd, yyyy")
//        , Locale.ENGLISH
        val date = Date(fileBean.time)
        // 格式化日期
        val time = dateFormat.format(date)
        binding.tvTime.text = "create time: $time"
        val size = Formatter.formatFileSize(this, fileBean.size)
        binding.tvSize.text = "size: $size"
        binding.tvPath.text = "path: ${fileBean.path}"
    }
}