const common = require('../../../common.js'); //确认信息

function confirmCommand(data, serialNum, key) {
    //标志体
    var toBody = confirmBody(serialNum, key); //标志头

    var toHead = [0x7a, toBody.length + 2].concat(data); //标志尾

    var toFoot = getMarkFoot(toBody, data);
    var body = toHead.concat(toBody).concat(toFoot);
    return body;
}

function togetherCommand(data, device) {
    console.log('设备信息');
    console.log(device); //标志体

    var toBody = []; //下发换电

    if (data[0] === 0x01 && data[1] === 0x01) {
        toBody = loadToMsgBody(device);
    }

    if (data[0] === 0x02 && data[1] === 0x01) {
        toBody = loadToMsgBody2(device);
    }

    if (data[0] === 0x03 && data[1] === 0x01) {
        toBody = loadToMsgBody3(device);
    } //标志头

    var toHead = [0x7a, toBody.length + 2].concat(data); //标志尾

    var toFoot = getMarkFoot(toBody, data);
    var body = toHead.concat(toBody).concat(toFoot);
    console.log('下发的机柜指令--');
    console.log(body);
    return body;
} //标志尾

function getMarkFoot(toBody, hexList) {
    var total = 0;

    for (var i = 0; toBody.length > i; i++) {
        total += toBody[i];
    }

    for (var i = 0; hexList.length > i; i++) {
        total += hexList[i];
    }

    total = toBody.length + 2 + total;
    var toFoot = [total & 0x00ff, 0x78];
    return toFoot;
} //标志体-确认

function confirmBody(serialNum, key) {
    var serialLen = 0;
    var serialNo = 0;

    if (serialNum !== '0') {
        //序列号长度
        var serialLen = serialNum.length; //序列号

        var serialNo = common.getASCIICode(serialNum);
    }

    var keyList = keyHex(key); //密钥长度 密钥索引 密钥值

    var toBody;

    if (serialNum !== '0') {
        toBody = contactList([[serialLen], serialNo]).concat(keyList);
    } else {
        toBody = [serialLen].concat(keyList);
    } //拼接

    return toBody;
} //换电标志体

function loadToMsgBody(device) {
    //空仓门编号
    var empityBoxNo = parseInt(device.empityBoxNo, 16); //满仓门编号

    var fullBoxNo = parseInt(device.fullBoxNo, 16);
    var boxNo = [empityBoxNo].concat([fullBoxNo]); //电池编号长度

    var batteryNoLen = device.battertNum.length; //电池编号

    var batteryNo = common.getASCIICode(device.battertNum); //订单号长度

    var orderNoLen = device.orderNo.length; //订单号

    var orderNo = common.getASCIICode(device.orderNo); //序列号长度

    var serialLen = device.serialNum.length; //序列号

    var serialNo = common.getASCIICode(device.serialNum); //得到密钥

    var keyList = keyHex(device.key);
    var toBody = contactList([boxNo, [batteryNoLen], batteryNo, [orderNoLen], orderNo, [serialLen], serialNo]).concat(keyList);
    return toBody;
} //租电标志体

function loadToMsgBody2(device) {
    //空仓门编号
    var boxNo = parseInt(device.boxNo, 16); //序列号长度

    var serialLen = device.serialNum.length; //序列号

    var serialNo = common.getASCIICode(device.serialNum); //订单号长度

    var orderNoLen = device.orderNo.length; //订单号

    var orderNo = common.getASCIICode(device.orderNo); //得到密钥

    var keyList = keyHex(device.key);
    var toBody = contactList([[boxNo], [serialLen], serialNo, [orderNoLen], orderNo]).concat(keyList);
    return toBody;
} //还电标志体

function loadToMsgBody3(device) {
    //还电仓门编号
    var boxNo = parseInt(device.boxNo, 16); //电池编号长度

    var batteryNoLen = device.battertNum.length; //电池编号

    var batteryNo = common.getASCIICode(device.battertNum); //序列号长度

    var serialLen = device.serialNum.length; //序列号

    var serialNo = common.getASCIICode(device.serialNum); //订单号长度

    var orderNoLen = device.orderNo.length; //订单号

    var orderNo = common.getASCIICode(device.orderNo); //得到密钥

    var keyList = keyHex(device.key);
    console.log(keyList);
    var toBody = contactList([[boxNo], [batteryNoLen], batteryNo, [serialLen], serialNo, [orderNoLen], orderNo]).concat(keyList);
    return toBody;
} //生成密钥

function keyHex(key) {
    //密钥索引
    var keyIndexHex = '00000002'; //由于随机太长导致机柜出现bug暂用00000002

    var keyIndex = hexToList(keyIndexHex); //密钥值

    var binaryKeyIndex = binaryArray(parseInt(keyIndexHex, 16));
    var keyValue = getEncryptionKey(binaryKeyIndex, key);
    var keyValueASCII = common.getASCIICode(keyValue); //秘钥验证数据长度

    var keyVerifyLen = 4 + keyValue.length; //密钥索引4字节+秘钥值长度

    return contactList([[keyVerifyLen], keyIndex, keyValueASCII]);
} //获取32位2进制数据

function binaryArray(b) {
    var array = [];

    for (var i = 31; i >= 0; i--) {
        array[i] = b & 1;
        b = b >> 1;
    }

    return array;
} //随机8位16进制

function randomHexNumber() {
    var result = '';

    for (var i = 0; i < 8; i++) {
        result += Math.floor(Math.random() * 16).toString(16); //获取0-15并通过toString转16进制
    }

    var str = result.toUpperCase();
    return str;
}

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;
}

function getEncryptionKey(keyList, key) {
    var keyStr = '';

    for (var i = 0; keyList.length > i; i++) {
        if (keyList[i] === 1) {
            keyStr += key.charAt(keyList.length - i - 1);
        }
    }

    var keyStr = keyStr.split('').reverse().join('');
    return keyStr;
}

function contactList(list) {
    var contactList = [];

    for (var i = 0; list.length > i; i++) {
        contactList = contactList.concat(list[i]);
    }

    return contactList;
}

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;
}

module.exports = {
    confirmCommand: confirmCommand,
    togetherCommand: togetherCommand,
    mergeUint8Array: mergeUint8Array
};
