import Stomp from "stompjs";
import { projectconfig } from "../static/projectconfig";
import store from '@/store/index'
import Vue from 'vue'
import Xaxios from 'axios';


// ============================一般使用的变量============================
export const calcInit = initialize;
export const calcGetPopupAlarmStatus = getPopupAlarmStatus;
export const calcSend = send;
let calcClient = null;
let number = 0;

// ============================  大函数体   ============================
function initialize() {
    calcClient = null;
    var url = projectconfig.websocket.calcHost;
    calcClient = Stomp.client(url);
    if (!projectconfig.allowCalcDebug) {
        calcClient.debug = null;
    }
    calcClient.connect({}, stompFrame => connectCallBackSubscribe(calcClient), error => reconnect(error));
}
// 断线重连
function reconnect(error) {
    //连接失败时再次调用函数
    number++;
    calcClient.connected = false;
    clearTimeout(setTimeout(initialize(), 1000 * 5));
    debugX("CalcClient reconnect:" + number + " 次");
    return;
}
/*** 是否连接成功 */
function isConnect() {
    return calcClient.connected;
}
/*** 发送数据 
 * @param {string} destination 地址,从配置文件中找
 * @param {{}} headers 头信息,没有的信息函数自动添加
 * @param {string} body 数据内容
 */
function send(destination, headers, body) {
    if (calcClient && calcClient.connected) {
        calcClient.send(destination, headers, body);
    }
}

/*** 查询设备详情数据
 * @param {{}} headers 需要添加一个"condition"头信息,searchCondition,searchCondition.objectId=设备的objectid
 * @param {any} 查询数据返回解析回调函数
*/

// 获取弹窗当前的状态
function setPopupAlarmStatus(frame) {
    // <long, bool>  key:faultinformation.id
    var popupAlarmStatusMap = JSON.parse(frame.body);
    store.dispatch('updatePopupAlarmStatus', popupAlarmStatusMap);
}
// ============================  订阅函数体  ============================
function connectCallBackSubscribe(calcClient)
{
    number = 0;
    calcClient.connected = true;
    calcClient.subscribe(projectconfig.websocket.calcSubscribeMap.popupAlarm, frame => popupAlarm(frame));                        // 报警弹窗
}

// ============================   ============================
/***查询当前弹窗的状态*/
function getPopupAlarmStatus() {
    if (store.state.alertMessageKeys.length > 0) {
        let headers = {};
        headers["idList"] = JSON.stringify(store.state.alertMessageKeys);
        if (calcClient && calcClient.connected) {
            calcClient.subscribe(projectconfig.calcSubmitMap.getPopupAlarmStatus, frame => setPopupAlarmStatus(frame), headers);
        }
    }
}
// ============================  解析函数体  ============================

// 报警信息弹窗
function popupAlarm(frame) {
    var alertMessage = JSON.parse(frame.body);
    let count = Object.getOwnPropertyNames(alertMessage).length;
    if (count > 0) {
        store.dispatch('ReceivePopupAlertMessage', alertMessage);
    }
}


// 设备报警详情
function getRealTimeAlarmList(frame) {
    var realtimeAlertList = JSON.parse(frame.body);
    store.dispatch('updateMachineAlarmDetailsByAlarmPopup', realtimeAlertList);

    // 此处注释的原因是因为有多处的请求 返回的结果都将成为一个数据源,放在全局中被引用
    // 每个请求的地方都是有自己特色的head的id开头,返回的heads中的subscription会标识出来
    // if(frame.headers["subscription"] && frame.headers["subscription"].startsWith(projectconfig.getRealTimeAlarmListEnum.alarmmessageclick))//点击弹窗上的元素而显示的
    // {
    //     //List<RealTimeAlertInfo>
    //     var realtimeAlertList=JSON.parse(frame.body);
    //     store.dispatch('updateMachineAlarmDetailsByAlarmPopup', runtimeAlarmList);
    // }
}




// ============================  其他  ============================
function debugX(text) {
    console.log(text);
}
