/**
* @description 威而信对接 Websocket对象接口
* @description 是否有重新连接IsReConnect设置为true，reConnectTime为重连的间隔；是否有心跳IsKeepAlive设置true|false, KeepAliveTime为心跳间隔, KeepAliveData心跳包参数
* @description 初始化：WebsocketInterface({"url":"",IsReConnect:true|false,reConnectTime:20,onOpen:function(e){},onMessage:function(e){},onError:function(e){},onClose:function(e){} });
* @description 方法：返回0成功，返回-1失败
* @description 发送：WebsocketInterface("Send","msg");
* @description 关闭：WebsocketInterface("Close");
* @description 重连：WebsocketInterface("ReConnect");
* @description 心跳：WebsocketInterface("KeepAlive");
* @description 错误：WebsocketInterface("GetError",code);
*/
export const WebsocketInterface = (function () {
    let wsObject:any = null; //连接对象
    let wsOptions :any= null; //当前socket的参数
    let wsInterval:any = null; //当前心跳定时器
    let wsTimeout:any = null; //当前重连定时器

    //方法
    const Methods:any = {
        Send: function (param: any) {
            function send() {
                wsObject.send(param);
            }
            if (wsObject.readyState == 1) {
                send();
            }
        },
        Close: function () {
            wsOptions.IsReConnect = false;
            if (!!wsObject) {
                wsObject.close();
            }
        },
        ReConnect: function () {
            const time = wsOptions.reConnectTime || 10; //默认为10秒
            wsTimeout = setTimeout(function () {
                WebsocketInterface(wsOptions);
            }, time * 1000);
        },
        KeepAlive: function () {
            const time = wsOptions.KeepAliveTime || 20; //默认为20秒
            if (!!wsInterval) {
                return;
            }
            if (wsOptions.IsKeepAlive && !!wsOptions.KeepAliveData) {
                wsInterval = setInterval(function () {
                    Methods["Send"](wsOptions.KeepAliveData);
                }, time * 1000);
            }
        },
        GetError: function (code: number) {
            let msgStr;
            switch (code) {
                case -1:
                    msgStr = "您的浏览器不支持WebSocket";
                    break;
                case 0:
                    msgStr = "WebSocket连接尚未创建,请先签入";
                    break;
                case 1:
                    msgStr = "WebSocket连接已经建立";
                    break;
                case 2:
                    msgStr = "WebSocket连接正在关闭";
                    break;
                case 3:
                    msgStr = "WebSocket连接已经关闭或不可用";
                    break;
                case 4:
                    msgStr = "指定方法未定义";
                    break;
                case 5:
                    msgStr = "初始化WebSocket未定义url";
                    break;
            }
            return msgStr;
        }
    };
    return function (options: any, param?: string) {

        if (options == "GetError") {
            return Methods["GetError"](param);
        }
        if (!window.WebSocket) {
            return -1;
        }
        if (typeof (options) == "string") {//方法
            if (!wsObject) {
                return 0;
            }
            if (!Methods[options]) {
                return 4;
            }
            Methods[options](param);
            return wsObject.readyState;
        } else {//属性或事件
            if (!wsObject && !options.url) {
                return 5;
            }
            wsOptions = options;
            wsObject = new WebSocket(options.url);
            wsObject.onopen = function (e: any) {
                if (!!options.onOpen) {
                    options.onOpen(e);
                }
            }
            wsObject.onmessage = function (e: any) {
                if (!!wsTimeout) {
                    clearTimeout(wsTimeout); //清空重连
                    wsTimeout = null;
                }
                if (!!options.onMessage) {
                    options.onMessage(e);
                }
            }
            wsObject.onerror = function (e: any) {
                if (!!options.onError) {
                    options.onError(e);
                }
            }
            wsObject.onclose = function (e: any) {
                if (!!options.onClose) {
                    options.onClose(e);
                }
                clearInterval(wsInterval); //清空心跳
                wsInterval = null;
                if (wsOptions.IsReConnect) {//是否重连，如果是手动断开，请调用WebsocketInterface("Close")
                    Methods.ReConnect();
                }
            }
        }
        return wsObject.readyState;
    }
})();