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");
    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, 0.5);
  }

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


  /**
   * 添加热点（展示端）
   */

  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");

    if (param.lineTxt) {
      krpano.call(`${!param.link ? "addLineTxtHotSpot" : "addLineTxtHotSpotWithBg"}(
        ${param.img},
        ${param.name},
        ${param.hotspotTitle},
        ${param.ath || ath},
        ${param.atv || atv},
        ${param.img.indexOf('line-l.png')>-1},
        ${type},
        ${param.link},
        ${true},
        ${param.visible})`);
    } else {
      param.img = param.img.replace(/old\//g, "");
      let split = param.img.split("xulie/");
      if (!param.link) {
        param.img = split[0] + "xulie/old/" + split[1];
      }

      krpano.call(`${!param.link ? "addImgTextHotSpotWithoutBg" : "addImgTextHotSpot"}(
      ${param.img},
      ${param.name},
      ${param.hotspotTitle},
      ${param.ath || ath},
      ${param.atv || atv},
      ${true},
      ${type},
      ${param.link},
      ${true},
      ${param.visible})`);
    }
  }

  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") || 120;
    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("edit") > -1) {
      vue.$bus.emit("openHotspot", id);
    } else {
      sceneCode &&
        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;
    }
  }

  
}
