import {getIMHandlerFactory} from "../libs/im-sdk/im-factory";
import { IM_HOST } from './../config/config'
import EventEmitter from './../utils/eventEmitter'
export default class AppIMDelegate {
    constructor(app) {
        this._app = app;
    }

    onLaunch(options) {
        this.iIMHandler = getIMHandlerFactory;
        EventEmitter.on('login', () => {
            this.connectSocket()
        })
        EventEmitter.on('logout', () => {
            this.iIMHandler.closeConnection()
        })
    }

    onShow(options) {
        this.connectSocket()
    }

    onHide() {
        this.iIMHandler.closeConnection()
    }

    getIMHandlerDelegate() {
        return this.iIMHandler;
    }
    connectSocket () {
        const app = getApp()
        if (app.globalData.token) {
            this.iIMHandler.createConnection({options: {url: `${IM_HOST}/im/${app.globalData.userinfo.viewerId}`}});
        }
    }
}
