<template>
  <div style="display: none">
    <slot />
  </div>
</template>
<script>
import PopupOptions from "./PopupOptions";

export default {
  name: "cesium-popup",
  props: {
    ...PopupOptions
  },
  inject: ["Cesium", "webGlobe"],
  watch: {
    position: {
      deep: true,
      handler () {
        this.update()
      }
    },
    showed:{
      deep: true,
      handler () {
        this.update()
      }
    }
  },
  mounted () {
    this.update()
  },
  destroyed () {
    this.unmount();
  },
  methods: {
    createCesiumObject () {
      let {
        webGlobe,
        position,
        options,
        container
      } = this;

      console.log('slot', this.$slots.default);
      if (this.$slots.default) {
        if(this.$slots.default[0].elm) {
          container = this.$slots.default[0].elm.innerHTML
        }else if(this.$slots.default[0].context.$children[0].$el){
          container = this.$slots.default[0].context.$children[0].$el.innerHTML
        }
      }

      return new CesiumZondy.Overlayer.PopupLayer(webGlobe.viewer, position, options, container);
    },
    mount () {
      const {
        webGlobe,
        popup,
      } = this;
      const viewer = webGlobe.viewer;
      return !viewer.isDestroyed() && popup.show();
    },
    unmount () {
      const {
        webGlobe
      } = this;
      const viewer = webGlobe.viewer;
      if( this.popup ){
        this.popup.remove()
        this.popup = undefined
      }
      return !viewer.isDestroyed();
    },
    update () {
      if (this.popup) {
        this.popup.remove();
        this.popup = undefined
      }
      if(this.showed){
        this.popup = this.createCesiumObject();
        this.mount();
      }
    }
  }
};

</script>
<style lang="scss">
.cesium-popup {
  position: absolute;
  text-align: center;
}

.cesium-popup-close-button {
  position: absolute;
  top: 0;
  right: 0;
  padding: 4px 4px 0 0;
  text-align: center;
  width: 18px;
  height: 14px;
  font: 16px/14px Tahoma, Verdana, sans-serif;
  color: #ffffff;
  text-decoration: none;
  font-weight: bold;
  background: transparent;
}

.cesium-popup-content-wrapper {
  text-align: left;
  padding: 10px;
  overflow-y: auto;
  font-size: 1.2em;
  z-index: 9999;
  border: 2px solid #46bcf1;
  box-shadow: 0px 0px 20px #46bcf1;
  -moz-box-shadow: 0px 0px 20px #46bcf1;
  -webkit-box-shadow: 0px 0px 20px #46bcf1;
  border-radius: 3px 3px 3px 3px;
  background: #46bcf188;
  background-size: 50% 50%;
  background-repeat: no-repeat;
}

.cesium-popup-content {
}

.cesium-popup-tip-container {
  margin: 0 auto;
  width: 40px;
  height: 20px;
  position: relative;
  overflow: hidden;
}

.cesium-popup-tip {
  background: #46bcf1;
  box-shadow: 0 3px 14px #46bcf1;
  width: 17px;
  height: 17px;
  padding: 1px;
  margin: -10px auto 0;
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}
</style>
