<template>
  <div class="wcontainer">
    <img class="logo" :src="require(`@/assets/images/logo.png`)" alt="" />
    <Loading :barWidth="loadprogress" v-if="!hadLoaded" />
    <transition appear name="dm">
      <vmenu @clickItem="handleItem" />
    </transition>

    <transition appear name="dm">
      <vintroduce v-if="currentItem == 'info'" />
    </transition>


    <div class="infobg">
      <img :src="require('@/assets/images/info_bg.png')" alt="">
      <p>特展馆·二楼<br />镇院之宝展厅</p>
    </div>

    <div class="right-title">
      <img :src="require('@/assets/images/title.png')" alt="">
    </div>


    <div id="player">
      <div class='widgets-doll-labels'> </div>
      <canvas></canvas>
    </div>



  </div>
</template>

<script>
import vmenu from "@/components/menu";
import Loading from "@/components/loading";
import vintroduce from "@/components/introduce";

const transitionTime = 2000

let labelClick = (e) => {
  if (e.centerCamePosi) {

    console.log(e);
    let { x, y, z } = e.centerCamePosi
    window.transitions.start(
      window.lerp.vector(window.viewer.camera.position,
        new window.THREE.Vector3(x, y, z)),
      transitionTime,
      () => {
        e.elem[0].style.display == 'block' && e.elem.addClass('detailShow')
        e.elem.siblings().removeClass('detailShow')
        e.elem.siblings().css({
          "opacity": 0,
          "pointer-events": "none"
        })
        console.log(e.elem.siblings());
        let wlmesh = window.viewer.model.getObjectByName("WL48_ping")
        wlmesh.material.emissiveMap = e.emissiveTexture;
        wlmesh.material.dispose();
        window.showWenli = true

        // window.viewer.labels.forEach(item => {

        //     if (centerLabel.title == item.title) {
        //       this.centerLabel = centerLabel
        //     }
        //     else {
        //       item.elem.removeClass('detailShow')
        //     }
        //   })
      },
      0/* Delay */,
      window.easing.easeInOutQuad,
      'cameraFly2');
  }
}


let infoLabels = [{ "title": "宝珠钮", "posInModel": [-4.6302, 20.0953, 2.9731] },
{ "title": "瓶身", "posInModel": [-11.7969, -1.7939, 3.9055] },
{ "title": "瓶盖", "posInModel": [-6.0636, 16.2615, 3.4633] }]

let picLabels = [
  {
    "title": "竹子",
    "entitle": "Diagonal squares pattern",
    "infodesc": "清代郑燮咏竹：咬定青山不放松，立根原在破岩中;千磨万击还坚劲，任尔东西南北风。",
    "imgsrc": "zhu",
    "labeltype": "canShow",
    "posInModel": [
      -9.9383,
      1.517,
      8.4702
    ],
    "centerCamePosi": {
      x: -0.4650665393060424,
      y: 0.0106499843061736,
      z: 0.43269467554899155,
    },
    clickFun: labelClick
  },
  {
    "title": "牡丹",
    "entitle": "Diagonal squares pattern",
    "infodesc": "唐朝·刘禹锡《赏牡丹》：庭前芍药妖无格，池上芙蕖净少情。唯有牡丹真国色，花开时节动京城。",
    "imgsrc": "mudan",
    "labeltype": "canShow",
    "posi": "right",
    "posInModel": [
      -10.5926,
      -6.1392,
      1.8685
    ],
    "centerCamePosi": {
      x: -0.5357516927819861,
      y: -0.3176574374999963,
      z: 0.1252552053945424
    },
    clickFun: labelClick
  },
  {
    "title": "梅",
    "entitle": "Diagonal squares pattern",
    "labeltype": "canShow",
    "infodesc": "元代王冕咏梅：不要人夸好颜色，只留清气满乾坤。",
    "imgsrc": "mei",
    "posi": "right",
    "posInModel": [
      12.4014,
      5.4827,
      2.9209
    ],
    "centerCamePosi": {
      x: 0.5882886392634421,
      y: 0.2267149762544653,
      z: 0.07836955309150107
    },
    clickFun: labelClick
  },
  {
    "title": "松",
    "entitle": "Diagonal squares pattern",
    "labeltype": "canShow",
    "infodesc": "唐代白居易咏松：岁暮满山雪，松色郁青苍。彼如君子心，秉操贯冰霜。",
    "imgsrc": "song",
    "posInModel": [
      -5.2427,
      1.903,
      -12.1582
    ],
    "centerCamePosi": {
      x: -0.2604583387784285,
      y: 0.00816509249833358,
      z: -0.5794133027654209
    },
    clickFun: labelClick
  }
]



export default {
  components: { vmenu, Loading, vintroduce },
  data() {
    return {
      scene: null,
      camera: null,
      renderer: null,
      controls: null,
      rafId: null,
      container: null,
      containerW: 0,
      containerH: 0,
      loadprogress: 0,
      hadLoaded: false,
      modelLoaded: null,
      timer: null,
      obj: null,
      labelRenderer: null,
      label: null,
      meshGroup: null,
      currentItem: 'info',
      centerLabel: null,
      fgtimer: null
    };
  },
  methods: {

    handleItem(data) {
      this.currentItem = data.id
      switch (data.id) {
        case "info":
          this.info()
          break;
        case "pic":
          this.pic()
          break;
        default:
          break;
      }
    },


    // 重置相机位置
    resetCameraView() {
      window.transitions.start(
        window.lerp.vector(window.viewer.camera.rotation,
          new window.THREE.Vector3(-6.123233995736765e-17, 0, 0)),
        transitionTime,
        () => { },
        0/* Delay */,
        window.easing.easeInOutQuad,
        'cameraFly');
    },

    info() {

      this.resetCameraView()
      window.viewer.removeAllLabels()
      window.viewer.model.getObjectByName("wl48_biaochi").visible = true

      //模型位置
      window.transitions.start(
        window.lerp.vector(window.viewer.model.position,
          new window.THREE.Vector3(0, 0, 0)),
        transitionTime,
        () => { },
        0/* Delay */,
        window.easing.easeInOutQuad,
        'cameraFly');

      //模型旋转角度

      window.transitions.start(
        window.lerp.vector(window.viewer.model.rotation,
          new window.THREE.Vector3(0, 0, 0)),
        transitionTime,
        () => {
          window.viewer.loadLabelsFromData(infoLabels)
          window.viewer.control.maxPolarAngle = Math.PI;
          window.viewer.control.minPolarAngle = 0;
        },
        0/* Delay */,
        window.easing.easeInOutQuad,
        'cameraFly');

      //相机位置
      window.transitions.start(
        window.lerp.vector(window.viewer.camera.position,
          new window.THREE.Vector3(0, 0, 0.78)),
        transitionTime,
        () => { },
        0/* Delay */,
        window.easing.easeInOutQuad,
        'cameraFly');


    },

    pic() {

      this.resetCameraView()
      window.viewer.removeAllLabels()
      window.viewer.model.getObjectByName("wl48_biaochi").visible = false

      //相机位置
      window.transitions.start(
        window.lerp.vector(window.viewer.camera.position,
          new window.THREE.Vector3(0, 3.8901816405972494e-17, 0.6353148749999993)),
        transitionTime,
        () => { },
        0/* Delay */,
        window.easing.easeInOutQuad,
        'cameraFly');

      //模型位置

      window.transitions.start(
        window.lerp.vector(window.viewer.model.position,
          new window.THREE.Vector3(0, 0, 0)),
        transitionTime,
        () => { },
        0/* Delay */,
        window.easing.easeInOutQuad,
        'cameraFly');

      //模型旋转角度

      window.transitions.start(
        window.lerp.vector(window.viewer.model.rotation,
          new window.THREE.Vector3(0, 0, 0)),
        transitionTime,
        () => {
          window.viewer.control.maxPolarAngle = Math.PI / 1.5;
          window.viewer.control.minPolarAngle = Math.PI / 6;
          window.viewer.loadLabelsFromData(picLabels)
          // window.showWenli = true
        },
        0/* Delay */,
        window.easing.easeInOutQuad,
        'cameraFly');


    },

    // 窗口改变
    onresize() {

    },



  },
  watch: {
    centerLabel(newVal) {
      if (newVal) {
        newVal.elem[0].style.display == 'block' && newVal.elem.addClass('detailShow')
        let wlmesh = window.viewer.model.getObjectByName("WL48_ping")
        wlmesh.material.emissiveMap = this.centerLabel.emissiveTexture;
        wlmesh.material.dispose();
      }
    },
    currentItem(newVal) {
      if (newVal != 'pix') {
        window.showWenli = false
      }
    }
  },
  mounted() {
    this.timer = setInterval(() => {
      if (!this.hadLoaded) {
        if (this.loadprogress < 80) {
          this.loadprogress += 0.8;
        } else {
          if (this.modelLoaded) {
            if (this.loadprogress <= 100) {
              this.loadprogress += 2;
            } else {
              this.hadLoaded = true;
              clearInterval(this.timer);
              this.timer = null;
            }
          }
        }
      }
    }, 10);
    window.viewer = new window.Viewer(0, window.$("#player")[0])

    window.viewer.addEventListener("hadLoaded", () => {
      this.modelLoaded = true
      window.viewer.loadLabelsFromData(infoLabels)
      window.viewer.control.maxPolarAngle = Math.PI;
      window.viewer.control.minPolarAngle = 0;
    })

    window.viewer.addEventListener("delayUpdate", (e) => {
      // setTimeout(() => {
      // if (this.currentItem == 'pic') {
      //   let centerLabel = window.viewer.getCLabel()
      //   if (centerLabel) {
      //     window.viewer.labels.forEach(item => {
      //       if (centerLabel.title == item.title) {
      //         this.centerLabel = centerLabel
      //       }
      //       else {
      //         item.elem.removeClass('detailShow')
      //       }
      //     })
      //   }
      // }
      // });
    })

  },

  beforeDestroy() {

  },
};
</script>

<style lang="less" scoped>
.wcontainer {
  width: 100%;
  height: 100%;
  position: relative;
  background-repeat: no-repeat;
  color: #ac5b5b;

  .logo {
    position: absolute;
    top: 50px;
    left: 66px;
    width: 100px;
    z-index: 9999;
  }

  .infobg {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 250px;
    height: auto;

    >p {
      position: absolute;
      top: 70%;
      left: 50%;
      transform: translate(-50%, -50%);
      color: rgba(255, 255, 255, 0.8);
      font-family: SimSun;
      line-height: 1.5;
    }
  }

  .right-title {
    position: absolute;
    right: 90px;
    top: 14%;
    width: 150px;
    height: auto;

    >img {
      width: 100%;
    }
  }


  #player {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }
}

.dm-enter {
  opacity: 0;
}

.dm-leave-to {
  opacity: 0;
}

.dm-enter-active,
.dm-leave-active {
  transition: all 2.8s ease;
}
</style>


<style lang="less">
*,
:after,
:before {
  box-sizing: border-box;
}





.widgets-doll-labels {
  overflow: hidden;
  position: absolute;
  pointer-events: none;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;

  .room-label {
    position: absolute;
    width: 0;
    height: 0;
    transform: translateZ(0);
    animation: room-label 0.3s ease 0.1s;
    animation-fill-mode: both;
    cursor: pointer;

    &:after {
      content: "";
      display: block;
      position: absolute;
      height: 1px;
      width: 88px;
      // background-image: url("img/labelline.svg");
      background-color: #A99979;
      background-size: contain;
      background-repeat: no-repeat;
      bottom: 0;
      right: 0;
    }

    a {
      display: block;
      position: absolute;
      line-height: 2;
      top: 50%;
      right: 88px;
      transform: translateY(-50%);
      text-align: center;
      white-space: nowrap;
      padding: 6px 14px;
      pointer-events: auto;
      background-repeat: no-repeat;
      background-size: 100% 100%;
      background-image: url('~@/assets/images/tab.svg');
      color: #fff;
      box-sizing: border-box;

      >p {
        margin: 0;
        width: 100%;
        height: 100%;
      }

      span {
        white-space: nowrap;
        user-select: none;
      }
    }
  }

  .right {
    a {
      left: 88px;
      right: unset;
    }

    &:after {
      content: "";
      display: block;
      position: absolute;
      bottom: 0;
      right: unset;
      left: 0;
    }

  }

  .canShow {
    &:after {
      content: "";
      display: block;
      position: absolute;
      height: 38px;
      width: 38px;
      background-image: url('~@/assets/images/point.svg');
      background-color: transparent;
      background-size: cover;
      background-repeat: no-repeat;
      bottom: 0;
      right: 0;
      top: 0;
      left: 0;
    }

    &::before {
      content: '';
      width: 100px;
      height: 2px;
      display: inline-block;
      background-color: #A99979;
      position: absolute;
      transform: rotate(45deg);
      top: -30px;
      right: -20px;
    }

    &:hover {
      color: #fff;

      &:after {
        background-image: url('~@/assets/images/point_active.svg');
        content: '';
      }

      &::before {
        content: '';
        background-color: #fff !important;
      }

      a {

        >p,
        span {
          color: #fff;
        }

        .sline {
          background-color: #fff;
        }
      }
    }

    a {
      display: block;
      position: absolute;
      line-height: 2;
      transition: .1s opacity ease;
      top: -103px;
      right: 51px;
      transform: translateY(0);
      text-align: center;
      white-space: nowrap;
      padding: 6px 14px;
      pointer-events: auto;
      background-repeat: no-repeat;
      background-size: 100% 100%;
      background-image: none;
      color: #fff;
      box-sizing: border-box;



      &::after {
        content: '';
      }

      &::before {
        content: '';
      }

      >p {
        margin: 0;
        width: 100%;
        height: 100%;
        text-align: left;
      }

      span {
        white-space: nowrap;
        padding-right: 30px;
        user-select: none;
        color: #A99979;
        font-size: 16px;
        text-align: left;
      }

      .sline {
        width: 100%;
        height: 2px;
        background-color: #A99979;
      }

      .enspan {
        font-size: 12px;
      }
    }

    &.right {
      a {
        left: 88px;
        right: unset;
      }


      &::before {
        content: '';
        width: 100px;
        height: 2px;
        display: inline-block;
        background-color: #A99979;
        position: absolute;
        transform: rotate(-45deg);
        top: -30px;
        left: 18px;
        right: unset;
      }

      a {
        >p {
          text-align: right;
          padding-right: 0;
          padding-left: 30px;
        }

        span {
          text-align: right;
          padding-right: 0;
          padding-left: 30px;
        }
      }
    }

    &.detailShow {
      z-index: 1088 !important;

      &:after {
        content: "";
        background-image: url('~@/assets/images/point_active.svg');
      }

      &::before {
        content: "";
        background-color: #fff;
      }

      a {
        opacity: 0;
      }

      .picdetail {
        opacity: 1;
        pointer-events: auto;

        &::before {
          content: '';
          background-color: #fff !important;
        }
      }

      &.right {
        .picdetail {
          top: -254px;
          left: 163px;
          right: unset;

          &::before {
            content: '';
            display: inline-block;
            width: 60px;
            height: 2px;
            background-color: #A99979;
            position: absolute;
            left: -60px;
            right: unset;
          }
        }
      }
    }
  }




}

.picdetail {
  position: absolute;
  z-index: 999;
  opacity: 0;
  top: -254px;
  right: 163px;
  width: 300px;
  background-image: url('~@/assets/images/pop.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  color: #fff;
  padding: 30px 36px;
  min-height: 378px;
  transition: .1s opacity ease;

  &::before {
    content: '';
    display: inline-block;
    width: 98px;
    height: 2px;
    background-color: #A99979;
    position: absolute;
    right: -98px;
    top: 189px;
  }

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .pinfo {
    width: 100%;
    margin-top: 20px;

    >img {
      width: 100%;
    }

    >p {
      text-align: justify;
      font-size: 14px;
      color: rgba(255, 255, 255, 0.8);
      margin-top: 10px;
      line-height: 1.5;
    }
  }
}



@-webkit-keyframes room-label {
  0% {
    opacity: 0;
    margin-top: 8.75px;
  }

  to {
    opacity: 1;
    margin-top: 0;
  }
}

@keyframes room-label {
  0% {
    opacity: 0;
    margin-top: 8.75px;
  }

  to {
    opacity: 1;
    margin-top: 0;
  }
}
</style>