/**
 * Created by zhangweicheng on 2017/8/7.
 */
socketObject = {
    roomInfo: null,
    messages: [],
    connect: function (from, payload) {
        // 连接socket服务器
        var hostName = window.location.hostname;
        let me = this;
        let port = window.location.protocol === 'http:' ? 3300 : 3301;
        socket = io(window.location.protocol + '//' + hostName + ':' + port);
        socket.on('connect', function () {
            if (from == 'pm') {
                me.roomInfo = {
                    compilationUser: `${userID}@${compilationData._id}`,
                    userID: userID
                };
            } else if (from == 'unitPrice') {
                me.roomInfo = {
                    unitFile: unitPriceFileID
                }
            } else {
                me.roomInfo = {
                    feeRate: me.getFeeRateRoomID(),
                    unitFile: me.getUnitFileRoomID(),
                    userIDProjectID: `${userID}@${projectObj.project.ID()}`
                };
                if (payload && payload.projectReadOnly === false) {
                    me.roomInfo.projectID = `projectID${projectObj.project.ID()}`;
                }
            }
            const emitData = {
                roomData: me.roomInfo,
                payload
            };
            socket.emit('join', emitData);
            if (me.messages.length > 0) {//发送缓存消息;
                for (let m of me.messages) {
                    socket.emit(m.message, m.data);
                }
            }
            if (typeof markReadProjectIDs !== 'undefined' && markReadProjectIDs.length) {
                SHARE_TO.emitPermissionChange(commonConstants.SharePermissionChangeType.READ, userID, null, [], { markReadProjectIDs });
            }
            console.log('连接成功');
        });

        //=========================================================
        //造价书页面接收消息部分
        socket.on('feeRateChange', function (data) {
            //data = JSON.parse(data);

            $("#message").html('费率文件已被修改，<a href="javascript:void(0);" id="load-data" onclick="window.location.reload()">点击加载并重新进行造价计算</a>');
            $("#notify").show();
            //alert('费率文件已经修改，请刷新页面');
            //window.location.reload();
        });
        socket.on('unitFileChange', function (data) {
            /*console.log(data);
            if (data.newValue === undefined) {
                return false;
            }*/
            $("#message").html('市场单价已被修改，<a href="javascript:void(0);" id="load-data" onclick="window.location.reload()">点击加载并重新进行造价计算</a>');
            $("#notify").show();
        });
        socket.on('changeFileNotify', function (data) {//收到文件改变的消息
            if (data.projectID == projectObj.project.ID()) {//如果是同个项目，则给出提示，否则忽略
                let preString = "";
                if (data.name == 'feeRate') {
                    preString = "费率文件";
                }
                if (data.name == 'unitFile') {
                    preString = "单价文件";
                }
                $("#message").html(preString + '已被修改，<a href="javascript:void(0);" id="load-data" onclick="window.location.reload()">点击加载并重新进行造价计算</a>');
                $("#notify").show();
            }
        });
        socket.on('handleAvatarList', function ({ editingUsers }) {
            projectInfoObj.handleAvatarList(editingUsers);
        });
        if(typeof SHARE_TO !== 'undefined') SHARE_TO.permissionChangeListener();

        //=============================================================================================
        //项目管理页面接收消息部分

        socket.on('refreshProjectIcon', function (data) {//收到刷新图标消息
            if (data.projectID && typeof projTreeObj !== 'undefined') projTreeObj.refreshProjectIcon(data.projectID);
        });
        socket.on('fileDataChange', function (data) {//收到单价文件、费率文件内容修改、文件切换、另存（暂时能共用，以后有需要可分离）推送消息
            if (data.projectID && typeof projTreeObj !== 'undefined') projTreeObj.refreshWhenFileDateChange(data.projectID);
        });
        // 项目管理树数据发生变化，提示刷新
        socket.on('pmTreeChange', function ({ expandState, selection }) {
            const isActive = $('#tab_pm_all').hasClass('active');
            if (isActive) {
                $("#message").html(`树结构发生变化，请<a href="javascript:void(0);" id="load-data">点击刷新列表</a>`);
                $('#load-data').on('click', () => projTreeObj.handleNotifyClick(expandState, selection));
                $('#notify > button').hide();
                $('#empty-modal').modal('show');
                $("#notify").show();
            }
        });
    },
    getFeeRateRoomID: function () {
        return projectObj.project.FeeRate.getActivateFeeRateFileID();
    },
    getUnitFileRoomID: function () {
        return projectObj.project.projectGLJ.datas.constData.roomId ? projectObj.project.projectGLJ.datas.constData.roomId : roomId;
    }
}