package com.common.bluetooth.service

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.pm.PackageManager
import android.util.Log
import com.common.bluetooth.BtConstants.BLUETOOTH_TYPE
import com.common.bluetooth.interfaces.IBluetoothSearch
import com.common.bluetooth.service.ble.BtBleSearcher
import com.common.bluetooth.service.bt.BluetoothClassicBase
import com.common.bluetooth.service.bt.BtClassicSearcher

/**
 * 通用蓝牙
 *
 * @author wangym
 * @since 2021-12-1
 */
open class BluetoothCommon(val mContext: Context) {
    companion object {
        const val TAG = "BluetoothCommon"
    }

    var mBluetoothAdapter: BluetoothAdapter? = null
    var mBluetoothManager: BluetoothManager? = null
    var mBluetoothSearcher: IBluetoothSearch? = null

    /**
     * 检查蓝牙设备是否支持
     *
     * @param btType 蓝牙类型
     */
    fun checkBtDevice(btType: BLUETOOTH_TYPE): Boolean {
        if (btType === BLUETOOTH_TYPE.BLE) {
            // 检查当前手机是否支持ble 蓝牙
            if (!mContext.packageManager
                    .hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)
            ) {
                Log.e(TAG, "not support ble device")
                return false
            }
        }

        // For API level 18 and above, get a reference to BluetoothAdapter
        // through BluetoothManager.
        if (mBluetoothManager == null) {
            mBluetoothManager = mContext
                .getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
            if (mBluetoothManager == null) {
                Log.e(TAG, "Unable to initialize BluetoothManager.")
                return false
            }
        }
        if (mBluetoothAdapter == null) {
            mBluetoothAdapter = mBluetoothManager?.adapter
            if (mBluetoothAdapter == null) {
                Log.e(TAG, "Unable to obtain a BluetoothAdapter.")
                return false
            }
        }
        return true
    }

    /**
     * 通过MAC地址获取设备对象
     *
     * @param mac mac地址
     * @return 设备对象
     */
    fun getBtDeviceByMac(mac: String?): BluetoothDevice? {
        if (mBluetoothAdapter == null) {
            Log.e(TAG, "getBtDeviceByMac mBluetoothAdapter is null")
            return null
        }
        return mBluetoothAdapter!!.getRemoteDevice(mac)
    }

    /**
     * 初始化BluetoothAdapter
     * Initializes a reference to the local Bluetooth adapter.
     *
     * @return Return true if the initialization is successful.
     */
    fun openBt(): Boolean {
        if (mBluetoothAdapter == null) {
            Log.e(TAG, "BluetoothManager do not init")
            return false
        }
        return mBluetoothAdapter!!.isEnabled || mBluetoothAdapter!!.enable()
    }

    fun closeBt(): Boolean {
        if (mBluetoothAdapter == null) {
            Log.e(TAG, "BluetoothManager do not init")
            return false
        }
        return mBluetoothAdapter!!.disable()
    }

    /**
     * 获取已配对蓝牙设备信息
     */
    fun getBondedDevices(): Set<BluetoothDevice>? {
        return mBluetoothAdapter?.bondedDevices
    }

    /**
     * 获取蓝牙扫描对象
     *
     * @return 蓝牙扫描对象
     */
    fun getBluetoothSearcher(btType: BLUETOOTH_TYPE): IBluetoothSearch {
        if (mBluetoothSearcher == null) {
            synchronized(BluetoothClassicBase::class.java) {
                if (mBluetoothSearcher == null) {
                    mBluetoothSearcher = if (btType == BLUETOOTH_TYPE.BLE) {
                        BtBleSearcher(mContext, mBluetoothAdapter)
                    } else {
                        BtClassicSearcher(mContext, mBluetoothAdapter)
                    }
                }
            }
        }
        return mBluetoothSearcher!!
    }
}