package xn.hxp.utils.bluetooth;

import android.bluetooth.BluetoothDevice;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.feasycom.common.bean.ConnectType;
import com.feasycom.spp.controler.FscSppCentralApi;
import com.feasycom.spp.controler.FscSppCentralApiImp;
import com.feasycom.spp.controler.FscSppCentralCallbacks;

/**
 * https://document.feasycom.com/docs/app/sdk/FeasycomSDK/CN/latest/FeasyBlue_Android.html
 * 蓝牙操作SDK 适用于乐琪称
 */
public enum SppTool {
    INSTANCE;

    private FscSppCentralApi sppApi;

    public void init() {
        sppApi = FscSppCentralApiImp.getInstance(Utils.getApp());
        sppApi.isShowLog(true);
        sppApi.initialize();
    }

    public interface BleSppCallback {
        void sppPeripheralConnected(BluetoothDevice bluetoothDevice, ConnectType connectType);

        void packetReceived(String address, String strValue);

        void sppPeripheralDisconnected(String address);
    }

    public void sacn(FscSppCentralCallbacks fscSppCentralCallbacks) {
        sppApi.setCallbacks(fscSppCentralCallbacks);
        sppApi.startScan();
    }

    public void connect(String mac, BleSppCallback bleSppCallback) {
        if (null != mac) {
            sppApi.setCallbacks(new FscSppCentralCallbacks() {
                @Override
                public void sppPeripheralConnected(BluetoothDevice bluetoothDevice, ConnectType connectType) {
                    LogUtils.d(bluetoothDevice, connectType);
                    bleSppCallback.sppPeripheralConnected(bluetoothDevice, connectType);
                }

                /**
                 * 收到数据
                 * @param address 设备地址
                 * @param strValue 字符串
                 * @param dataHexString 十六进制
                 * @param data 源数据
                 */
                @Override
                public void packetReceived(String address, String strValue, String dataHexString, byte[] data) {
                    LogUtils.d("蓝牙数据", strValue);
                    strValue = strValue.replace(" ", "");
                    strValue = strValue.replace("\r", "");
                    strValue = strValue.replace("\n", "");
                    strValue = strValue.trim();
                    if (strValue.startsWith("=")) {
                        try {
                            String wD = new StringBuilder(strValue.replace("=", "")).reverse().toString();
                            bleSppCallback.packetReceived(address, wD);
                        } catch (Exception e) {
                            LogUtils.e(e.getMessage());
                        }
                    } else {
                        bleSppCallback.packetReceived(address, strValue);
                    }
                }

                /**
                 * 断开连接
                 */
                @Override
                public void sppPeripheralDisconnected(String address) {
                    LogUtils.d("sppPeripheralDisconnected", address);
                    bleSppCallback.sppPeripheralDisconnected(address);
                }
            });
            sppApi.connect(mac);
        }
    }

    public void disconnect() {
        sppApi.disconnect();
    }
}
