package com.common.bluetooth

import android.content.Context
import android.provider.Settings
import java.util.*

/**
 * BT静态方法
 *
 * @author wangym
 * @since 2021-10-14
 */
object BtConstants {
    const val BT_NAME = "innovation bt"

    /**
     * 蓝牙类型
     */
    enum class BLUETOOTH_TYPE {
        /**
         * 经典蓝牙
         */
        CLASSIC,

        /**
         * 低功耗蓝牙
         */
        BLE
    }

    /**
     * 蓝牙类型
     */
    enum class CLIENT_TYPE {
        /**
         * 服务端
         */
        SERVER,

        /**
         * 客户端
         */
        CLIENT
    }

    private const val STATUS_BASE = 0x1
    const val CONNECT_SUCCESS = STATUS_BASE shl 1
    const val CONNECT_ERROR = STATUS_BASE shl 2
    const val READ_SUCCESS = STATUS_BASE shl 3
    const val READ_ERROR = STATUS_BASE shl 4
    const val WRITE_SUCCESS = STATUS_BASE shl 5
    const val WRITE_ERROR = STATUS_BASE shl 6
    const val CREATE_SERVER_SUCCESS = STATUS_BASE shl 7
    const val CREATE_SERVER_ERROR = STATUS_BASE shl 8
    const val DISCONNECT = STATUS_BASE shl 9

    enum class EXCEPTION(s: String) {
        NOT_CONNECTED("not connect"),
        NULL_SERVICE("service is null"),
        NULL_CHARACTERISTIC("characteristic is null"),
        READ_EXCEPTION("characteristic read error"),
        WRITE_EXCEPTION("characteristic write error"),
        NOTIFY_OPEN_EXCEPTION("notification open error"),
        NOTIFY_CLOSE_EXCEPTION("notification close error")
    }

    // Message types sent from the BluetoothChatService Handler
    const val MESSAGE_STATE_CHANGE = 1
    const val MESSAGE_READ = 2
    const val MESSAGE_WRITE = 3
    const val MESSAGE_DEVICE_NAME = 4
    const val MESSAGE_TOAST = 5

    // Key names received from the BluetoothChatService Handler
    const val DEVICE_NAME = "device_name"
    const val TOAST = "toast"

    /**
     * 最大的单包发送字节数
     */
    const val MAX_MTU = 35

    val UUID_SERVICE = UUID.fromString("66f564dc-121f-3e7f-80b1-f005d3f194c9")
    val UUID_CHARACTERISTIC_NOTIFY = UUID.fromString("66f564dd-121f-3e7f-80b1-f005d3f194c9")
    val UUID_CHARACTERISTIC_WRITE = UUID.fromString("66f564de-121f-3e7f-80b1-f005d3f194c9")
    val UUID_DESCRIPTOR = UUID.fromString("66f564df-121f-3e7f-80b1-f005d3f194c9")

    /**
     * 获取UUID
     */
    fun getUUid(context: Context?): UUID {
        if (context == null) {
            return UUID.randomUUID()
        }
        val androidId =
            Settings.System.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
        return UUID.nameUUIDFromBytes(androidId.toByteArray())
    }
}