import {global} from "http://devtalk.kospo.co.kr:3000/static/js/module/variable.js";

const HubEvent = {
    start: (payload) => {
        global.stomp.send(`/pub/hub.start.${global.tabId}`, {}, JSON.stringify(payload))
    },
    connect: (payload) => {
        global.registration.active.postMessage(payload)
    },
    close: (payload) => {
        if(payload.talkId === null) return false;
        global.registration.active.postMessage(payload)
        global.stomp.send(`/pub/hub.close.${global.tabId}`, {}, JSON.stringify(payload))
    },
    kill: (payload) => {
        global.registration.active.postMessage(payload)
        global.registration.unregister();
    },
    sendSetMaster: (payload) => {
        payload.channelSabun = global.user.sabun
        payload.tabId = global.tabId
        global.stomp.send(`/pub/hub.send.setMaster`, {}, JSON.stringify(payload))
    },
    otherInit: (payload) => {
        payload.channelSabun = global.user.sabun
        payload.tabId = global.tabId
        global.stomp.send(`/pub/hub.otherInit`, {}, JSON.stringify(payload))
    },
    send: (payload) => {
        // console.log('hub send is me', payload)
        if(payload.talkId === null) return false;
        payload.channelSabun = global.user.sabun
        payload.tabId = global.tabId
        try {
            if (payload.type === 'ONLINE' || payload.type === 'OFFLINE') {
                global.stomp.send(`/pub/hub.online`, {}, JSON.stringify(payload))
            } else if (payload.type === 'ATTACH_INIT') {
                global.stomp.send(`/pub/hub.file.message`, {}, JSON.stringify(payload))
            } else {
                /*톡 요청 상태에 따른 comp_yn 완료 처리*/
                if(payload.reqType === '9000') {
                    const cacheTalk = global.talkData.filter((talk) => talk.talkId === payload.talkId)[0];
                    cacheTalk.compYn= true
                    console.log(cacheTalk)
                }
                global.stomp.send(`/pub/hub.talk.message`, {}, JSON.stringify(payload))
            }
        } catch (e) {
        }
    },
    fileSend: (payload) => {
        if(payload.talkId === null) return false;
        payload.channelSabun = global.user.sabun
        payload.tabId = global.tabId
        global.stomp.send(`/pub/hub.file.message`, {}, JSON.stringify(payload))
    },
    work: (payload) => {
        payload.channelSabun = global.user.sabun
        payload.tabId = global.tabId
        global.stomp.send(`/pub/hub.work`, {}, JSON.stringify(payload))
    }
}


export {HubEvent};