package com.zxhy.hfilemanagermaster.dialog

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.example.hfilemanagermaster.R
import com.example.hfilemanagermaster.databinding.DialogFileDetailsBinding
import com.example.hfilemanagermaster.databinding.DialogFileNameBinding
import com.zxhy.hfilemanagermaster.glide.loadIntoImageView
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale

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 {
        dialog.dismiss()
        sureAction.invoke(binding.etName.text.toString())
    }
    binding.ivCancel.setOnClickListener {
        dialog.dismiss()
    }

    dialog.setOnCancelListener { dismissAction.invoke() }
}


@SuppressLint("SetTextI18n")
fun Context.showFileDetailsDialog(
    tittle: String = "",
    file: File,
    cancelAction: () -> Unit,
    dismissAction: () -> Unit
) {
    val binding = DialogFileDetailsBinding.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_233)
    lp?.height= ViewGroup.LayoutParams.WRAP_CONTENT
    dialog.window?.attributes = lp
    dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
    dialog.window?.setDimAmount(0f)

    binding.apply {
        tvTittle.text = tittle

        if (file.name.contains(".jpg")
            or
            file.name.contains(".mp4")
            or
            file.name.contains(".png")
        ) {
            loadIntoImageView(this@showFileDetailsDialog, file.absoluteFile, binding.ivFile)
        } else if (file.name.contains(".txt")) {
            ivFile.setImageResource(R.mipmap.io_002)
        } else if (file.name.contains(".xlsx") or file.name.contains(".xls")) {
            ivFile.setImageResource(R.mipmap.as_85230)
        } else if (file.name.contains(".pdf")) {
            ivFile.setImageResource(R.mipmap.io_0012)
        } else if (file.name.contains(".ppt") or file.name.contains(".pptx")) {
            ivFile.setImageResource(R.mipmap.io_002223)
        } else if (file.name.contains(".apk")) {
            ivFile.setImageResource(R.mipmap.fe_98856)
        } else if (file.name.contains(".zip")) {
            ivFile.setImageResource(R.mipmap.qwett_98959)
        } else if (file.name.contains("log") && file.isFile) {
            ivFile.setImageResource(R.mipmap.rqwr_875)
        } else if (file.isDirectory) {
            ivFile.setImageResource(R.mipmap.tt_695)
        } else {
            ivFile.setImageResource(R.drawable.icon_file_unknown)
        }
        tvName.text = file.name
        val sizeS = Formatter.formatFileSize(this@showFileDetailsDialog, file.length())
        tvSize.text = sizeS
        val timeE = SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH).format(file.lastModified())
        tvTime.text = "Time:$timeE"
        tvPosition.text = "Position:${file.absoluteFile}"

        tvCancel.setOnClickListener {
            cancelAction.invoke()
            dialog.dismiss()
        }
        ivCancel.setOnClickListener {
            cancelAction.invoke()
            dialog.dismiss()
        }
    }
    dialog.setOnCancelListener { dismissAction.invoke() }
}