<template>
  <div class="header" @touchmove.prevent>
    <div class="left" :class="{ show: player.showVR }">
      <div v-show="mode != 'panorama' && !isApp" :class="{ disabled: flying }" class="back-pano" @click="onChangeMode">
        <ui-icon type="show_back"></ui-icon>
      </div>
      <div v-show="mode == 'panorama'" class="back" @click="onBack">
        <ui-icon type="_back"></ui-icon>
      </div>
    </div>
    <div
      class="title"
      :class="{ up: player.showDescription, drak: mode != 'panorama', empty: !description }"
      @click="onShowDescription"
      v-show="player.showWidgets"
    >
      <div>
        <span>
          {{ metadata.title }}
        </span>
        <i class="iconfont icon-pull-down"></i>
      </div>
    </div>
    <div class="right" :class="{ more: player.showMore }" v-show="player.showWidgets" @click="onShowMore">
      <img  v-if="false" :src="require(`@/assets/images/icon/${player.lang}.png`)" alt="" />
      <keep-alive>
        <transition
          appear
          name="custom-classes-transition"
          enter-active-class="animated fadeInUp short faster"
          leave-active-class="animated fadeOutDown short faster"
        >
          <!-- <div v-if="player.showMore"> -->
          <div v-if="false">
            <ul>
              <li @click.stop="onMenuClick('zh_CN')">
                <img :src="require(`@/assets/images/icon/zh_CN.png`)" alt="" />
                <span>简体中文</span>
              </li>
              <li @click.stop="onMenuClick('zh_HK')">
                <img :src="require(`@/assets/images/icon/zh_HK.png`)" alt="" />
                <span>繁体中文</span>
              </li>
              <li @click.stop="onMenuClick('en')">
                <img :src="require(`@/assets/images/icon/en.png`)" alt="" />
                <span>English</span>
              </li>
            </ul>
          </div>
        </transition>
      </keep-alive>
    </div>
    <transition
      appear
      name="custom-classes-transition"
      enter-active-class="animated fadeInUp short faster"
      leave-active-class="animated fadeOutDown short faster"
    >
      <div class="content" :class="{ drak: mode != 'panorama' }" v-if="player.showDescription" @click="onShowDescription">
        <div>
          <div v-html="description"></div>
        </div>
      </div>
    </transition>
    <teleport to="body">
      <template v-if="isApp">
        <transition
          appear
          name="custom-classes-transition"
          enter-active-class="animated fadeInUp short faster"
          leave-active-class="animated fadeOutDown short faster"
        >
          <div class="app-share" v-if="showShare">
            <ul :class="{ flex: getLocale() == 'zh' }">
              <li @click="onShare('weixin')">
                <ui-icon type="_wechat"></ui-icon>
                <div>{{ $t("share.wechat") }}</div>
              </li>
              <li @click="onShare('weixinFriend')">
                <ui-icon type="_friend"></ui-icon>
                <div>{{ $t("share.friends") }}</div>
              </li>
              <li @click="onShare('qq')">
                <ui-icon type="_qq"></ui-icon>
                <div>{{ $t("share.qq") }}</div>
              </li>
              <li v-if="getLocale() != 'zh'" @click="onShare('faceBook')">
                <ui-icon type="icon_share_facebook"></ui-icon>
                <div>{{ $t("share.facebook") }}</div>
              </li>
              <li v-if="getLocale() != 'zh'" @click="onShare('whatsApp')">
                <ui-icon type="icon_share_whatsapp"></ui-icon>
                <div>{{ $t("share.whatsApp") }}</div>
              </li>
              <li @click="onShare('copy')">
                <ui-icon type="link1"></ui-icon>
                <div>{{ $t("share.copyLink") }}</div>
              </li>
            </ul>
            <div @click="showShare = false">{{ $t("common.cancel") }}</div>
          </div>
        </transition>
      </template>
    </teleport>

    <div class="url-share" v-show="showCopy">
      <div>
        <div class="tips">
          <h4>{{ $t("share.shareLinkTips") }}</h4>
          <i class="iconfont iconshow_cancel" @click="showCopy = false"></i>
        </div>
        <div class="url">{{ copyLink }}</div>
        <div class="btns">
          <ui-button class="cancel" @click="showCopy = false">{{ $t("common.cancel") }}</ui-button>
          <ui-button class="primary" :data-clipboard-text="copyLink" ref="copy$">{{ $t("share.fastCopy") }}</ui-button>
        </div>
      </div>
    </div>
  </div>
  <div class="rules-btn" v-if="showRules">
    <ui-button type="cancel" @click="cancelRules">取消</ui-button>
    <ui-button type="primary" :class="{ disabled: !next }" @click="confirmRules">确定起点({{ rulesIndex }}/2)</ui-button>
  </div>
</template>
<script setup>
import ClipboardJS from "clipboard";
import browser from "@/utils/browser";
import { useStore } from "vuex";
import { Dialog } from "@/global_components/";
import { IsApp, MessageToApp, NotchHeight } from "@/utils/platform";
import { useMusicPlayer } from "@/utils/sound";
import { onMounted, watch, computed, ref, nextTick } from "vue";
import { useApp, getApp } from "@/app";
import i18n, { getLocale, setI18nLanguage, useI18n, loadLocaleMessages } from "@/i18n";

const store = useStore();

const { t } = useI18n({ useScope: "global" });
let getUrl = (href, queryArr) => {
  queryArr.forEach((item) => {
    if (!browser.hasURLParam(item.key)) {
      let ttt = href.split("index.html?");
      href = `${ttt[0]}index.html?${item.key}=${item.val}&${ttt[1]}`;
    } else {
      href = browser.replaceQueryString(href, item.key, item.val);
    }
  });

  return href;
};
let share_url = browser.getURLParam("share_url");
if (share_url) {
  share_url = decodeURIComponent(share_url);
} else {
  share_url = location.href.split("#")[0];
}

const deploy = ref(process.env.VUE_APP_DEPLOY);
const musicPlayer = useMusicPlayer();
const loadingLogoFile = computed(() => store.getters["scene/loadingLogoFile"]);
const metadata = computed(() => store.getters["scene/metadata"]);
const controls = computed(() => {
  return metadata.value.controls;
});
const showMusicPlaying = ref(false);
const showMusic = computed(() => store.getters["scene/metadata"].music);
const player = computed(() => store.getters["player"]);
const flying = computed(() => store.getters["flying"]);
const copy$ = ref(null);
const copyLink = computed(() => {
  return share_url.replace("&app", "");
});
const isApp = ref(IsApp);
const showCopy = ref(false);
const showShare = ref(false);
const isMusicPlaying = ref(false);

const mode = computed(() => store.getters["mode"]);
const description = computed(() => metadata.value.description);

watch(
  () => showCopy.value,
  (val, old) => {
    store.commit("SetPlayerOptions", {
      showMap: !showCopy.value,
      showToolbar: !showCopy.value,
    });
  },
  {
    deep: true,
  }
);
watch(
  () => showShare.value,
  (val, old) => {
    store.commit("SetPlayerOptions", {
      showMap: !showShare.value,
      showToolbar: !showShare.value,
    });
  },
  {
    deep: true,
  }
);

onMounted(() => {
  useApp().then((app) => {
    app.TagManager.on("tagManager.markTagPos", (res) => {
      next.value = true;
    });
  });

  new ClipboardJS(copy$.value.$el).on("success", function (e) {
    e.clearSelection();
    showCopy.value = false;
    Dialog.toast({ content: t("toast.copySuccess"), type: "success" });
  });

  nextTick(() => {
    document.querySelector(".player").addEventListener("touchstart", () => {
      if (player.value.showMore) {
        store.commit("SetPlayerOptions", {
          showMore: false,
          showMap: true,
          showToolbar: true,
        });
      } else if (player.value.showDescription) {
        store.commit("SetPlayerOptions", {
          showDescription: false,
          showMap: true,
          showToolbar: true,
        });
      }
    });

    window.Back = () => {
      onBack();
    };
    musicPlayer.on("play", () => (showMusicPlaying.value = true));
    musicPlayer.on("pause", () => (showMusicPlaying.value = false));
  });
});
const onListen = () => {
  // this.$bus.on('orientation', () => {
  if (NotchHeight) {
    this.$el.style.top = NotchHeight + "px";
  }

  // 发送返回按钮信息
  const rect = document.querySelector(".back").getBoundingClientRect();
  const data = {
    BackBtnInfo: {
      left: rect.left,
      top: rect.top,
      radius: rect.width / 2,
    },
  };
  if (browser.detectAndroid()) {
    MessageToApp(JSON.stringify(data));
  } else {
    MessageToApp(data);
  }
  // })
};
const onBack = () => {
  // player.value.showVR && store.commit('showVR')
  if (player.value.showVR) {
    MessageToApp("VRMode0");
    store.commit("showVR");
  }
};
const onShowMore = () => {
  let show = !player.value.showMore;
  store.commit("SetPlayerOptions", {
    showMore: show,
    showMap: show == false,
    showToolbar: show == false,
    showDescription: false,
  });
};
const onShowDescription = () => {
  let show = !player.value.showDescription;
  store.commit("SetPlayerOptions", {
    showMore: false,
    showMap: show == false,
    showToolbar: show == false,
    showDescription: show,
  });
};
const onMusicClick = () => {
  showMusicPlaying.value ? musicPlayer.pause() : musicPlayer.play();
};
const onMenuClick = (name) => {
  store.commit("SetPlayerOptions", {
    showMore: false,
    showDescription: false,
    showMap: true,
    showToolbar: true,
  });

  nextTick(() => {
    loadLocaleMessages(i18n, name).then(() => {
      setI18nLanguage(i18n, name);
      store.commit("SetPlayerOptions", {
        lang: name,
      });

      let tmp = getUrl(window.location.href, [
        {
          key: "lang",
          val: name,
        },
      ]);
      console.log(tmp);
      history.replaceState(null, null, tmp);
    });
  });
};
const onShare = (name) => {
  if (name == "copy") {
    showShare.value = false;
    nextTick(() => {
      showCopy.value = true;
    });
  } else {
    MessageToApp(`Share-${name}`);
    showCopy.value = false;
    showShare.value = false;
  }
};
const onChangeMode = () => {
  store.commit("setMode", "panorama");
};

const rulesIndex = ref(1);
// const emits = defineEmits(['close'])
const confirmRules = () => {
  next.value = false;
  getApp().TagManager.confirmMeasure(rulesIndex.value);
  if (rulesIndex.value < 2) {
    Dialog.toast({
      content: "起点确定完毕，请定位终点",
    });
    rulesIndex.value++;
  } else {
    cancelRules();
  }
};
const showRules = ref(false);
const cancelRules = () => {
  showRules.value = false;
  rulesIndex.value = 1;
  getApp().TagManager.cancelMeasure();
  store.commit("toggleUi");
};
const next = ref(false);
</script>

<style lang="scss" scoped>
.disable {
  opacity: 1;
}
.rules-btn {
  background: rgba(0, 0, 0, 0.8);
  position: fixed;
  height: 1.3333rem;
  width: 100%;
  bottom: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  .ui-button {
    width: 40%;
    height: 1.0667rem;
    &.cancel {
      margin-right: 10px;
    }
  }
}
.header {
  position: absolute;
  top: 0.3rem;
  left: 0;
  height: 1.15789rem;
  width: 100%;
  z-index: 101;
  color: #fff;
  display: flex;
  align-items: center;
  text-shadow: 0px 0px 4px rgba(0, 0, 0, 0.4);
  &.app {
    top: 1rem;
  }
  .left {
    width: 1rem;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    &.show {
      visibility: visible;
      pointer-events: auto;
      .back {
        visibility: visible;
      }
    }
    .back {
      width: 0.78rem;
      height: 0.78rem;
      border-radius: 50%;
      background-color: rgba(0, 0, 0, 0.3);
      display: flex;
      align-items: center;
      justify-content: center;
      visibility: hidden;
      position: relative;
      i {
        font-size: 0.3rem;
        width: auto;
        position: static;
      }
    }
    .back-pano {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      // padding-left: 15px;
      position: relative;
      i {
        font-size: 0.6rem;
        position: static;
      }
    }
  }
  .right {
    position: relative;
    width: 1.28rem;
    height: 100%;
    padding-right: 15px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    z-index: 11;
    > i {
      font-size: 0.6rem;
    }
    > img {
      width: 20px;
    }
    > div {
      position: absolute;
      display: flex;
      flex-direction: column;
      right: 10px;
      top: 1.3rem;
      padding: 4px 0.42105rem;
      background-color: rgba(0, 0, 0, 0.5);
      border-radius: 5px;
      &::after {
        content: "";
        position: absolute;
        top: -5px;
        right: 0.3rem;
        width: 0;
        height: 0;
        border-width: 0 5px 5px;
        border-style: solid;
        border-color: transparent transparent rgba(0, 0, 0, 0.5);
      }
      &.flex {
        li {
          span {
            flex: 1;
            width: auto;
            // min-width: 2.8rem;
          }
        }
      }
      li {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        // margin-top: 0.3rem;
        font-size: 0;
        // height: .5333rem;
        padding: 0.1333rem 0 0.2667rem;
        > img {
          width: 22px;
          margin-right: 6px;
        }
        i {
          // position: absolute;
          // left: 0;
          // top: 0;
          // font-size: 0.45rem;
          font-size: 0.3733rem;
          margin-right: 0.4rem;
        }
        b {
          position: absolute;
          left: 0.32rem;
          top: 0.33rem;
          width: 7px;
          height: 7px;
          border-radius: 50%;
          background-color: #00c2c4;
          i {
            color: #fff;
            font-size: 12px;
            transform: scale(0.3, 0.3);
            top: -4px;
            left: -3px;
            position: absolute;
          }
        }
        span {
          width: 1.89474rem;
          white-space: nowrap;
          text-align: left;
          // padding-bottom: 0.3rem;
          // margin-left: 0.75rem;
          font-size: 0.3733rem;
          // text-indent: 0.2rem;
          // padding-right: 0.2rem;
        }
      }

      .home {
        border-top: solid 1px rgba(255, 255, 255, 0.4);
        a {
          width: 1.89474rem;
          margin-top: 0.3rem;
          margin-bottom: 0.1rem;
          margin-left: auto;
          margin-right: auto;
          display: block;
          text-decoration: none;
          overflow: hidden;

          img {
            width: 100%;
            outline: none;
            border: none;
          }
        }
      }
    }
  }
  .title {
    display: flex;
    flex: 1;
    width: 100%;
    height: 100%;
    font-size: 0.42105rem;
    letter-spacing: 1px;
    align-items: center;
    justify-content: center;
    > div {
      display: flex;
      transition: background 0.3s ease, min-width 0.3s ease, border-radius 0.3s ease;
      align-items: center;
      justify-content: center;
      padding-right: 12px;
      padding-left: 12px;
      white-space: nowrap;
      position: absolute;
      height: 100%;
      min-width: 100%;
      overflow: visible;
      pointer-events: none;
      background: linear-gradient(90deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.15) 50%, rgba(0, 0, 0, 0) 100%);
    }
    span {
      display: inline-block;
      position: relative;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      max-width: 7rem;
    }
    i {
      transition: all 0.3s;
      position: absolute;
      left: 50%;
      bottom: 0;
      font-size: 0.3rem;
      transform: translateX(-50%);
      margin-bottom: 0.07rem;
    }

    &.up {
      i {
        transform: translateX(-50%) rotate(180deg);
      }
      > div {
        min-width: 0;
        //position: static;
        flex-shrink: 0;
        background-color: rgba(0, 0, 0, 0.5);
        border-radius: 1.15789rem;
      }
      &.drak {
        > div {
          background-color: rgba(0, 0, 0, 0.8);
        }
      }
    }

    &.empty {
      i {
        display: none;
      }
    }
  }
  .content {
    position: absolute;
    top: 1.6rem;
    left: 0.92105rem;
    right: 0.92105rem;
    padding: 10px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 5px;
    font-size: 0.36842rem;
    text-align: center;
    &.drak {
      background: rgba(0, 0, 0, 0.8);
    }
    > div {
      display: inline-block;
      text-align: left;
      letter-spacing: 1px;
      word-break: break-all;
      white-space: normal;
      line-height: 1.5;
      // h4 {
      //     margin: 0;
      //     padding: 0;
      //     margin-bottom: 0.28rem;
      //     font-size: 0.43rem;
      //     padding-left: 0.18789rem;
      //     width: 100%;
      //     position: relative;
      //     &::before {
      //         content: "";
      //         position: absolute;
      //         left: 0;
      //         top: 50%;
      //         height: 80%;
      //         transform: translateY(-50%);
      //         width: 2px;
      //         background-color: var(--editor-main-color);
      //     }
      // }

      :deep(p) {
        word-break: break-word;
      }

      :deep(a) {
        color: var(--editor-main-color);
      }
    }
    // &::after {
    //     content: "";
    //     position: absolute;
    //     top: -6px;
    //     left: 50%;
    //     margin-left: -3px;
    //     width: 0;
    //     height: 0;
    //     border-width: 0 7px 6px;
    //     border-style: solid;
    //     border-color: transparent transparent rgba(0, 0, 0, 0.5);
    // }
  }

  .url-share {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.1);
    > div {
      position: absolute;
      left: 0.6rem;
      right: 0.6rem;
      top: 50vh;
      transform: translateY(-50%);
      border-radius: 4px;
      background-color: rgba(0, 0, 0, 0.5);
      padding: 0.31579rem;
      font-size: 0.36842rem;
      .tips {
        display: flex;
        align-items: center;
        justify-content: space-between;
        h4 {
          margin: 0;
          font-size: 0.42105rem;
        }
        i {
          font-size: 0.6rem;
        }
      }
      .url {
        display: -webkit-box;
        color: #fff;
        text-align: center;
        padding: 0.26316rem;
        width: 100%;
        height: 1.8rem;
        margin: 0.7rem 0rem;
        border-radius: 0.07rem;
        line-height: 1.99;
        font-size: 0.36842rem;
        background-color: rgba(0, 0, 0, 0.35);
        overflow: hidden;
        text-overflow: ellipsis;
        word-break: break-all;
        word-wrap: break-word;
        -webkit-line-clamp: 2; //在第几行加省略号
        -webkit-box-orient: vertical;
      }
      .btns {
        display: flex;
        justify-content: space-between;
        button {
          font-size: 0.36842rem;
          width: 47%;
          height: 1.05263rem;
          border-radius: 1.05263rem;
          &.submit {
            background: #00c2c4;
          }
        }
      }
    }
  }
}

@media (orientation: landscape) {
  .header {
    top: 0.2rem;
    height: 0.7rem;
    &.app {
      top: 0.2rem;
    }
    .left {
      .back-pano {
        i {
          font-size: 0.5rem;
        }
      }
    }
    .right {
      > i {
        font-size: 0.5rem;
      }
      > div {
        right: 10px;
        top: 0.75rem;
        padding: 4px 0.2rem;
        &::after {
          right: 0.23rem;
        }
        li {
          margin-top: 0.15rem;
          i {
            font-size: 0.3rem;
          }
          b {
            left: 0.2rem;
            top: 0.15rem;
          }
          span {
            padding-bottom: 0.15rem;
            margin-left: 0.5rem;
            font-size: 0.25rem;
          }
        }

        .home {
          a {
            margin-top: 0.15rem;
            margin-bottom: 0rem;
            width: 1.4rem;
          }
        }
      }
    }
    .title {
      font-size: 0.3rem;
    }
    .content {
      top: 1rem;
      left: 2rem;
      right: 2rem;
      padding: 10px;

      > div {
        max-height: 3rem;
        font-size: 0.25rem;
        line-height: 1.5;
      }
    }
  }
}

// ipad 横屏
@media only screen and (min-device-width: 768px) and (orientation: landscape) {
  .header {
    height: 0.8rem;
    .title {
      i {
        margin-bottom: 0;
      }
    }
  }
}
.app-share {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: #fff;
  color: var(--editor-main-color);
  text-shadow: none;
  border-radius: 0.146667rem 0.146667rem 0px 0px;
  z-index: 1000;
  i {
    // font-size: 2rem;
    font-size: 1rem;
    margin: 0.666667rem 0 0.106667rem;
    display: inline-block;
  }
  ul {
    // display: flex;
    // justify-content: space-around;
    overflow: hidden;
    div {
      text-align: center;
      // margin-top: -0.3rem;
      font-size: 0.34rem;
    }
    li {
      width: 33.3%;
      float: left;
      // padding-bottom: 0.5rem;
      overflow: hidden;
      text-align: center;
      // i{
      //   font-size: 2rem;
      // }
    }
    &.flex {
      display: flex;
      li {
        float: none;
      }
    }
  }
  > div {
    height: 1.293333rem;
    font-size: 16px;
    text-align: center;
    // padding: 0.5rem;
    border-top: solid 1px #eeeeee;
    margin-top: 0.466667rem;
    line-height: 1.293333rem;
  }
}
</style>
<style lang="scss">
.animated.short {
  &.faster {
    animation-duration: 0.3s;
  }
  @keyframes fadeInUp {
    0% {
      opacity: 0;
      transform: translate3d(0, 1rem, 0);
    }
    to {
      opacity: 1;
      transform: translateZ(0);
    }
  }
  @keyframes fadeOutDown {
    0% {
      opacity: 1;
    }
    to {
      opacity: 0;
      transform: translate3d(0, 1rem, 0);
    }
  }
}
</style>
