package com.zxhy.simplefilesmanager.simplejoke

import android.annotation.SuppressLint
import android.app.usage.StorageStatsManager
import android.content.Context
import android.os.Build
import android.os.storage.StorageManager
import android.os.storage.StorageVolume
import androidx.annotation.RequiresApi
import java.io.File
import java.lang.reflect.Field
import java.lang.reflect.Method
import java.util.UUID

/**
 * 计算储存介质
 */

object DiskHelper {
    @SuppressLint("ObsoleteSdkInt", "DiscouragedPrivateApi")
    fun getMountInfoList(context: Context): List<Disk> {
        val diskList = arrayListOf<Disk>()
        val storageManager = context.getSystemService(Context.STORAGE_SERVICE) as StorageManager
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {//小于6.0
            val getVolumeListMethod = StorageManager::class.java.getDeclaredMethod("getVolumeList")
            val volumes = getVolumeListMethod.invoke(storageManager) as Array<StorageVolume>
            for (volume in volumes) {
                try {
                    val fileMethod = volume.javaClass.getDeclaredMethod("getPathFile")
                    val file = fileMethod.invoke(volume) as File
                    val path = file.path
                    val name = file.name
                    val total = file.totalSpace //总计
                    val used = total - file.freeSpace //空闲
                    val usable = file.usableSpace //可用
                    diskList.add(Disk(path, name, -1, total, used, usable))
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        } else {
            val getVolumeMethod = StorageManager::class.java.getDeclaredMethod("getVolumes")
            val volumes = getVolumeMethod.invoke(storageManager) as List<Any>
            for (volume in volumes) {
                try {
                    val getTypeField: Field = volume.javaClass.getField("type")
                    val type: Int = getTypeField.getInt(volume)
                    val fileMethod = volume.javaClass.getDeclaredMethod("getPath")
                    val f: File = fileMethod.invoke(volume) as File
                    val path = f.path
                    val name = f.name
                    val total =
                        if (type != Disk.TYPE_U && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            val getFsUuid: Method = volume.javaClass.getDeclaredMethod("getFsUuid")
                            val fsUuid = getFsUuid.invoke(volume) as? String
                            getTotalSize(context, fsUuid)
                        } else {
                            f.totalSpace //总计
                        }
                    val used = total - f.usableSpace //空闲
                    val usable = f.usableSpace //空闲
                    val disk = Disk(path, name, type, total, used, usable)
                    diskList.add(disk)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
        return diskList
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun getTotalSize(context: Context, fsUuid: String?): Long {
        return try {
            val id = if (fsUuid == null) StorageManager.UUID_DEFAULT else UUID.fromString(fsUuid)
            val stats = context.getSystemService(StorageStatsManager::class.java)
            stats.getTotalBytes(id)
        } catch (e: Exception) {
            e.printStackTrace()
            -1
        }
    }
}
