package aicare.net.cn.sdk.ailinksdkdemoandroid.utils;

import com.pingwang.bluetoothlib.AILinkBleManager;
import com.pingwang.bluetoothlib.bean.BleScanAddAilinkBean;
import com.pingwang.bluetoothlib.config.BleConfig;
import com.pingwang.bluetoothlib.server.ELinkBleServer;
import com.pingwang.bluetoothlib.utils.UuidUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 蓝牙扫描设备工具
 * 因为 AILink 现在接了不是 AILink 协议的设备，比如旧的营养秤 UUID 不同
 */
public class BleScanUtil {

    /**
     * 开始扫描设备
     *
     * @param blueToothService 蓝牙服务
     * @param timeout          延迟 ms
     */
    public static void scanLeDevice(ELinkBleServer blueToothService, long timeout) {
        blueToothService.scanLeDevice(timeout, getScanLeDeviceMap());
    }

    public static void scanLeDevice(AILinkBleManager aiLinkBleManager, long timeout) {
        aiLinkBleManager.startScan(timeout, getScanLeDeviceMap());
    }


    public static List<BleScanAddAilinkBean> getScanLeDeviceMap() {
        List<BleScanAddAilinkBean> list = new ArrayList<>();
        list.add(getAifresh());
        list.add(getAifit02());
        list.add(getAifit03());
        list.add(getOkOk());
        return list;
    }

    public static UUID[] getScanLeDeviceUUID() {
        UUID[] uuids = new UUID[]{UUID.fromString("0000FFB0-0000-1000-8000-00805F9B34FB"),// 旧的营养秤 AC05
//                UUID.fromString("0000FEE7-0000-1000-8000-00805F9B34FB"),// 华盛达手表
                BleConfig.UUID_SERVER_AILINK, BleConfig.UUID_SERVER_BROADCAST_AILINK};
        return uuids;
    }


    /**
     * 好营养AC05的设备
     *
     * @return {@link BleScanAddAilinkBean}
     */
    private static BleScanAddAilinkBean getAifresh() {
        // 旧的营养秤
        BleScanAddAilinkBean bleScanAddAilinkBean = new BleScanAddAilinkBean();
        bleScanAddAilinkBean.setDeviceServiceUUID(new UUID[]{UuidUtils.getUuid("0000FFB0-0000-1000-8000-00805F9B34FB")});
        byte[] bytes = new byte[]{(byte) 0xAC, 0x05};
        bleScanAddAilinkBean.setDeviceBroadcast(bytes, 0, bytes.length);
        bleScanAddAilinkBean.setCid(0x0037);
        bleScanAddAilinkBean.setVid(0x0000);
        bleScanAddAilinkBean.setPid(0x0000);
        return bleScanAddAilinkBean;
    }

    /**
     * 好身材AC02的体脂秤,带温度
     *
     * @return {@link BleScanAddAilinkBean}
     */
    private static BleScanAddAilinkBean getAifit02() {
        // 旧的好身材
        BleScanAddAilinkBean bleScanAddAilinkBean = new BleScanAddAilinkBean();
        bleScanAddAilinkBean.setDeviceServiceUUID(new UUID[]{UuidUtils.getUuid("0000FFB0-0000-1000-8000-00805F9B34FB")});
        byte[] bytes = new byte[]{(byte) 0xAC, 0x02};
        bleScanAddAilinkBean.setDeviceBroadcast(bytes, 0, bytes.length);
        bleScanAddAilinkBean.setCid(0x005E);
        bleScanAddAilinkBean.setVid(0x0070);
        bleScanAddAilinkBean.setPid(0x0001);
        return bleScanAddAilinkBean;
    }

    /**
     * 好身材AC03的体脂秤,带温度
     *
     * @return {@link BleScanAddAilinkBean}
     */
    private static BleScanAddAilinkBean getAifit03() {
        // 旧的好身材
        BleScanAddAilinkBean bleScanAddAilinkBean = new BleScanAddAilinkBean();
        bleScanAddAilinkBean.setDeviceServiceUUID(new UUID[]{UuidUtils.getUuid("0000FFB0-0000-1000-8000-00805F9B34FB")});
        byte[] bytes = new byte[]{(byte) 0xAC, 0x03};
        bleScanAddAilinkBean.setDeviceBroadcast(bytes, 0, bytes.length);
        bleScanAddAilinkBean.setCid(0x005E);
        bleScanAddAilinkBean.setVid(0x0070);
        bleScanAddAilinkBean.setPid(0x0001);
        return bleScanAddAilinkBean;
    }

    private static BleScanAddAilinkBean getOkOk() {
        // OKOK广播秤
        BleScanAddAilinkBean bleScanAddAilinkBean = new BleScanAddAilinkBean();
        bleScanAddAilinkBean.setDeviceServiceUUID(null);
        byte[] bytes = new byte[]{(byte) 0x00, 0x00};
        bleScanAddAilinkBean.setDeviceBroadcast(bytes, 6, 6+bytes.length);
        bleScanAddAilinkBean.setCid(0x08+0xFFFF);
        bleScanAddAilinkBean.setVid(0x0001);
        bleScanAddAilinkBean.setPid(0x0002);
        return bleScanAddAilinkBean;
    }

}
