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

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

const msg = require('./LSCabinetMsg.js');

var functionList = [];
const DF_CAB_INFO_LOAD = 10001; //机柜信息传输中

const DF_CAB_INFO_DONE = 10000; //机柜信息传输完成

function realData(value1) {
    //汇报数据
    var json = {};
    json['command'] = 0x00;
    json['response'] = 0;
    json['report'] = 0;
    console.log(value1);
    var value = dataBody(value1);
    if (value.length < 2) return;

    if (value[0] === 0x81 || value[0] === 0x82 || value[0] === 0x83) {
        //租电 换电 还电 响应数据
        json = exChangeHireReturnReponse(value, json); //换电 租电 还电汇报数据

        json = exChangeHireReturnReport(value, json);
    }

    if (value[0] === 0x84) {
        if (value[1] === 0x01) {
            //响应数据
            json['msg'] = '取消未确认成功';
            json['state'] = value[2];
        }
    } //电柜信息

    json = cabinetInfo(value, value1, json);
    return json;
} //换电 租电 还电汇报数据

function exChangeHireReturnReport(value, json) {
    if (value[0] === 0x81) {
        if (value[1] === 0x02) {
            json['msg'] = msg.reportMsg(value);
            json['report'] = 1; //汇报数据

            var toIndex = 4;
            json['state'] = value[2];
            json['emptyNo'] = value[3];
            json['emptyBatteryNo'] = asciiListToString(toIndex, value);
            json['emptyBatterySOC'] = value[toIndex + value[toIndex] + 1];
            toIndex = toIndex + value[toIndex] + 1 + 1;
            json['fullNo'] = value[toIndex];
            toIndex = toIndex + 1;
            json['fullBatteryNo'] = asciiListToString(toIndex, value);
            json['fullBatterySOC'] = value[toIndex + value[toIndex] + 1];
            toIndex = toIndex + value[toIndex] + 1 + 1;
            json['orderNo'] = asciiListToString(toIndex, value);
            toIndex = toIndex + value[toIndex] + 1;
            json['serialLen'] = value[toIndex];
            json['serialNo'] = asciiListToString(toIndex, value);
        }
    } else {
        if (value[1] === 0x02) {
            json['msg'] = msg.reportMsg(value);
            json['report'] = 1;
            var toIndex = 4;
            json['state'] = value[2];
            json['boxNo'] = value[3];
            json['batteryNo'] = asciiListToString(toIndex, value);
            json['batterySOC'] = value[toIndex + value[toIndex] + 1];
            toIndex = toIndex + value[toIndex] + 1 + 1;
            json['orderNo'] = asciiListToString(toIndex, value);
            toIndex = toIndex + value[toIndex] + 1;
            json['serialLen'] = value[toIndex];
            json['serialNo'] = asciiListToString(toIndex, value);
        }
    }

    console.log('蓝牙汇报操作的json数据');
    console.log(json);
    return json;
} //换电 租电 还电响应数据

function exChangeHireReturnReponse(value, json) {
    if (value[1] === 0x01) {
        //响应数据
        json['state'] = value[2];
        json['response'] = 1;
        json['msg'] = msg.reponseMsg(value);
        json['command'] = value[0];

        if (value[0] === 0x81) {
            json['batteryNo'] = asciiListToString(3, value);
            var toIndex = 3 + value[3] + 1;
            json['serialLen'] = value[toIndex];
            json['serialNo'] = asciiListToString(toIndex, value);
        } else {
            json['serialLen'] = value[3];
            json['serialNo'] = asciiListToString(3, value);
        }
    }

    console.log('蓝牙响应操作的json数据');
    console.log(json);
    return json;
} //换电柜信息

function cabinetInfo(value, value1, json) {
    if (value[0] === 0x86) {
        if (value[1] === 0x01) {
            functionList = [];
        }

        json['state'] = DF_CAB_INFO_LOAD; //信息传输中

        json['command'] = value[0];

        if (value.length > 3) {
            if (value[1] === 0x04) {
                if (functionList.length > 0 && functionList[0].length > 2 && functionList[0][2] === 0x86 && functionList[0][3] === 0x01) {
                    json['commandList'] = functionList;
                    json['state'] = DF_CAB_INFO_DONE; //信息传输结束

                    functionList = [];
                }
            } else {
                functionList.push(value1);
            }
        }
    }

    return json;
}

function asciiListToString(index, value) {
    var len = value[index];
    index = index + 1;
    var str = '';

    if (len != 0) {
        for (var i = 0; len > i; i++) {
            str += String.fromCharCode(value[index + i]);
        }
    } else {
        str = '0';
    }

    return str;
}

function dataBody(hexList) {
    if (hexList.length > 4) {
        if (hexList[0] === 0x7a && hexList[hexList.length - 1] === 0x78) {
            return hexList.slice(2, -2);
        }
    } else {
        return hexList;
    }
}

function reportCabintHire(dev_id, sn, json) {
    const pData = {
        battery_ca: json.batteryNo,
        battery_sn: sn,
        box_sn: json.boxNo,
        dev_id: dev_id,
        order_sn: json.orderNo,
        status: json.state
    };
    http.postApi(config.API_CABINET_BLUETOOTH_HIRE, pData, function (response) {
        if (response.data.code === 200) {
        } else {
            common.simpleToast(response.data.msg);
        }
    });
}

function reportCabintExchange(dev_id, json) {
    const pData = {
        battery_sn: json.batteryNo,
        dev_id: dev_id,
        order_sn: json.orderNo,
        status: json.state,
        rtn_door_id: json.emptyNo,
        brw_door_id: json.fullNo,
        rtn_battery_ca: json.emptyBatteryNo,
        brw_battery_ca: json.fullBatteryNo
    };
    http.postApi(config.API_CABINET_BLUETOOTH_EXCHANGE_SUCCESS, pData, function (response) {
        if (response.data.code === 200) {
        } else {
            common.simpleToast(response.data.msg);
        }
    });
}

function reportCabintReturn(dev_id, sn, json) {
    const pData = {
        battery_sn: sn,
        rtn_battery_ca: json.batteryNo,
        dev_id: dev_id,
        order_sn: json.orderNo,
        box_sn: json.boxNo,
        status: json.state
    };
    http.postApi(config.API_CABINET_BLUETOOTH_RETURN_SUCCESS, pData, function (response) {
        if (response.data.code === 200) {
        } else {
            common.simpleToast(response.data.msg);
        }
    });
}

module.exports = {
    realData: realData,
    reportCabintExchange: reportCabintExchange,
    reportCabintHire: reportCabintHire,
    reportCabintReturn: reportCabintReturn
};
