self.importScripts('wasmcommon.js');
/**
 * wesoket的实体类
 * @constructor
 */
function Downloadereasy() {
    this.logger = new Logger('Downloader');
    this.Socket = null;
    this.msgId = 0;

}


Downloadereasy.prototype.reportDevmessage = function(code, data) {
    var objData = {
        t: kcreateDevRsp,
        i: {
            code: code,
            data: data,
        }
    };
    self.postMessage(objData)
};


Downloadereasy.prototype.reportDevlivemessage = function(code, data) {
    var objData = {
        t: kcrdevliveRsp,
        i: {
            code: code,
            data: data,
        }
    };
    self.postMessage(objData)
};




Downloadereasy.prototype.reportDevrecordpathmessage = function(code, data) {
    var objData = {
        t: kcrdevrecordRsp,
        i: {
            code: code,
            data: data,
        }
    };
    self.postMessage(objData)
};


Downloadereasy.prototype.reportDevsnapshotmessage = function(code, data) {
    var objData = {
        t: kcrdevsnapshotRsp,
        i: {
            code: code,
            data: data,
        }
    };
    self.postMessage(objData)
};




Downloadereasy.prototype.reportclosemessage = function(code, data) {
    var objData = {
        t: kdevCloseRsp,
        i: {
            code: code,
            data: data,
        }
    };
    self.postMessage(objData)
};



Downloadereasy.prototype.reporthidemessage = function(code, data) {
    var objData = {
        t: kdevSetcloseRsp,
        i: {
            code: code,
            data: data,
        }
    };
    self.postMessage(objData)
};


Downloadereasy.prototype.reportserverPushmessage = function(code, data) {
    var objData = {
        t: kserverPushRsp,
        i: {
            code: code,
            data: data,
        }
    };
    self.postMessage(objData)
};



Downloadereasy.prototype.requestDevtWebsocket = function(url, msg, type, cb) {
    var that = this;
    setTimeout(function() { //延时为了火狐，但是在其他浏览器socket会出现非1状态的报错，不影响
        if (that.Socket && that.Socket.readyState != 1) { //Socket存在且并非处于连接状态,关闭并清空之前的WebSocket连接。
            that.Socket = null;
        }
    }, 500);
    if (!this.Socket) { //null 重新创建WebSocket实例
        this.Socket = new WebSocket(url);
        if (type != "") {
            this.Socket.binaryType = type;
        }
        var self = this;
        this.Socket.onopen = (function() { //当连接成功,执行send,调试发现这个是异步可能存在先后顺序的问题，导致状态还没又改变
            if (self.Socket.readyState == 1) {
                self.Socket.send(msg);
            }
        });
        this.Socket.onerror = cb.onenventonerror; //如连接失败、传输错误等情况
        this.Socket.onclose = cb.onenventonclose; //关闭Socket触发
        this.Socket.onmessage = cb.onenventmessage; //监听服务器返回的消息
    } else {
        this.Socket.send(msg);
        this.Socket.onerror = cb.onenventonerror;
        this.Socket.onclose = cb.onenventonclose;
        this.Socket.onmessage = cb.onenventmessage;
    }
};


Downloadereasy.prototype.getDevByWebsocket = function(url, msg, type) {
    var data = null;
    var self = this;
    this.requestDevtWebsocket(url, JSON.stringify(msg), type, {
        onenventmessage: function(evt) {
            self.Socket.close();
            data = JSON.parse(evt.data);
            if (msg.msgId == JSON.parse(evt.data).msgId) {
                self.reportDevmessage(data.code, data)
            }
        },
        onenventonerror: function() {
            self.reportDevmessage(9999, data);
        }
    })
};

Downloadereasy.prototype.repordevreconstructionmessage = function(code, data) {
    var objData = {
        t: kdevreconstructionRsp,
        i: {
            code: code,
            data: data,
        }
    };
    self.postMessage(objData)
};

Downloadereasy.prototype.repordevglobalCtrmessage = function(code, data) {
    var objData = {
        t: kdevreconsglobalCtrlRsp,
        i: {
            code: code,
            data: data,
        }
    };
    self.postMessage(objData)
};



Downloadereasy.prototype.getDevliveByWebsocket = function(url, msg, type) {
    var data = null;
    var self = this;
    this.requestDevtWebsocket(url, JSON.stringify(msg), type, {
        onenventmessage: function(evt) {
            data = JSON.parse(evt.data);
            if (data.Fountion == "serverPush") {
                self.reportserverPushmessage(200, data)
            }
            if (msg.msgId == JSON.parse(evt.data).msgId) {
                self.reportDevlivemessage(data.code, data)
            }
        },
        onenventonerror: function() {
            self.reportDevlivemessage(9999, data);
        },
        onenventonclose: function() {
            self.reportDevlivemessage(9999, data);
        }
    })
};




Downloadereasy.prototype.getDevrecordpathByWebsocket = function(url, msg, type) {
    var data = null;
    var self = this;
    this.requestDevtWebsocket(url, JSON.stringify(msg), type, {
        onenventmessage: function(evt) {
            data = JSON.parse(evt.data);
            if (data.Fountion == "serverPush") {
                self.reportserverPushmessage(200, data)
            }
            if (msg.msgId == JSON.parse(evt.data).msgId) {
                self.reportDevrecordpathmessage(data.code, data)
            }
        },
        onenventonerror: function() {
            self.reportDevrecordpathmessage(9999, data);
        },
        onenventonclose: function() {
            self.reportDevrecordpathmessage(9999, data);
        }
    })
};


Downloadereasy.prototype.getDevsnapshotByWebsocket = function(url, msg, type) {
    var data = null;
    var self = this;
    this.requestDevtWebsocket(url, JSON.stringify(msg), type, {
        onenventmessage: function(evt) {
            data = JSON.parse(evt.data);
            if (data.Fountion == "serverPush") {
                self.reportserverPushmessage(200, data)
            }
            if (msg.msgId == JSON.parse(evt.data).msgId) {
                self.reportDevsnapshotmessage(data.code, data)
            }

        },
        onenventonerror: function() {
            self.reportDevsnapshotmessage(9999, data);
        },
        onenventonclose: function() {
            self.reportDevsnapshotmessage(9999, data);
        }
    })
};

Downloadereasy.prototype.getclsoeWebsocket = function(url, msg, type) {
    var data = null;
    var self = this;
    this.requestDevtWebsocket(url, JSON.stringify(msg), type, {
        onenventmessage: function(evt) {
            data = JSON.parse(evt.data);
            if (msg.msgId == JSON.parse(evt.data).msgId) {
                self.reportclosemessage(data.code, data)
            }
            self.Socket.close();
        },
        onenventonerror: function() {
            self.reportclosemessage(9999, data);
        }
    })
};


Downloadereasy.prototype.getwebclsoeWebsocket = function(url, msg, type) {
    var data = null;
    var self = this;
    this.requestDevtWebsocket(url, JSON.stringify(msg), type, {
        onenventmessage: function(evt) {
            data = JSON.parse(evt.data);
            if (msg.msgId == JSON.parse(evt.data).msgId) {
                self.reporthidemessage(data.code, data)
            }
        },
        onenventonerror: function() {
            self.reporthidemessage(9999, data);
        },
        onenventonclose: function() {
            self.reporthidemessage(9999, data);
        }
    })
};

Downloadereasy.prototype.getdevreconstructionWebsocket = function(url, msg, type) {
    var data = null;
    var self = this;
    this.requestDevtWebsocket(url, JSON.stringify(msg), type, {
        onenventmessage: function(evt) {
            data = JSON.parse(evt.data);
            if (msg.msgId == JSON.parse(evt.data).msgId) {
                self.repordevreconstructionmessage(data.code, data)
            }
        },
        onenventonerror: function() {
                self.repordevreconstructionmessage(10000, data);
            }
            // , onenventonclose:function(){
            //     self.repordevreconstructionmessage(9999, data);
            // }
    })
};

Downloadereasy.prototype.getkdevreconsglobalCtrl = function(url, msg, type) {
    var data = null;
    var self = this;
    this.requestDevtWebsocket(url, JSON.stringify(msg), type, {
        onenventmessage: function(evt) {
            data = JSON.parse(evt.data);
            if (msg.msgId == JSON.parse(evt.data).msgId) {
                self.repordevglobalCtrmessage(data.code, data)
            }
        },
        onenventonerror: function() {
            self.repordevglobalCtrmessage(10000, data);
        }
        // , onenventonclose:function(){
        //     self.repordevglobalCtrmessage(9999, data);
        // }
    })
};



self.downloadereasy = new Downloadereasy();


self.onmessage = function(evt) { //当主线程通过 worker.postMessage() 向 Web Worker 发送消息时，Web Worker 就会触发 onmessage 事件，并将消息作为事件的参数。
    if (!self.downloadereasy) {
        console.log('[ER] downloadereasy not initialized!');
        return
    }
    let objData = evt.data;
    self.downloadereasy.msgId++;
    objData.msg.msgId = self.downloadereasy.msgId;
    switch (objData.t) {
        case kcreateDevReq:
            self.downloadereasy.getDevByWebsocket(objData.url, objData.msg, objData.type);
            break;
        case kcrdevliveReq:
            self.downloadereasy.getDevliveByWebsocket(objData.url, objData.msg, objData.type);
            break;

        case kcrdevrecordReq:
            self.downloadereasy.getDevrecordpathByWebsocket(objData.url, objData.msg, objData.type);
            break;
        case kcrdevsnapshotReq:

            self.downloadereasy.getDevsnapshotByWebsocket(objData.url, objData.msg, objData.type);
            break;

        case kdevCloseReq:
            self.downloadereasy.getclsoeWebsocket(objData.url, objData.msg, objData.type);
            break;
        case kdevSetcloseReq:
            self.downloadereasy.getwebclsoeWebsocket(objData.url, objData.msg, objData.type);
            break;
        case kdevreconstructionReq:
            self.downloadereasy.getdevreconstructionWebsocket(objData.url, objData.msg, objData.type);
            break;
        case kdevreconsglobalCtrlReq:
            self.downloadereasy.getkdevreconsglobalCtrl(objData.url, objData.msg, objData.type);
            break;
        default:
            self.downloadereasy.logger.logError('Unsupport messsage' + objData.t)
    }
};
