package pxds.lmmd.zmwf

import android.app.usage.StorageStats
import android.app.usage.StorageStatsManager
import android.content.Context
import android.content.pm.IPackageStatsObserver
import android.content.pm.PackageManager
import android.content.pm.PackageStats
import android.os.Build
import android.os.storage.StorageManager
import android.text.format.Formatter
import androidx.annotation.RequiresApi
import com.test.basd.supercleanermax.R
import pxds.lmmd.zmwf.Khko.string
import java.io.IOException
import java.util.UUID

class Tqzu private constructor() {

    private val TAG = R.string.ޔޟޕގޔ_D.string()

    fun printPackageStats(context: Context, packageName: String, packageStats: PackageStats?) {
        if (packageStats != null) {
            val cacheSizeF = Formatter.formatFileSize(context, packageStats.cacheSize)
            val dataSizeF = Formatter.formatFileSize(context, packageStats.dataSize)
            val codeSizeF = Formatter.formatFileSize(context, packageStats.codeSize)
            val size = packageStats.cacheSize + packageStats.dataSize + packageStats.codeSize
            val total = Formatter.formatFileSize(context, size)

            val log = R.string.ޞއމނޤ_D.string((packageName).toString(), (cacheSizeF).toString(), (dataSizeF).toString(), (codeSizeF).toString(), (total).toString())
            Egac.logDebug(TAG, log)
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun printStorageStats(context: Context, packageName: String, storageStats: StorageStats?) {
        if (storageStats != null) {
            val cacheBytesF = Formatter.formatFileSize(context, storageStats.cacheBytes)
            val dataBytesF = Formatter.formatFileSize(context, storageStats.cacheBytes)
            val appBytesF = Formatter.formatFileSize(context, storageStats.cacheBytes)
            val size = storageStats.cacheBytes + storageStats.dataBytes + storageStats.appBytes
            val total = Formatter.formatFileSize(context, size)
            val log = R.string.ޅހގޣގ_D.string((packageName).toString(), (cacheBytesF).toString(), (dataBytesF).toString(), (appBytesF).toString(), (total).toString())
            Egac.logDebug(TAG, log)
        }
    }

    companion object {
        private var mApiUrl: Tqzu? = null
        val instance: Tqzu?
            get() {
                if (mApiUrl == null) {
                    synchronized(Tqzu::class.java) {
                        if (mApiUrl == null) {
                            mApiUrl = Tqzu()
                        }
                    }
                }
                return mApiUrl
            }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    fun getAppSizeO(context: Context, packageName: String): Long {
        val storageStatsManager = context.getSystemService(Context.STORAGE_STATS_SERVICE) as StorageStatsManager
        val storageManager = context.getSystemService(Context.STORAGE_SERVICE) as StorageManager

        val storageVolumes = storageManager.getStorageVolumes()
        for (item in storageVolumes) {
            val uuidStr = item.uuid
            val uuid: UUID = if (uuidStr == null) {
                StorageManager.UUID_DEFAULT
            } else {
                UUID.fromString(uuidStr)
            }
            val uid = getUid(context, packageName)

            var storageStats: StorageStats? = null
            try {
                storageStats = storageStatsManager.queryStatsForUid(uuid, uid)
            } catch (e: IOException) {
                e.printStackTrace()
            }
            if (storageStats != null) {
                val size = storageStats.cacheBytes + storageStats.dataBytes + storageStats.appBytes
                printStorageStats(context, packageName, storageStats)
                return size
            }
        }
        return 0
    }

    fun getUid(context: Context, pakName: String?): Int {
        try {
            return context.packageManager.getApplicationInfo(pakName!!, PackageManager.GET_META_DATA).uid
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return -1
    }

    fun getAppSize(context: Context, packageName: String): Long {
        var size = 0L
        try {
            val method = PackageManager::class.java.getMethod(R.string.ގޑޜށށ_D.string(), String::class.java, IPackageStatsObserver::class.java)

            method.invoke(context.packageManager, packageName, object : IPackageStatsObserver.Stub() {
                override fun onGetStatsCompleted(pStats: PackageStats, succeeded: Boolean) {
                    size = pStats.cacheSize + pStats.dataSize + pStats.codeSize
                    printPackageStats(context, packageName, pStats)
                }
            })
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return size
    }
}
