/*
 * @Author: xiYou 
 * @Date: 2020-04-13 16:18:39 
 * @Last Modified by: xiYou
 * @Last Modified time: 2020-04-13 16:56:18
 */
<template>
  <div :id="nodeID" style="width:400px;heigth:400px"></div>
</template>
   
<script>
import RFB from '@novnc/novnc/core/rfb';
export default {
  data () {
    return {
   
    }
  },
  props: {
    
    configData: {
      type: Object,
      required: true
    },
    nodeID: {
      type: String,
      required: true
    }
  },
  mounted () {
    this.connectVNC(this.nodeID)
  },
  beforeDestroy() {
    // if (!this.myEcharts) {
    //   return
    // }
    // let vue = this
    // window.removeEventListener('resize', vue.change)
    // this.myEcharts.dispose()
    // this.myEcharts = null
  },
  watch: {
    option (option) {
      this.checkAndSetOption()
    }
  },
  methods: {  
     /**
     * @desc vnc连接断开的回调函数
     */
    disconnectedFromServer(msg) {
        console.log('connnect failed')
        if (msg.detail.clean) {
            // 根据 断开信息的msg.detail.clean 来判断是否可以重新连接
            this.contentVnc()
        } else {
            //这里做不可重新连接的一些操作
        }
    },

    /**
     * @desc 远程桌面连接成功后的回调函数
     */
    _onCompleteHandler(rfb, fbu) {
        console.log('success')
        // 清除 onComplete 的回调。
        rfb.set_onFBUComplete(function () {});

        let oDisplay = rfb.get_display(),
            nWidth = oDisplay.get_width(),
            nHeight = oDisplay.get_height(),
            oView = oDisplay.get_target(),
            nViewWidth = oView.clientWidth,
            nViewHeight = oView.clientHeight;
        // 设置当前与实际的比例。
        oDisplay.setScale(nWidth / nViewWidth, nHeight / nViewHeight);
    },
    /**
     * 获取vnc链接，接入虚机
     * @param callback
     * @param vm_uuid
     * @param topic_id
     * @param vm_id
     */
    getVnc(callback, vm_uuid, topic_id, vm_id) {
        disableChoosen();
        $.get('/vm/vnc', {
            i: vm_uuid,
            topic_id: topic_id,
            vm_id: vm_id
        }).done(function (response) {
            // enableChoosen();
            if (response.code == 0) {
                //console.log('prepare to call callback');
                callback(response.datas);
            }
        });
    },
    /**
     * desc 连接函数
     */
    connectVNC(nodeID) {
        console.log(nodeID)
        var socket_addr = '192.168.101.119:2120';
        // var vnc_params = {"host":"192.168.101.119",
        //                 "port":"8888",
        //                 "path":"websockify\/?vm_uuid=532922ca-92ca-4556-b963-22f4de627469",
        //                 "password":""};
        var DEFAULT_HOST = this.configData.host,
            DEFAULT_PORT = this.configData.port,
            DEFAULT_PASSWORD = this.configData.password,
            DEFAULT_PATH = this.configData.path;

        let cRfb = null;

        let that = this;
        // if (!this.currentEquipment) {
        //     console.log('直接返回')
        //      return;
        // }

        // let novncPort = this.currentEquipment.novncPort;
        // getNovncIp().then(function(resData) {

        // WebUtil.init_logging(WebUtil.getConfigVar('logging', 'warn'));

        //var sHost = resData.data.content.ip || DEFAULT_HOST,
        var sHost = DEFAULT_HOST,
            // nPort = novncPort || DEFAULT_PORT,
            nPort = DEFAULT_PORT,
            sPassword = DEFAULT_PASSWORD,
            sPath = DEFAULT_PATH;

        cRfb = new RFB(
            document.getElementById(nodeID),
            sPath, {
                credentials: {
                    username: '',
                    password: sPassword,
                    target: '',
                    wsProtocols: '',
                    // repeaterID: WebUtil.getConfigVar('repeaterID', ''),
                    // focusContainer: top.document, // 鼠标焦点定位
                    // encrypt: WebUtil.getConfigVar(
                    //     'encrypt',
                    //     window.location.protocol === 'https:'
                    // ),

                    // true_color: WebUtil.getConfigVar('true_color', true),
                    // local_cursor: WebUtil.getConfigVar('cursor', true),
                    // shared: WebUtil.getConfigVar('shared', true),
                    // view_only: WebUtil.getConfigVar('view_only', false),
                    // onFBUComplete: that._onCompleteHandler, // 回调函数
                    // onDisconnected: that.disconnectedFromServer, // 断开连接
                }
            })
        cRfb.scaleViewport = true; //scaleViewport指示是否应在本地扩展远程会话以使其适合其容器。禁用时，如果远程会话小于其容器，则它将居中，或者根据clipViewport它是否更大来处理。默认情况下禁用。
        cRfb.resizeSession = true; //是一个boolean指示是否每当容器改变尺寸应被发送到调整远程会话的请求。默认情况下禁用
        console.log(cRfb)
        cRfb.addEventListener('connect', this._onCompleteHandler);
        cRfb.addEventListener('disconnect', this.disconnectedFromServer);
        // cRfb.connect(sHost, nPort, sPassword, sPath);
        //   });
    }
  }
}
</script>
