package llc.arma.ble.data.repository.extensions

import llc.arma.ble.domain.model.BleInfo
import no.nordicsemi.android.kotlin.ble.core.scanner.BleScanResult

val BleScanResult.timerEnabled: BleInfo.HistoryTableStatus
    get() {
        return when(data?.scanRecord?.manufacturerSpecificData?.get(89)?.getByte(2)){
            1.toByte() -> BleInfo.HistoryTableStatus.NOT_EMPTY
            2.toByte() -> BleInfo.HistoryTableStatus.EMPTY
            else -> BleInfo.HistoryTableStatus.DISABLED
        }
    }

val BleScanResult.info: BleInfo
    get() {
        this.device.name
        return BleInfo(
            name = this.device.name ?: "",
            serial = device.address,
            batteryLevel = batteryLevel ?: 0,
            rssi = data?.rssi,
            type = type,
            scanTime = (data?.timestampNanos ?: 0) / 1_000_000,
            tx = data?.scanRecord?.txPowerLevel ?: 0,
            tableStatus = timerEnabled
        )
    }

val BleScanResult.batteryLevel: Int?
    get() {
        return data?.scanRecord?.manufacturerSpecificData?.get(89)?.getByte(1)
            ?.toUByte()?.toInt()
    }

val BleScanResult.type: BleInfo.Type
    get() {
        return when(data?.scanRecord?.manufacturerSpecificData?.get(89)?.getByte(0)?.toUByte()?.toInt()){
            4 -> BleInfo.Type.HOST
            1 -> BleInfo.Type.BEACON
            2 -> BleInfo.Type.THERMOMETER
            else -> BleInfo.Type.ACCELEROMETER
        }
    }

