
import Vue from 'vue'

let vue = new Vue()

export default class Utils {
  constructor(){
    
  }
  /**
   * 设置初始视角
   */
  setInitView(krpano,canvas) {
      let url = this.setInitAngleImg(canvas);
      var sceneName = krpano.get('xml.scene');
      var hlookat = krpano.get("view.hlookat");
      var vlookat = krpano.get("view.vlookat");
      console.log(hlookat,vlookat);
      return {
        sceneName,
        hlookat,
        vlookat,
        url
      }
  }

  scaleCanvas (canvas, width, height) {
    let w = canvas.width
    let h = canvas.height;
    if (width == undefined) {
        width = w;
    }
    if (height == undefined) {
        height = h;
    }
    var retCanvas = document.createElement('canvas');
    var retCtx = retCanvas.getContext('2d');
    retCanvas.width = width;
    retCanvas.height = height;
    retCtx.drawImage(canvas, 0, 0, w, h, 0, 0, width, height);
    return retCanvas;
  }
  getDataURL (canvas, type, width, height) {
    let cas = this.scaleCanvas(canvas, width, height);
    return cas.toDataURL(type,1.0);
  }

  setInitAngleImg(mycanvas){
      var url = this.getDataURL(mycanvas,"image/jpeg",600,400);
      return url
  }
  radarRotate(){
    // console.log(sceneName,hlookat);
  }

  /**
   * 编辑热点
   */

  edithotspot(krpano,param){
    krpano.call(`editImgTextHotSpot(
      ${param.img},
      ${param.name},
      ${param.hotspotTitle},
      ${param.ath},
      ${param.atv},
      ${true},
      ${true},
      ${param.link},
      ${true},
      ${param.visible},
      ${param.size||1},
      ${param.fontSize})`); 
   }


  /**
   * 添加热点
   */

  addhotspot(krpano,param, type){
    krpano.set("curscreen_x", $('#pano').width() / 2);
    krpano.set("curscreen_y", $('#pano').height() / 2);
    krpano.call("screentosphere(curscreen_x, curscreen_y, curscreen_ath, curscreen_atv);");
    let ath = krpano.get("curscreen_ath");
    let atv = krpano.get("curscreen_atv");
    krpano.call(`addImgTextHotSpot(
      ${param.img},
      ${param.name},
      ${param.hotspotTitle},
      ${param.ath!=''?param.ath:ath},
      ${param.atv!=''?param.atv:atv},
      ${true},
      ${type},
      ${param.link},
      ${true},
      ${param.visible},
      ${(param.size * 50) || 1},
      ${Number(param.fontSize)})`); 
  }

  htmlEncode(str) {
      var s = "";
      if (str.length == 0) return "";
      s = str.replace(/\'/g, "&#39;");
      s = s.replace(/\"/g, "&quot;");
      s = s.replace(/\(/g, "（");
      s = s.replace(/\)/g, "）");
      s = s.replace(/,/g, "，");
      return s;
  }

  /**
   * 
   * @param {*} 定位热点 
   */

  looktohotspot(krpano, hotspotName){
      var curFov = krpano.get('view.fov');
      krpano.call('looktohotspot('+hotspotName+','+curFov+')');
  }


  /**
   * 
   * @param {*} 定位热点 
   */

  getHotspotHV(krpano, hotspotName){
      var ath = krpano.get(`hotspot[${hotspotName}].ath`);
      var atv = krpano.get(`hotspot[${hotspotName}].atv`);
      return {ath,atv}
  }

    
  /**
   * 打开热点链接
   */

  linkopen(sceneCode,id){

    if (window.location.pathname.indexOf('show')>-1) {
      vue.$bus.emit('clickHotspot',id)
    }
    else if (window.location.pathname.indexOf('edit')>-1) {
      vue.$bus.emit('openHotspot',id)
    }
    else{
      window.parent.postMessage({ event: 'hotspot', targetCode: sceneCode },'*')
    }
   
  }

  

  
  
  /**
   * 初始化
   */

  initHotspot(krpano,someData,type){
    if (!someData) {
      return 
    }
    let mysd = JSON.parse(someData)
    mysd.hotspots.forEach(item => {
      this.addhotspot(krpano,item,type)
    });
  }

  /**
   * 显示隐藏热点
   */

  toggleHotspot(krpano,toggle){
    try {
      krpano.call(`set_hotspot_visible(${toggle})`);
    } catch (e) {
      e
    }
  }

  /**
   * 显示隐藏热点
   */

}


// toggleHotspot(krpano,someData,toggle){
//   if (!someData) {
//     return 
//   }
//   let mysd = JSON.parse(someData)
//   mysd.hotspots.forEach(item => {
//     krpano.set(`hotspot[${item.name}].visible`,toggle);
//   });
// }