package com.xyzshell.andinfo.libs

import android.content.Context
import android.hardware.display.DisplayManager
import android.os.Build
import android.view.Display
import android.graphics.Point
import android.util.DisplayMetrics

class DisplayInfo(private val context: Context) {

    private val displayManager = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager

    fun getDefaultDisplayInfo(): DefaultDisplayInfo? {
        val defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY) ?: return null

        val displayMetrics = DisplayMetrics()
        defaultDisplay.getMetrics(displayMetrics)

        val size = Point()
        defaultDisplay.getSize(size)

        val mode = defaultDisplay.mode
        val refreshRate = mode.refreshRate

        val isHdr = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            defaultDisplay.hdrCapabilities?.supportedHdrTypes?.isNotEmpty() ?: false
        } else {
            false
        }

        val isWideColorGamut = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            defaultDisplay.isWideColorGamut
        } else {
            false
        }

        return DefaultDisplayInfo(
            id = defaultDisplay.displayId,
            name = defaultDisplay.name,
            widthPixels = displayMetrics.widthPixels,
            heightPixels = displayMetrics.heightPixels,
            densityDpi = displayMetrics.densityDpi,
            xdpi = displayMetrics.xdpi,
            ydpi = displayMetrics.ydpi,
            refreshRate = refreshRate,
            isHdr = isHdr,
            isWideColorGamut = isWideColorGamut
        )
    }

    data class DefaultDisplayInfo(
        val id: Int,
        val name: String,
        val widthPixels: Int,
        val heightPixels: Int,
        val densityDpi: Int,
        val xdpi: Float,
        val ydpi: Float,
        val refreshRate: Float,
        val isHdr: Boolean,
        val isWideColorGamut: Boolean
    )
}
