<template>
  <div class="hot-spot-list" app-border dir-left>
    <div class="title">
      {{$i18n.t('hotspot.add_hotspot')}}
      <i class="iconfont icon-material_prompt tool-tip-for-editor" v-tooltip="$i18n.t('hotspot.hotspot_tips')" />
    </div>
    <ul class="hotspot-type-list">
      <li
        class="hotspot-type-item"
        v-for="(item, index) in hotspotTypeList"
        :key="index"
        @click="open({
          isAdd: true,
          hotspotType: item.id,
        })"
      >
        <img class="icon" :src="item.icon" alt="" draggable="false">
        <div class="type-name">{{item.name}}</div>
        <img
          v-if="item.isExperience"
          class="exp-tag"
          src="@/assets/img/experience.png"
          alt=""
          draggable="false"
        >
      </li>
    </ul>

    <div class="total-count">{{$i18n.t('hotspot.current_hotspots')}}
      <span class="number">({{ someData.hotspots.length }})</span>
    </div>
    <div class="hots">
      <ul v-if="someData.hotspots.length > 0">
        <li v-for="(item, key) in someData.hotspots" :key="key" @click="open(item)">
          <img class="hot-spot-thumb" :src="item.img" alt="">
          <span class="hot-spot-title" v-title="item.hotspotTitle">{{ item.hotspotTitle }}</span>
          <i
            class="iconfont icon-editor_list_delete icon-delete"
            v-tooltip="$i18n.t('hotspot.delete')"
            @click.stop="deleIndex = key"
          />
          <div class="deletion-confirm-wrap">
            <div class="deletion-confirm" :class="deleIndex == key ? 'show' : 'hide'" v-clickoutside="clickoutside"
              @click.stop="deleteHot(item)">
              {{$i18n.t('hotspot.delete')}}
            </div>
          </div>
        </li>
      </ul>
      <div v-else class="empty-tip">
        <img src="@/assets/images/default/empty_hotspot_list.png" alt="">
        <div>{{$i18n.t('hotspot.no_hotspot')}}</div>
      </div>
    </div>
    <EditPanel
      class="adding-hotspot-panel"
      v-if="showPanel"
      :editTitle="editTitle"
      :show="showPanel"
      @save="save"
      @close="close"
    />
  </div>
</template>

<script>
import EditPanel from "./EditPanel"
import { mapGetters } from "vuex"
import browser from "@/utils/browser"
import hotspotTypeList from "./hotspotTypeList.js";

let mapFontSize = {
  12: 0.5,
  17: 1.5,
  20: 2,
}

export default {
  name: 'HotSpotList',
  components: {
    EditPanel,
  },
  data() {
    return {
      hotspotTypeList,
      
      showPanel: false,
      someData: { hotspots: [] },
      deleIndex: -1,
      editTitle: '',
    }
  },
  computed: {
    ...mapGetters({
      currentScene: "scene/currentScene",
      hotspot: 'hotspot',
      info: "info",
    }),
  },
  watch: {
    "$route.name": function () {
      this.showPanel = false
    },
    currentScene: {
      immediate: true,
      handler: function (newVal) {
        this.someData = newVal.someData || ""
        if (this.someData) {
          if (typeof this.someData == 'string') {
            try {
              this.someData = JSON.parse(this.someData)
            } catch (e) {
              console.error(e)
              return false
            }
          }
          if (!this.someData.hotspots) {
            this.someData.hotspots = []
          }
        }
        else {
          this.someData = { hotspots: [] }
        }
      },
    },
    showPanel(newVal) {
      this.$store.commit("UpdateIsEditingState", newVal)
      this.$store.commit("tags/setIsConfirmingPosi", false);
    },
  },
  mounted() {
    this.$bus.on("updateHotSpotHV", (data) => {
      let hptarget = this.someData.hotspots.find((item) => item.name.toLowerCase() == data.hpname.toLowerCase())
      console.log(hptarget);
      hptarget.ath = data.ath
      hptarget.atv = data.atv
    })

    this.$bus.on("openHotspot", (data) => {
      let idx = this.someData.hotspots.findIndex((item) => item.name == data)
      console.log(data);
      if (data == this.hotspot.name) {
        window.__krfn.utils.looktohotspot(this.$getKrpano(), this.hotspot.name)
        if (!this.showPanel) {
          this.open(this.someData.hotspots[idx])
        }
        return
      }
      if (this.editTitle == '新增' || this.editTitle == this.$i18n.t('hotspot.add')) {
        if (this.showPanel) {
          return this.$confirm({
            content: this.$i18n.t('hotspot.close_dialog'),
            ok: () => {
              this.deleteKRHotspot(this.hotspot)
              this.open(this.someData.hotspots[idx])
            }
          })
        }

      }

      this.open(this.someData.hotspots[idx])

    })
  },
  methods: {
    deleteKRHotspot(data) {
      this.$getKrpano().call("removehotspot(" + data.name + ",true);")
      this.$getKrpano().call("removeplugin(" + ("tooltip_" + data.name) + ",true);")
    },
    close(data) {
      if (data) {
        if (data.type == 'edit') {
          this.deleteKRHotspot(data.data)
          this.$bus.emit('addhotspot', data.data)
          let idx = this.someData.hotspots.findIndex(item => item.name == data.data.name)
          this.someData.hotspots[idx] = data.data
        }
        else {
          this.deleteKRHotspot(data.data)
        }
      }
      this.showPanel = false
    },

    updateInfo() {
      let iidx = this.info.scenes.findIndex(item => this.currentScene.sceneCode == item.sceneCode)
      if (iidx > -1) {
        this.info.scenes[iidx] = {
          ...this.currentScene
        }
      }
      this.$store.commit("SetInfo", this.info)
    },
    save(data) {
      let HV = window.__krfn.utils.getHotspotHV(this.$getKrpano(), data.name)
      data.ath = HV.ath
      data.atv = HV.atv
      let idx = this.someData.hotspots.findIndex((item) => item.name === data.name)
      if (idx <= -1) {
        this.someData.hotspots.push(data)
      }
      else {
        this.someData.hotspots[idx] = data
      }

      this.currentScene.someData = this.someData
      this.$msg.success(this.editTitle + this.$i18n.t('hotspot.success'))

      window.g_hotspotCurrentScale = mapFontSize[data.fontSize] || 1


      let iidx = this.info.scenes.findIndex(item => this.currentScene.sceneCode == item.sceneCode)
      if (iidx > -1) {
        this.info.scenes[iidx] = {
          ...this.currentScene
        }
      }

      this.updateInfo()

    },
    deleteHot(data) {
      this.someData.hotspots.splice(
        this.someData.hotspots.findIndex((item) => item.name === data.name),
        1
      )
      this.deleteKRHotspot(data)
      this.currentScene.someData = this.someData
      this.updateInfo()
      this.$msg.success(this.$i18n.t('hotspot.delete')+this.$i18n.t('hotspot.success'))
    },
    open(data) {
      let hotspotData = null
      if (data.isAdd) {
        this.editTitle = this.$i18n.t('hotspot.add')
        hotspotData = {
          hotspotType: data.hotspotType, // 热点类型，切换场景、图片、视频、音频、链接、文本等等
          
          hotspotIconType: 'system_icon', // 热点图标的类型，系统图标(system_icon)、自定义图片(custom_image)、序列帧(serial_frame)、个性标签(personalized_tag)
          img: '', // 热点图标
          serialFrameInfo: { // 热点图标类型为序列帧时，序列帧的数据
            frameNumber: 0, // 总帧数
            duration: 0, // 总播放时长（秒）
          },
          personalizedTagInfo: { // 热点图标类型为个性标签时，个性标签的数据
            isShowLine: true,
            lineDirection: '',
          },
          
          name: "_" + this.$randomWord(true, 8, 8),
          hotspotTitle: this.$i18n.t('hotspot.click_to_comfirm'),
          fontSize: 12,
          type: '',
          link: '',
          titleDisplayMode: 'always', // 'always' | 'never' | 'hover' 标题显示方式
          titlePosition: 'top', // 'top' | 'bottom' | 'left' | 'right' | 'custom' 标题相对图标位置
          ath: '',
          atv: '',
          size: 1,
          secne: '',
          hyperlink: '',
          textarea: '',
          image: [],
          audio: '',
          video: ''
        }
      } else {
        hotspotData = browser.CloneObject(data)
        // v1.3新增了“热点图标类型”
        if (!hotspotData.hotspotIconType) {
          hotspotData.hotspotIconType = 'system_icon'
        }
        // v1.3针对序列帧类型的热点图标，新增了序列帧信息
        if (!hotspotData.serialFrameInfo) {
          hotspotData.serialFrameInfo = {
            frameNumber: 0,
            duration: 0,
          }
          hotspotData.personalizedTagInfo = {
            isShowLine: true,
            lineDirection: '',
          }
        }
        // v1.3把visible: Boolean换成了titleDisplayMode
        if (hotspotData.visible) {
          hotspotData.titleDisplayMode = 'always'
        } else if (hotspotData.visible === false) {
          hotspotData.titleDisplayMode = 'never'
        }
        // v1.3新增了titlePosition
        if (!hotspotData.titlePosition) {
          hotspotData.titlePosition = 'top'
        }
      }

      this.$store.commit("SetHotspot", hotspotData)
      this.showPanel = true

      if (!data.isAdd) {
        this.editTitle =  this.$i18n.t('hotspot.edit')
        window.__krfn.utils.looktohotspot(this.$getKrpano(), data.name)
      }
    },
    clickoutside() {
      if (this.deleIndex > -1) {
        this.deleIndex = -1
      }
    },
  },
}
</script>

<style lang="less" scoped>
.hot-spot-list {
  padding: 20px;
  display: flex;
  flex-direction: column;
  background: #252526;
  position: relative;

  > .title {
    flex: 0 0 auto;
    font-size: 18px;
    color: #fff;
    flex: 0 0 auto;
    margin-bottom: 24px;

    >i {
      font-size: 12px;
      position: relative;
      top: -2px;
    }
  }

  > .hotspot-type-list {
    flex: 0 0 auto;
    margin-right: -10px;
    > .hotspot-type-item {
      position: relative;
      display: inline-flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 72px;
      height: 72px;
      background: #313131;
      border-radius: 2px;
      border: 1px solid #404040;
      margin-right: 9px;
      margin-bottom: 9px;
      cursor: pointer;
      > .icon {
        width: 28px;
        height: 28px;
        margin-bottom: 3px;
      }
      > .type-name {
        font-size: 12px;
        color: #FFFFFF;
      }
      > .exp-tag {
        position: absolute;
        top: 0;
        right: 0;
        width: 30px;
        height: 30px;
      }
    }
  }

  .total-count {
    flex: 0 0 auto;
    margin-top: 24px;
    font-size: 18px;
    color: #FFFFFF;

    .number {
      font-size: 14px;
      color: rgba(255, 255, 255, 0.6);
      position: relative;
      top: -1px;
    }
  }

  .hots {
    flex: 1 0 1px;
    margin-top: 16px;
    background: available;
    background: #1A1B1D;
    border-radius: 4px;
    border: 1px solid #404040;
    position: relative;
    overflow: auto;

    ul {
      padding: 10px;

      li {
        position: relative;
        height: 40px;
        border-radius: 2px;
        display: flex;
        align-items: center;
        padding: 0 5px 0 10px;

        &:hover {
          background: #252526;

          .icon-delete {
            display: block;
          }
        }

        >.hot-spot-thumb {
          width: 18px;
        }

        >.hot-spot-title {
          flex: 1 1 auto;
          margin-left: 10px;
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
        }

        >.icon-delete {
          margin-left: 12px;
          display: none;
          cursor: pointer;
          padding: 5px;

          &:hover {
            color: #fa5555;
          }
        }

        >.deletion-confirm-wrap {
          position: absolute;
          top: 0;
          bottom: 0;
          right: 0;
          width: 44px;
          overflow: hidden;
          pointer-events: none;
          border-top-right-radius: 2px;
          border-bottom-right-radius: 2px;

          >.deletion-confirm {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
            background: #FA5555;
            transition: right 0.3s;
            cursor: pointer;
            text-align: center;
            font-size: 12px;
            color: #fff;
            pointer-events: auto;

            &::after {
              content: '';
              height: 100%;
              vertical-align: middle;
              display: inline-block;
            }

            &.show {
              right: 0;
            }

            &.hide {
              right: -44px;
            }
          }
        }
      }
    }

    .empty-tip {
      text-align: center;
      position: absolute;
      text-align: center;
      width: 100%;
      top: 50%;
      transform: translateY(-50%);

      img {
        width: 125px;
      }

      div {
        margin-top: 20px;
        color: rgba(255, 255, 255, 0.6);
        font-size: 14px;
      }
    }
  }

  .ui-button {
    width: 100%;
  }

  .adding-hotspot-panel {
    position: absolute;
    top: 0;
    height: 100%;
    right: 0;
    width: 100%;
  }
}
</style>
