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


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",
    "labeltype": "canShow",
    "posInModel": [
      0.2358,
      24.1179,
      4.7176
    ]
  },
  {
    "title": "斜方格纹",
    "entitle": "Diagonal squares pattern",
    "labeltype": "canShow",
    "posi": "right",
    "posInModel": [
      4.9625,
      16.7108,
      3.7418
    ]
  },
  {
    "title": "卷草纹",
    "entitle": "Diagonal squares pattern",
    "labeltype": "canShow",
    "posi": "right",
    "posInModel": [
      8.1682,
      12.9736,
      5.3315
    ]
  },
  {
    "title": "缠枝扁菊纹",
    "entitle": "Diagonal squares pattern",
    "labeltype": "canShow",
    "posInModel": [
      -9.4563,
      11.9996,
      5.5396
    ]
  }
]

export default {
  components: { vmenu, Loading, vintroduce },
  data() {
    return {
      scene: null,
      camera: null,
      renderer: null,
      controls: null,
      textrueLoader: 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: ''
    };
  },
  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)),
        3000,
        () => { },
        0/* Delay */,
        window.easing.easeInOutQuad,
        null);
    },

    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)),
        3000,
        () => { },
        0/* Delay */,
        window.easing.easeInOutQuad,
        null);

      //模型旋转角度

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

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

        
    },

    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, 2.1020988923367118e-17, 0.34329880154837705)),
        3000,
        () => {
          window.viewer.loadLabelsFromData(picLabels)
        },
        0/* Delay */,
        window.easing.easeInOutQuad,
        null);

      //模型位置

      window.transitions.start(
        window.lerp.vector(window.viewer.model.position,
          new window.THREE.Vector3(0, -0.17, 0)),
        3000,
        () => { },
        0/* Delay */,
        window.easing.easeInOutQuad,
        null);

      //模型旋转角度

      window.transitions.start(
        window.lerp.vector(window.viewer.model.rotation,
          new window.THREE.Vector3(0, 0, 0)),
        3000,
        () => {
          window.viewer.control.maxPolarAngle = Math.PI / 1.5;
          window.viewer.control.minPolarAngle = Math.PI / 6;
        },
        0/* Delay */,
        window.easing.easeInOutQuad,
        null);


    },

    // 窗口改变
    onresize() {

    },



  },
  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.addEventListener("view.changed", () => {
      if (this.currentItem == 'pic') {

        let centerLabel = window.viewer.getCLabel()

        if (centerLabel) {
          window.viewer.labels.forEach(item => {
            item.elem.removeClass('detailShow')
          })
          centerLabel.elem[0].style.display == 'block' && centerLabel.elem.addClass('detailShow')
        }
      }

    })


  },
  watch: {
    hadLoaded(newVal) {

    },
  },
  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;
      transition: all 0.3s;
    }

    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;
      transition: all 0.3s;
    }

  }

  .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;
      transition: all 0.3s;
    }

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

    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 {
      a {
        opacity: 0;
      }

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

      &.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;
  transition: .1s .1s opacity ease;

  &::before {
    content: '';
    display: inline-block;
    width: 98px;
    height: 2px;
    background-color: #A99979;
    position: absolute;
    right: -98px;
    top: 50%;
    transform: translateY(-50%);
  }

  .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>