package com.xyzshell.andinfo.libs

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build

class BluetoothInfo(private val context: Context) {

    private val bluetoothManager: BluetoothManager? = context.getSystemService(
        BluetoothManager::class.java
    )

    @get:SuppressLint("MissingPermission")
    val bluetoothAdapter by lazy { bluetoothManager?.adapter }

    @get:SuppressLint("MissingPermission")
    val adapterName: String?
        get() = bluetoothAdapter?.name

    @get:SuppressLint("HardwareIds", "MissingPermission")
    val adapterMacAddress: String?
        get() = bluetoothAdapter?.address

    @get:SuppressLint("MissingPermission")
    val bondedDevices: List<Pair<String?, String>>
        get() = bluetoothAdapter?.bondedDevices?.map {
            Pair(it.name, it.address)
        } ?: emptyList()

    val isBluetoothLeSupported: Boolean
        get() = context.packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)

    val requiredPermissions: Array<String>
        get() = buildList {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                add(Manifest.permission.BLUETOOTH_CONNECT)
            } else {
                add(Manifest.permission.BLUETOOTH)
                add(Manifest.permission.BLUETOOTH_ADMIN)
            }
        }.toTypedArray()
}
