package com.ray650128.gstreamer_demo_app

import android.content.Context
import android.os.Build
import android.util.DisplayMetrics
import android.view.WindowManager

/**
 * 螢幕參數工具類別
 * @author Raymond Yang
 */
class DisplayUtils(private var context: Context) {
    private val TAG = DisplayUtils::class.java.simpleName

    /**
     * 取得螢幕寬度
     * @return 螢幕寬度值
     */
    fun getScreenWidth(): Int {
        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager

        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val windowMetrics = windowManager.currentWindowMetrics
            windowMetrics.bounds.width()
        } else {
            val metric = DisplayMetrics()
            @Suppress("DEPRECATION")
            windowManager.defaultDisplay.getMetrics(metric)
            metric.widthPixels
        }
    }

    /**
     * 取得螢幕高度
     * @return 螢幕高度值
     */
    fun getScreenHeight(): Int {
        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager

        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val windowMetrics = windowManager.currentWindowMetrics
            windowMetrics.bounds.height()
        } else {
            val metric = DisplayMetrics()
            @Suppress("DEPRECATION")
            windowManager.defaultDisplay.getMetrics(metric)
            metric.heightPixels
        }
    }

    /**
     * 取得狀態列高度
     * @return 狀態列高度值
     */
    fun getStatusBarHeight(): Int {
        var result = 0
        val resourceId: Int = context.resources.getIdentifier(
            "status_bar_height",
            "dimen",
            "android"
        )
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return result
    }

    /**
     * 取得導航列高度
     * @return 導航列高度值
     */
    fun getNavigationBarHeight(): Int {
        val resources = context.resources
        val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "android")
        return if (resourceId > 0) {
            resources.getDimensionPixelSize(resourceId)
        } else 0
    }

    /**
     * 取得視窗範圍高度
     * @return 視窗範圍高度值
     */
    fun getWindowHeight(): Int {
        val screen = getScreenHeight()
        val statusBar = getStatusBarHeight()
        val navBar = getNavigationBarHeight()
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            screen - statusBar - navBar
        } else {
            screen
        }
    }
}
