const bluetooth = require('../bluetooth.js');
const parse = require('./blueParse/LSBMS.js');
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 common = require('../common.js');
const app = getApp();
const MTU = 115;
var curTime = 0;
var isEndPackage = true;
var total_length = 0;
var functionCode = 0;
var functionList = [0, 0, 0, 0];
function acceptDevice(device) {
    return device.btid ? true : false;
}
function haveBms() {
    console.log('是单蓝牙并且带bms');
    return true;
}
function isSingleBt() {
    return true;
}
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() == [9, 255, 1, 2].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(1, [171, 205, 171, 205])];
}
function readData(device, value, data) {
    var value = new Uint8Array(value);
    console.log(value);
    let macid = device.mac_id;
    if (!isEndPackage) {
        var endTime = new Date().getTime();
        if (endTime - curTime < 800) {
            curTime = endTime;
            var bms_data = mergeUint8Array(app.globalData.deviceBTBMSBuf[macid].bms_origin_data, value);
            app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bms_data;
            if (bms_data.length === total_length) {
                app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bms_data.slice(0, -1);
                if (functionCode === 30) {
                    functionList.splice(0, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
                }
                if (functionCode === 32) {
                    functionList.splice(2, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
                }
                if (functionCode === 9) {
                    functionList.splice(3, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
                    console.log(functionList);
                }
                if (functionList[0] != 0 && functionList[1] != 0 && functionList[2] != 0 && functionList[3] != 0) {
                    data = loadDoneBmsInfo(functionList, macid);
                }
                isEndPackage = true;
                app.globalData.deviceBTBMSBuf[macid].bms_origin_data = null;
            }
        } else {
            isEndPackage = true;
        }
    }
    switch (value[0]) {
        case 59:
            {
                if (value[4] === 30) {
                    functionCode = 30;
                    curTime = new Date().getTime();
                    isEndPackage = false;
                    total_length = value[5] + 1;
                    var bmsData = value.slice(6);
                    if (bmsData.length === total_length) {
                        isEndPackage = true;
                    }
                    app.globalData.deviceBTBMSBuf[macid] = {
                        bms_origin_data: null
                    };
                    app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData;
                }
                if (value[4] === 31) {
                    functionCode = 31;
                    curTime = new Date().getTime();
                    isEndPackage = false;
                    total_length = value[5] + 1;
                    var bmsData = value.slice(6);
                    if (bmsData.length === total_length) {
                        isEndPackage = true;
                        app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData.slice(0, -1);
                        functionList.splice(1, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
                        if (functionList[0] != 0 && functionList[1] != 0 && functionList[2] != 0 && functionList[3] != 0) {
                            data = loadDoneBmsInfo(functionList, macid);
                        }
                        app.globalData.deviceBTBMSBuf[macid].bms_origin_data = null;
                    }
                    app.globalData.deviceBTBMSBuf[macid] = {
                        bms_origin_data: null
                    };
                    app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData;
                }
                if (value[4] === 32) {
                    functionCode = 32;
                    curTime = new Date().getTime();
                    isEndPackage = false;
                    total_length = value[5] + 1;
                    var bmsData = value.slice(6);
                    if (bmsData.length === total_length) {
                        isEndPackage = true;
                        app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData.slice(0, -1);
                        functionList.splice(2, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
                        if (functionList[0] != 0 && functionList[1] != 0 && functionList[2] != 0 && functionList[3] != 0) {
                            data = loadDoneBmsInfo(functionList, macid);
                        }
                        app.globalData.deviceBTBMSBuf[macid].bms_origin_data = null;
                    }
                    app.globalData.deviceBTBMSBuf[macid] = {
                        bms_origin_data: null
                    };
                    app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData;
                }
                if (value[4] === 9) {
                    console.log('xxxxx');
                    functionCode = 9;
                    curTime = new Date().getTime();
                    isEndPackage = false;
                    total_length = value[5] + 1;
                    var bmsData = value.slice(6);
                    if (bmsData.length === total_length) {
                        console.log('09功能进来了');
                        isEndPackage = true;
                        app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData.slice(0, -1);
                        functionList.splice(3, 1, app.globalData.deviceBTBMSBuf[macid].bms_origin_data);
                        console.log('functionList');
                        console.log(functionList);
                        if (functionList[0] != 0 && functionList[1] != 0 && functionList[2] != 0 && functionList[3] != 0) {
                            data = loadDoneBmsInfo(functionList, macid);
                            console.log(data);
                        }
                        app.globalData.deviceBTBMSBuf[macid].bms_origin_data = null;
                    }
                    console.log('bmsDatabmsDatabmsData');
                    console.log(bmsData);
                    app.globalData.deviceBTBMSBuf[macid] = {
                        bms_origin_data: null
                    };
                    app.globalData.deviceBTBMSBuf[macid].bms_origin_data = bmsData;
                }
            }
            break;
    }
    return data;
}
function loadDoneBmsInfo(functionList, macid) {
    var data = parse.parseBmsData(functionList[0], functionList[1], functionList[2]);
    common.reportBluetooth(macid, functionList[0], functionList[1], functionList[2], functionList[3]);
    data.voltage = data.state.voltageAll;
    data.quantity = data.state.soc;
    data.soh = data.state.soh;
    data.alarm = data.state.alarm;
    data.temp = data.state.temp.length != 0 ? data.state.temp[0] : 0;
    console.log(data);
    return data;
}
function stateUpdate(device, deviceId) {
    console.log('读电压');
    return [
        [58, 126, 1, 1, 30, 0, 216],
        [58, 126, 1, 1, 31, 0, 217],
        [58, 126, 1, 1, 32, 0, 218],
        [58, 126, 1, 1, 9, 0, 195]
    ];
}
function turnOn(device, deviceId) {
    console.log('通电');
    //return [sendCommand(0x03, [0xaa])]
    return [[58, 126, 1, 0, 60, 4, 255, 1, 255, 255, 247]];
}
function turnOff(device, deviceId) {
    console.log('断电');
    //return [sendCommand(0x03, [0xbb])]
    return [[58, 126, 1, 0, 60, 4, 255, 0, 255, 255, 246]];
}
function bmsChargingMOS(value) {
    if (value == 1) {
        return [[58, 126, 1, 0, 60, 4, 255, 255, 1, 255, 247]];
    } else {
        return [[58, 126, 1, 0, 60, 4, 255, 255, 0, 255, 246]];
    }
}
function bmsDischargeMOS(value, device) {
    if (value == 1) {
        return [[58, 126, 1, 0, 60, 4, 255, 1, 255, 255, 247]];
    } else {
        console.log('xxxxxx44');
        return [[58, 126, 1, 0, 60, 4, 255, 0, 255, 255, 246]];
    }
}
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 bmsInfo(){

// }

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