function parseBmsData(bmsData, bmsData1, bmsData2) {
    var state = {};
    state = getBmsFunction30(bmsData, state);
    state = getBmsFunction31(bmsData1, state);
    state = getBmsFunction32(bmsData2, state);
    return {
        state: state,
        btsets: getBmsSet()
    };
}

function getBmsFunction30(bmsData, state) {
    state['voltageAll'] = (bmsData[1] * 0x100 + bmsData[0]) * 0.01;
    state['soc'] = (bmsData[5] * 0x100 + bmsData[4]) * 0.02;
    state['soh'] = (bmsData[7] * 0x100 + bmsData[6]) * 1;
    state['maxVoltage'] = ((bmsData[9] * 0x100 + bmsData[8]) * 0.001).toFixed(2);
    state['minVoltage'] = ((bmsData[13] * 0x100 + bmsData[12]) * 0.001).toFixed(2);
    state['voltagDifference'] = ((bmsData[17] * 0x100 + bmsData[16]) * 0.001).toFixed(2);
    state['maxTemp'] = ((bmsData[19] * 0x100 + bmsData[18] - 2731) * 0.1).toFixed(2);
    state['minTemp'] = ((bmsData[23] * 0x100 + bmsData[22] - 2731) * 0.1).toFixed(2);
    var alarmList1 = getBooleanArray(bmsData[26]);
    var alarmList2 = getBooleanArray(bmsData[27]);
    var protectList1 = getBooleanArray(bmsData[30]);
    var protectList1 = getBooleanArray(bmsData[31]);
    var failureList = getBooleanArray(bmsData[34]);
    var systemList1 = getBooleanArray(bmsData[36]);
    var systemList2 = getBooleanArray(bmsData[37]);
    state['dischargeMOSOnOff'] = systemList1[0];
    state['chargingMOSOnOff'] = systemList1[1];
    state['dischargeMOS'] = state['dischargeMOSOnOff'];
    state['chargingMOS'] = state['chargingMOSOnOff'];
    var chargeState = 0;
    var current = parseInt(bmsData[3] * 0x100 + bmsData[2]) & 0xffff;

    if ((current & 0x8000) == 0x8000) {
        // 充电状态 0:静止 1:充电 2:放电
        chargeState = 2;
        current = ((~current + 1) & 0xffff) * -1;
    } else {
        if (current > 0) {
            chargeState = 1;
            current = current;
        }
    }

    state['current'] = current;
    state['chargeState'] = chargeState;
    return state;
}

function getBmsFunction31(bmsData1, state) {
    var tempCount = bmsData1[1] * 0x100 + bmsData1[0];
    state['tempCount'] = tempCount;
    var temp = [];

    for (var i = 0; i < tempCount; i++) {
        var tempInt = ((bmsData1[i * 2 + 3] * 0x100 + bmsData1[i * 2 + 2] - 2731) * 0.1).toFixed(2);
        temp.push(tempInt);
    }

    state['temp'] = temp;
    return state;
}

function getBmsFunction32(bmsData2, state) {
    var stateCount = bmsData2[1] * 0x100 + bmsData2[0];
    var sct = stateCount;
    var c = parseInt(Math.ceil(sct / 8));
    var i = 0;
    var sum = 0;
    var voltage = [];

    while (sct--) {
        var v = parseFloat(((bmsData2[i + 1 + c + 2] * 0x100 + bmsData2[i + c + 2]) * 0.001).toFixed(2));
        voltage.push(v);
        i += 2;
        sum += v;
    }

    state['voltage'] = voltage;
    state['count'] = stateCount;
    if (sum > 0) state['averageVoltage'] = parseFloat((sum / stateCount).toFixed(2));
    return state;
}

function getBmsSet() {
    var bmssets = [
        {
            class: '高低温保护阀值',
            name: 'AU10',
            vars: [
                {
                    label: '低温保护阀值',
                    name: 'AU100',
                    type: 'number',
                    value: ''
                },
                {
                    label: '高温保护阀值',
                    name: 'AU101',
                    type: 'number',
                    value: ''
                }
            ]
        },
        {
            class: '充放电保护电压',
            name: 'AU12',
            vars: [
                {
                    label: '充电保护电压',
                    name: 'AU120',
                    type: 'number',
                    value: ''
                },
                {
                    label: '放电保护电压',
                    name: 'AU121',
                    type: 'number',
                    value: ''
                }
            ]
        },
        {
            class: '充放电保护开关',
            name: 'AU13',
            vars: [
                {
                    label: '充电保护',
                    name: 'AU130',
                    type: 'radio',
                    option: [
                        {
                            label: '关闭',
                            value: '0'
                        },
                        {
                            label: '开启',
                            value: '1'
                        }
                    ],
                    value: ''
                },
                {
                    label: '放电保护',
                    name: 'AU131',
                    type: 'radio',
                    option: [
                        {
                            label: '关闭',
                            value: '0'
                        },
                        {
                            label: '开启',
                            value: '1'
                        }
                    ],
                    value: ''
                }
            ]
        },
        {
            class: '充放电保护恢复电压',
            name: 'AU14',
            vars: [
                {
                    label: '充电保护恢复电压',
                    name: 'AU140',
                    type: 'number',
                    value: ''
                },
                {
                    label: '放电保护恢复电压',
                    name: 'AU141',
                    type: 'number',
                    value: ''
                }
            ]
        },
        {
            class: '高低温保护恢复值',
            name: 'AU15',
            vars: [
                {
                    label: '低温保护恢复值',
                    name: 'AU150',
                    type: 'number',
                    value: ''
                },
                {
                    label: '高温保护恢复值',
                    name: 'AU151',
                    type: 'number',
                    value: ''
                }
            ]
        },
        {
            class: '电池循环次数',
            name: 'AU16',
            vars: [
                {
                    label: '循环次数',
                    name: 'AU160',
                    type: 'number',
                    value: ''
                }
            ]
        },
        {
            class: 'BMS自动保护开关',
            name: 'AU17',
            vars: [
                {
                    label: '自动保护',
                    name: 'AU170',
                    type: 'radio',
                    option: [
                        {
                            label: '关闭',
                            value: '0'
                        },
                        {
                            label: '开启',
                            value: '1'
                        }
                    ],
                    value: ''
                }
            ]
        }
    ];
    return bmssets;
}

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

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

    return array;
}

module.exports = {
    parseBmsData: parseBmsData
};
