const readServiceID = '0000FFE0-0000-1000-8000-00805F9B34FB';
const readID = '0000FFE1-0000-1000-8000-00805F9B34FB';
const writeServiceID = '0000FFE0-0000-1000-8000-00805F9B34FB';
const writeID = '0000FFE1-0000-1000-8000-00805F9B34FB';
const readDataMethod = {
    255: {
        length: 6,
        read: readDataFF
    }
};
var readState = {
    value: null,
    index: 0
};
function acceptDevice(device) {
    return device.btid ? true : 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(2, 4).toString() == [136, 160].toString() && advertisData.slice(4, 10).toString() == mac.toString()) {
        return true;
    }
    return false;
}
function readDataFF(value, index, data) {
    switch (index) {
        case 0:
            // B0 - B19
            data.count = value[1];
            if (!data.voltageList) {
                data.voltageList = [];
            }
            for (var i = 0; i < 9; i++) {
                data.voltageList[i] = Math.round(value[(i + 1) * 2] + value[(i + 1) * 2 + 1] * 256) / 1000;
            }
            break;
        case 1:
            // B20 - B39
            if (!data.voltageList) {
                data.voltageList = [];
            }
            for (var i = 9; i < 19; i++) {
                data.voltageList[i] = Math.round(value[(i - 9) * 2] + value[(i - 9) * 2 + 1] * 256) / 1000;
            }
            break;
        case 2:
            // B40 - B59
            if (!data.voltageList) {
                data.voltageList = [];
            }
            for (var i = 19; i < 24; i++) {
                data.voltageList[i] = Math.round(value[(i - 19) * 2] + value[(i - 19) * 2 + 1] * 256) / 1000;
            }
            data.chargeState = value[10] + 1;
            data.current = Math.round(value[11] + value[12] * 256) / 10;
            data.voltage = Math.round(value[13] + value[14] * 256) / 100;
            data.quantity = value[15];
            data.chargeCurrentCountState = (value[18] >> 0) & 1;
            data.dischargeProtectCharacteristic = (value[18] >> 1) & 1;
            data.equilibrium = (value[19] >> 0) & 1;
            data.disconnectState = (value[19] >> 1) & 1;
            data.dischargeProtectState = (value[19] >> 2) & 1;
            data.chargeProtectState = (value[19] >> 3) & 1;
            data.highTempProtectState = (value[19] >> 4) & 3;
            data.lowTempProtectState = (value[19] >> 6) & 1;
            data.highVoltageDifferenceState = (value[19] >> 7) & 1;
            break;
        case 3:
            // B60 - B79
            data.alarmState = [];
            if (((value[0] >> 0) & 1) == 1) {
                data.alarmState.push('低容量报警');
            }
            if (((value[0] >> 1) & 1) == 1) {
                data.alarmState.push('功率板超温报警');
            }
            if (((value[0] >> 2) & 1) == 1) {
                data.alarmState.push('充电过流报警');
            }
            if (((value[0] >> 3) & 1) == 1) {
                data.alarmState.push('放电过流报警');
            }
            data.faultState = [];
            if (((value[0] >> 4) & 1) == 1) {
                data.alarmState.push('充电MOS管故障');
            }
            if (((value[0] >> 5) & 1) == 1) {
                data.alarmState.push('放电MOS管故障');
            }
            data.chargingMOS = (value[0] >> 6) & 1;
            data.dischargeMOS = (value[0] >> 7) & 1;
            data.cycle = value[1] + value[2] * 256;
            data.tempCount = value[10];
            data.powerBoardTemp = value[11] - 40;
            data.equilibriumBoardTemp = value[12] - 40;
            if (!data.tempList) {
                data.tempList = [];
            }
            for (var i = 0; i < 7; i++) {
                data.tempList[i] = value[13 + i] - 40;
            }
            break;
        case 4:
            // B80 - B99
            data.capacity = Math.round(value[0] + value[1] * 256) / 100;
            data.dischargeCapacity = Math.round(value[2] + value[3] * 256 + value[4] * 65536 + value[5] * 16777216) / 100;
            break;
        case 5:
            // B100 - B120
            if (!data.voltageList) {
                data.voltageList = [];
            }
            for (var i = 24; i < 32; i++) {
                data.voltageList[i] = Math.round(value[(i - 24) * 2] + value[(i - 24) * 2 + 1] * 256) / 1000;
            }
            break;
    }
    console.log(data);
    return data;
}
function readData(device, value, data) {
    var value = Array.from(new Uint8Array(value));
    console.log(value);
    if (readState.value == null) {
        if (readDataMethod[value[0]]) {
            readState = {
                value: value[0],
                index: 1
            };
            return readDataMethod[value[0]].read(value, 0, data);
        }
    } else {
        const readStateValue = readState.value;
        const readStateIndex = readState.index;
        readState.index++;
        if (readState.index >= readDataMethod[readState.value].length) {
            readState = {
                value: null,
                index: 0
            };
        }
        return readDataMethod[readStateValue].read(value, readStateIndex, data);
    }
    return false;
}
function stateUpdate(device, deviceId) {
    var data = [
        [178, 233, 209, 175, 202, 253, 190, 221],
        [187, 241, 200, 161, 197, 228, 214, 195]
    ];
    return data;
}
function turnOn(device, deviceId) {
    var data = [
        [195, 220, 194, 235, 209, 233, 214, 164, 163, 186, 2, 0, 0, 4, 1, 3, 237, 167],
        [1, 6, 0, 216, 0, 1, 200, 49]
    ];
    return data;
}
function turnOff(device, deviceId) {
    var data = [
        [195, 220, 194, 235, 209, 233, 214, 164, 163, 186, 2, 0, 0, 4, 1, 3, 237, 167],
        [1, 6, 0, 216, 0, 0, 9, 241]
    ];
    return data;
}
function bmsInfo(device, deviceId, info) {
    return false;
}
function bmsSet(device) {
    return false;
}
module.exports = {
    readServiceID: readServiceID,
    readID: readID,
    writeServiceID: writeServiceID,
    writeID: writeID,
    acceptDevice: acceptDevice,
    isDevice: isDevice,
    readData: readData,
    stateUpdate: stateUpdate,
    turnOn: turnOn,
    turnOff: turnOff,
    bmsInfo: bmsInfo,
    bmsSet: bmsSet
};
