package com.rugvedsarika.deviceinfo

import android.content.Context
import android.os.Build
import android.app.ActivityManager
import android.os.Environment
import android.os.StatFs
import java.io.File

object DeviceInfoProvider {

    fun getBrand(): String = Build.BRAND.replaceFirstChar { it.uppercase() }

    fun getModel(): String = Build.MODEL

    fun getAndroidVersion(): String = "Android ${Build.VERSION.RELEASE} (API ${Build.VERSION.SDK_INT})"

    fun getManufacturer(): String = Build.MANUFACTURER

    fun getBuildNumber(): String = Build.DISPLAY

    fun getCpuInfo(): String {
        return "${Build.SUPPORTED_ABIS.joinToString(", ")}"
    }

    fun getRamInfo(context: Context): String {
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val memoryInfo = ActivityManager.MemoryInfo()
        activityManager.getMemoryInfo(memoryInfo)
        
        val totalRam = memoryInfo.totalMem / (1024 * 1024 * 1024.0)
        val availableRam = memoryInfo.availMem / (1024 * 1024 * 1024.0)
        
        return "Total: %.2f GB / Available: %.2f GB".format(totalRam, availableRam)
    }

    fun getInternalStorageInfo(): String {
        val path: File = Environment.getDataDirectory()
        val stat = StatFs(path.path)
        val blockSize = stat.blockSizeLong
        val totalBlocks = stat.blockCountLong
        val availableBlocks = stat.availableBlocksLong
        
        val totalSize = (totalBlocks * blockSize) / (1024 * 1024 * 1024.0)
        val availableSize = (availableBlocks * blockSize) / (1024 * 1024 * 1024.0)
        
        return "Total: %.2f GB / Free: %.2f GB".format(totalSize, availableSize)
    }

    fun getStoragePercent(): Int {
        val path: File = Environment.getDataDirectory()
        val stat = StatFs(path.path)
        val totalBlocks = stat.blockCountLong
        val availableBlocks = stat.availableBlocksLong
        val usedBlocks = totalBlocks - availableBlocks
        return if (totalBlocks > 0) ((usedBlocks.toDouble() / totalBlocks) * 100).toInt() else 0
    }

    fun getDisplayInfo(context: Context): Map<String, String> {
        val displayMetrics = context.resources.displayMetrics
        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as android.view.WindowManager
        val display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            context.display
        } else {
            @Suppress("DEPRECATION")
            windowManager.defaultDisplay
        }
        
        val refreshRate = display?.refreshRate ?: 0f
        
        return mapOf(
            "Resolution" to "${displayMetrics.widthPixels} x ${displayMetrics.heightPixels}",
            "Density" to "${displayMetrics.densityDpi} DPI",
            "Refresh Rate" to "%.0f Hz".format(refreshRate),
            "Size" to "%.1f inches".format(calculateScreenSize(displayMetrics))
        )
    }

    private fun calculateScreenSize(dm: android.util.DisplayMetrics): Double {
        val x = Math.pow(dm.widthPixels.toDouble() / dm.xdpi, 2.0)
        val y = Math.pow(dm.heightPixels.toDouble() / dm.ydpi, 2.0)
        return Math.sqrt(x + y)
    }
}
