const common = require('../common.js');

const app = getApp();
const readServiceID = '0000FEE7-0000-1000-8000-00805F9B34FB';
const readID = '000036F6-0000-1000-8000-00805F9B34FB';
const writeServiceID = '0000FEE7-0000-1000-8000-00805F9B34FB';
const writeID = '000036F5-0000-1000-8000-00805F9B34FB';
const MTU = 128;
var joinPack = [];
var lastLength = 0;
var curTime = 0;
var stateRepeatTime = 0;
var readRepeatTime = 0;

function acceptDevice(device) {
    return device.btid ? true : false;
}

function isSingleBt() {
    return true;
}

function isBuzzer() {
    return true;
}

function haveBms() {
    console.log('是单蓝牙并且带bms');
    return false;
}

function isDevice(device, data) {
    const advertisData = new Uint8Array(data.advertisData);
    const mac = device.btid
        .split('')
        .map((p, i) => parseInt(p + device.btid[i + 1], 16))
        .filter((p, i) => i % 2 == 0);

    if (advertisData.slice(0, 4).toString() == [0x09, 0xff, 0x01, 0x02].toString() && advertisData.slice(4, 10).toString() == mac.toString()) {
        return true;
    }

    return false;
}

function sendCommand(cmd, data = []) {
    data = [cmd, data.length].concat(data);
    return common.completArrayCRC(data, data.length);
} // 鉴权

function alterConnect(device, deviceId) {
    return [sendCommand(0x01, [0xab, 0xcd, 0xab, 0xcd])];
}

function readData(device, value, data) {
    var value = new Uint8Array(value);

    switch (value[0]) {
        case 0x48:
            if (value.length == 7) {
                data.voltage = value[2] * 0x100 + value[3] * 1;

                if (value[4] == 0x80) {
                    data.temp = -value[5];
                } else {
                    data.temp = value[5];
                }
            }

            break;

        case 0x42:
            break;
    }

    if (value[0] != 0x48 && value.length != 7) return;
    return data;
}

function sendQVCommand(data) {
    data = [0x06, data.length].concat(data);
    return common.completArrayCRC(data, data.length);
}

function sendDataCommand(data) {
    data = [0x02, data.length, (data.length & 0xff00) >> 8, data.length & 0x00ff, 0].concat(data);
    return common.completArrayCRC(data, data.length);
}

function mergeUint8Array(arr1, arr2) {
    let len1 = arr1 ? arr1.length : 0;
    let len2 = arr2.length;
    let arr = new Uint8Array(len1 + len2);

    for (let i = 0; i < len1; i++) {
        arr[i] = arr1[i];
    }

    for (let i = 0; i < len2; i++) {
        arr[len1 + i] = arr2[i];
    }

    return arr;
}

function voltageToEle(device, value) {
    var commandList = [];
    var list = hexToList(value);
    commandList.push(sendQVCommand(list));
    return commandList;
}

function stateUpdate(device, deviceId) {
    return [[0x40, 0x00, 0x40]];
}

function turnOn(device, deviceId) {
    return [sendCommand(0x30, [0xaa])];
}

function turnOff(device, deviceId) {
    return [sendCommand(0x30, [0xbb])];
}

function turnOnBuzzer(device, deviceId) {
    return [sendCommand(0x50, [0xaa])];
}

function turnOffBuzzer(device, deviceId) {
    return [sendCommand(0x50, [0xbb])];
}

function bmsDischargeMOS(value, device) {
    if (value == 1) {
        const bluetoothConfig = app.globalData.bluetoothConfig;

        if (Object.keys(bluetoothConfig).includes(device.device_type)) {
            var turnonCommand = bluetoothConfig[device.device_type].turnon;
            var commandList = [];
            var list = hexToList(turnonCommand);
            commandList.push(sendDataCommand(list));
            return commandList;
        }
    } else {
        const bluetoothConfig = app.globalData.bluetoothConfig;

        if (Object.keys(bluetoothConfig).includes(device.device_type)) {
            var turnoffCommand = bluetoothConfig[device.device_type].turnoff;
            var commandList = [];
            var list = hexToList(turnoffCommand);
            commandList.push(sendDataCommand(list));
            return commandList;
        }
    }
}

function bmsChargingMOS(value, device) {
    if (value == 1) {
        const bluetoothConfig = app.globalData.bluetoothConfig;

        if (Object.keys(bluetoothConfig).includes(device.device_type)) {
            var chargingMosOn = bluetoothConfig[device.device_type].chargingMosOn;
            var commandList = [];
            var list = hexToList(chargingMosOn);
            commandList.push(sendDataCommand(list));
            return commandList;
        }
    } else {
        const bluetoothConfig = app.globalData.bluetoothConfig;

        if (Object.keys(bluetoothConfig).includes(device.device_type)) {
            var chargingMosOff = bluetoothConfig[device.device_type].chargingMosOff;
            var commandList = [];
            var list = hexToList(chargingMosOff);
            commandList.push(sendDataCommand(list));
            return commandList;
        }
    }
}

function hexToList(str) {
    var val = [];

    for (var i = 0; i < str.length / 2; i++) {
        val.push(parseInt(str.substring(0 + i * 2, 2 + i * 2), 16));
    }

    return val;
}

module.exports = {
    readServiceID: readServiceID,
    readID: readID,
    writeServiceID: writeServiceID,
    writeID: writeID,
    MTU: MTU,
    acceptDevice: acceptDevice,
    isDevice: isDevice,
    alterConnect: alterConnect,
    readData: readData,
    stateUpdate: stateUpdate,
    turnOn: turnOn,
    turnOff: turnOff,
    turnOnBuzzer: turnOnBuzzer,
    turnOffBuzzer: turnOffBuzzer,
    isSingleBt: isSingleBt,
    haveBms: haveBms,
    bmsDischargeMOS: bmsDischargeMOS,
    bmsChargingMOS: bmsChargingMOS,
    voltageToEle: voltageToEle,
    isBuzzer: isBuzzer
};
