package com.tgv.malware.utils

import android.app.ActivityManager
import android.content.Context
import android.os.Environment
import android.os.StatFs
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import java.math.BigDecimal
import java.math.RoundingMode
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

object Utils {

    fun toDate(): String {
        val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        return formatter.format(Date())
    }

    fun Context.dpToPx(dp: Int): Float {
        val density = resources.displayMetrics.density
        return dp * density
    }

    fun getSizeTwoDecimalPlace(bytes: Long): String {
        val value = getSize(bytes)
        val formattedNumber = String.format("%.2f", value[0])
        return formattedNumber + value[1]
    }

    fun getSizeFormat(bytes: Long): String {
        if (bytes == 0.toLong()) return "0 B"
        val value = getSize(bytes)
        val formattedNumber = String.format("%.1f", value[0])
        return formattedNumber + value[1]
    }

    fun getSizeArray(bytes: Long): Array<String> {
        val value = getSizeFormat(bytes)
        val size = value.substring(0, value.indexOf(" "))
        val unit = value.substring(value.indexOf(" ") + 1)
        return arrayOf(size, unit)
    }

    fun getSize(bytes: Long): Array<Any> {
        val value: Double
        val unit: String
        if (bytes.toDouble() < 1024) {
            value = bytes.toDouble()
            unit = " B"
        } else if (bytes.toDouble() < 1024 * 1024) {
            value = bytes.toDouble() / 1024
            unit = " KB"
        } else if (bytes.toDouble() < 1024 * 1024 * 1024) {
            value = bytes.toDouble() / (1024 * 1024)
            unit = " MB"
        } else {
            value = bytes.toDouble() / (1024 * 1024 * 1024)
            unit = " GB"
        }
        return arrayOf(value, unit)
    }

    fun divide(dividend: Int, divisor: Int): String {
        val bigDecimalDividend = BigDecimal(dividend)
        val bigDecimalDivisor = BigDecimal(divisor)
        // 执行除法操作，并保留一位小数，不四舍五入
        val result = bigDecimalDividend.divide(bigDecimalDivisor, 1, RoundingMode.DOWN)
        // 将结果转换回Double
        return result.toString()
    }

    fun getMemoryTotal(context: Context): Long {
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val memoryInfo = ActivityManager.MemoryInfo()
        activityManager.getMemoryInfo(memoryInfo)
        return memoryInfo.totalMem
    }

    fun getMemoryAvailable(context: Context): Long {
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val memoryInfo = ActivityManager.MemoryInfo()
        activityManager.getMemoryInfo(memoryInfo)
        return memoryInfo.availMem
    }

    fun getTotalStorageSize(): Long {
        try {
            val statFs = StatFs(Environment.getExternalStorageDirectory().path)
            val statFs2 = StatFs(Environment.getRootDirectory().path)
            val blockSizeLong =
                statFs2.blockSizeLong * statFs2.blockCountLong + statFs.blockSizeLong * statFs.blockCountLong
            return calculateTotalStorage(blockSizeLong)
        } catch (e: Exception) {
            return 0
        }
    }

    fun getAvailableStorageSize(): Long {
        try {
            val statFs = StatFs(Environment.getExternalStorageDirectory().path)
            val statFs2 = StatFs(Environment.getRootDirectory().path)
            val blackSize =
                statFs2.blockSizeLong * statFs2.availableBlocksLong + statFs.blockSizeLong * statFs.availableBlocksLong
            return calculateAvailableStorage(blackSize)
        } catch (e: Exception) {
            return 0
        }
    }

    private fun calculateTotalStorage(size: Long): Long {
        var powerOfTwo: Long = 1
        while (powerOfTwo < size) {
            powerOfTwo *= 2
        }
        return powerOfTwo
    }

    private fun calculateAvailableStorage(size: Long): Long {
        var value = size
        var n = 0
        while (true) {
            val m: Long = 1000
            if (value < m) {
                break
            }
            value /= m
            n++
        }
        for (i in 0 until n) {
            value *= 1024
        }
        return value
    }

    fun Int.inflate(parent: ViewGroup, attachToRoot: Boolean = false): View {
        return LayoutInflater.from(parent.context).inflate(this, parent, attachToRoot)
    }
}