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

import llc.arma.ble.domain.model.Ble
import llc.arma.ble.domain.usecase.AccelScale
import llc.arma.ble.domain.usecase.AccelViewMode
import llc.arma.ble.domain.usecase.FftAxis
import llc.arma.ble.domain.usecase.FftFrequency
import llc.arma.ble.domain.usecase.FftViewMode

fun Ble.BleState.TX.Companion.fromByte(byte: Byte): Ble.BleState.TX? {
    return Ble.BleState.TX.values().associateBy { it.sendData }[byte]
}

val Ble.BleState.TX.sendData: Byte
    get() {
        return when (this) {
            Ble.BleState.TX.MINUS_40 -> -40
            Ble.BleState.TX.MINUS_20 -> -20
            Ble.BleState.TX.MINUS_16 -> -16
            Ble.BleState.TX.MINUS_12 -> -12
            Ble.BleState.TX.MINUS_8 -> -8
            Ble.BleState.TX.MINUS_4 -> -4
            Ble.BleState.TX.ZERO -> 0
            Ble.BleState.TX.PLUS_3 -> 3
            Ble.BleState.TX.PLUS_4 -> 4
        }
    }

val FftFrequency.sendData: Byte
    get() {
        return when(this){
            FftFrequency.OFF -> 0
            FftFrequency.F_1 -> 1
            FftFrequency.F_10 -> 2
            FftFrequency.F_25 -> 3
            FftFrequency.F_50 -> 4
            FftFrequency.F_100 -> 5
            FftFrequency.F_200 -> 6
            FftFrequency.F_400 -> 7
            FftFrequency.F_1620 -> 8
            FftFrequency.F_1344 -> 9
        }
    }

val FftAxis.sendData: Byte
    get() {
        return when(this){
            FftAxis.AUTO -> 0
            FftAxis.X -> 1
            FftAxis.Y -> 2
            FftAxis.Z -> 3
        }
    }

val FftViewMode.sendData: Byte
    get() {
        return when(this){
            FftViewMode.SPECTRE -> 0
            FftViewMode.X -> 1
            FftViewMode.Y -> 2
            FftViewMode.Z -> 3
        }
    }

fun AccelViewMode.Companion.fromByte(byte: Byte): AccelViewMode? {
    return AccelViewMode.values().associateBy { it.sendData }[byte]
}

val AccelViewMode.sendData: Byte
    get() {
        return when(this){
            AccelViewMode.ACCELERATION -> 0
            AccelViewMode.PEAK_ACCELERATION -> 1
            AccelViewMode.RMS -> 2
            AccelViewMode.VIBRATION -> 3
            AccelViewMode.ANGLE -> 4
            AccelViewMode.ROTATIONS -> 5
        }
    }

fun AccelScale.Companion.fromByte(byte: Byte): AccelScale? {
    return AccelScale.values().associateBy { it.sendData }[byte]
}

val AccelScale.sendData: Byte
    get() {
        return when(this){
            AccelScale.S_2 -> 0
            AccelScale.S_4 -> 1
            AccelScale.S_8 -> 2
            AccelScale.S_16 -> 3
        }
    }