package com.xhly.corelib.utils

import android.bluetooth.BluetoothManager
import android.content.Context
import android.os.Build
import androidx.core.content.ContextCompat.getSystemService
import java.lang.reflect.Field
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method

class CustomOSUtils {
    companion object {
        /**
         * 获得所有属性
         */
        public fun getAllSign(): String {
            return Build::class.java.fields.joinToString("\n") {
                "Build.${it.name} = ${
                    it.get(
                        it.name
                    )
                }"
            }

        }

        /**
         * 获得系统版本
         */
        public fun getSystemDisplay(): String {
            return Build.DISPLAY
        }

        /**
         * 获得厂商
         */
        public fun getSystemBrand(): String {
            return Build.BRAND
        }

        /**
         * 获得手机型号
         */
        public fun getSystemDevice(): String {
            return Build.DEVICE
        }

        /**
         * 获取本机蓝牙地址, 确保蓝牙已开启，关闭状态无法获取到
         */
        public fun getBluetoothAddress(context: Context): String? {
            try {
                val bluetoothAdapter = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager).adapter
                val field: Field = bluetoothAdapter.javaClass.getDeclaredField("mService")
                field.isAccessible = true
                val bluetoothManagerService: Any = field.get(bluetoothAdapter) ?: return null
                val method: Method = bluetoothManagerService.javaClass.getMethod("getAddress")
                val address: Any? = method.invoke(bluetoothManagerService)
                return if (address != null && address is String) {
                    address
                } else {
                    null
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return null
        }
        //开启护眼模式
        public fun openOrCloseNightMode(context: Context, open: Boolean) {
            try {
                val cl: Class<*> = Class.forName("android.hardware.display.ColorDisplayManager")
                val control: Any =  context.getSystemService(cl)
                val md: Method = cl.getDeclaredMethod("setNightDisplayActivated", Boolean::class.javaPrimitiveType)
                md.invoke(control, open)
            } catch (e: Exception) {
                LogShow("ClassNotFoundException:" + e.message)
            }
        }
    }
}
