/**
* Created by Zhang Haijun on 2017/8/29.
*/
// 查询参数封装
export function formatSearchParam (param) {
    // 对参数进行处理，去除空参数
    let toObjFormat = function (obj) {
        for (let a in obj) {
            if (obj[a] === '') delete obj[a];
        }
        return obj;
    };
    let toTwo = function (str) {
        if (str.toString().length == 1) {
            str = '0' + str;
        }
        return str;
    };
// 对时间进行处理
    let toFormatTime = function (time) {
        if (!time) return '';
        return time.getFullYear() + '/' + toTwo(time.getMonth() + 1) + '/' + toTwo(time.getDate()) + ' ' + toTwo(time.getHours()) + ':' + toTwo(time.getMinutes()) + ':' + toTwo(time.getSeconds());
    };
    let params = [];
    let lkParam = toObjFormat(param.lkParam || {});
    let eqParam = toObjFormat(param.eqParam || {});
    let ueqParam = toObjFormat(param.ueqParam || {});
    let getParam = toObjFormat(param.getParam || {});
    let letParam = toObjFormat(param.letParam || {});
    let nulParam = toObjFormat(param.nulParam || {});
    if (param.date) {
        for (let a in param.date){
            let item = param.date[a];
            if (item) {
                getParam[a] = toFormatTime(item[0]);
                letParam[a] = toFormatTime(item[1]);
            }
        }
    }
    if (JSON.stringify(lkParam).length > 2) params.push({param: lkParam, sign: 'LK'});
    if (JSON.stringify(eqParam).length > 2) params.push({param: eqParam, sign: 'EQ'});
    if (JSON.stringify(ueqParam).length > 2) params.push({param: ueqParam, sign: 'UEQ'});
    if (JSON.stringify(getParam).length > 2) params.push({param: getParam, sign: 'GET'});
    if (JSON.stringify(letParam).length > 2) params.push({param: letParam, sign: 'LET'});
    if (JSON.stringify(nulParam).length > 2) params.push({param: nulParam, sign: 'NUL'});
    return JSON.stringify(params);
}
// websocket文件上传
export function uploadFile (item, callback, errorCallBack) {
    item.isReady = true;
    let socket = new WebSocket('ws://' + location.host + '/uploadService');
    let i = 0;
    let startSize = 0, endSize = 0;
    let paragraph = 4 * 1024 * 1024; // 以4MB为一个分片
    let count = parseInt(item.file.size / paragraph) + 1;
    socket.onopen = function () {
        item.isUploading = true;
        socket.send(JSON.stringify({
            'filename': item.file.name,
            'upload': 'file'
        }));
    // 取消上传
        item.cancel = function () {
            item.progress = 0;
            socket.send(JSON.stringify({
                'UPLOAD_CANCEL': 'UPLOAD_CANCEL'
            }));
            item.isUploading = false;
        };
    };
    socket.onmessage = function (event) {
        let sendFile = function () {
            if (startSize < item.file.size) {
                let blob;
                endSize += paragraph;
                if (item.file.webkitSlice) {
                    blob = item.file.webkitSlice(startSize, endSize);
                } else if (item.file.mozSlice) {
                    blob = item.file.mozSlice(startSize, endSize);
                } else {
                    blob = item.file.slice(startSize, endSize);
                }
                let reader = new FileReader();
                reader.readAsArrayBuffer(blob);

                reader.onload = function loaded (evt) {
                    let result = evt.target.result;
                    i++;
                    let isok = (i / count) * 100;
                    item.progress = parseInt(isok);
                    startSize = endSize;
                    socket.send(result);
                };
            } else {
                item.progress = 100;
                socket.send(JSON.stringify({
                    'sendover': 'sendover'
                }));
            }
        };
        item.isUploading = true;
        item.isCancel = false;
        let obj = JSON.parse(event.data);
        if (obj.category == 'UPLOAD_ACK') {
            item.filePath = obj.content;
            sendFile();
        } else if (obj.category == 'UPLOAD') {
            if (obj.content == 'SAVE_FAILURE') {
                item.isUploading = false;
                errorCallBack(item);
            } else if (obj.content == 'SAVE_SUCCESS') {
                sendFile();
            } else if (obj.content == 'TRUE') {
                callback(item);
                item.isReady = true;
                item.isSuccess = true;
                item.isUploading = false;
                socket.close();
            }
        } else if (obj.category == 'UPLOAD_CANCEL') {
            item.progress = 0;
            item.isCancel = true;
            socket.close();
        }
    };
}
// 生成时间戳
export function makeTimeStamp () {
    let format = function (value) {
        if (value / 1 < 10) value = '0' + value;
        return value.toString();
    };
    let date = new Date();
    return date.getFullYear() + format(date.getMonth() + 1) + format(date.getDate()) + format(date.getHours()) + format(date.getMinutes()) + format(date.getSeconds());
}
// 将数字转换为正整数
export function toInteger (attr, data) {
    if (!data[attr]) return;
    data[attr] = Math.round(Math.abs(data[attr]));
}
