<template>
  <view class="page">
		<navbar leftClick @leftClick="$utils.navigateBack" />

    <view class="content">
      <video class="video"
        id="video"
        :src="detail.vio" 
        autoplay
        play-btn-position="center"
        :controls="!timeIsUp"
        :show-fullscreen-btn="false"
        :show-center-play-btn="true"
        @timeupdate="onTimeupdate"
      ></video>
      <view class="info">
        <view class="author">{{ detail.author }}</view>
        <view class="title">{{ detail.headTitle }}</view>
        <view class="desc">{{ detail.textDetails }}</view>
      </view>
    </view>

    <uv-overlay :show="timeIsUp" @click="onPlay" zIndex="998">
      <popupUnlock ref="popupUnlock" src="../static/sharing/unlock-video.png"></popupUnlock>
      <popupQrCode ref="popupQrCode" :src="detail.wxCodeImage"></popupQrCode>
    </uv-overlay>
  </view>
</template>

<script>
  import popupUnlock from '../components/popupUnlock.vue'
  import popupQrCode from '../components/popupQrCode.vue'

	export default {
    components: {
      popupUnlock,
      popupQrCode,
    },
		data() {
			return {
        detail: {
          id: null,
          headTitle: null,
          indexImage: null,
          vio: null,
          timeNum: 10,
          num: 10,
          wxCodeImage: null,
          textDetails: null,
        },
        timeIsUp: false,
        isLocked: true,
        videoContext: null
      }
    },
    async onLoad(option) {
      const { id } = option
      
      await this.fetchDetails(id)

      this.videoContext = uni.createVideoContext('video');
    },
    onShareAppMessage(res) {
      const {
        headTitle,
        indexImage,
      } = this.detail

      let o = {
        title : headTitle,
        imageUrl: indexImage,
        query: `id=${this.id}`,
      }

      // todo: get times and check is unlocked
      this.refreshLockStatus()

      return o
    },
    methods: {
      async fetchDetails(id) {
        try {
          this.detail = await this.$fetch('getVideoShareInfo', { id })
        } catch (err) {
          
        }
      },
      async fetchCheckShare(id) {
        try {
          return await this.$fetch('checkVideoShare', { id })
        } catch (err) {
          return {}
        }
      },
      async refreshLockStatus() {
        const result = await this.fetchCheckShare()
        const { title, open } = result

        if (open) {
          this.isLocked = false
          this.$refs.popupUnlock.close();
          this.$refs.popupQrCode.open()
          return
        }

        title && uni.showToast({
          title,
          icon: 'none'
        })
      },
      async onPlay() {
        if (!this.isLocked) {
          this.$refs.popupQrCode.open()
          return
        }
        
        const result = await this.fetchCheckShare()
        const { open } = result

        if (open) { // 转发已达标
          this.isLocked = false
          this.$refs.popupQrCode.open()
        } else {
          this.$refs.popupUnlock.open();
        }
      },
      async onTimeupdate(e) {
        const { currentTime } = e.target

        // todo: check
        if (currentTime >= this.detail.timeNum) {
          this.videoContext.pause()
          this.timeIsUp = true
          this.onPlay()
        }
      },
    },
  }
</script>

<style scoped lang="scss">
.video {
  width: 100%;
  height: calc(100vh - #{$navbar-height} - var(--status-bar-height) - 20rpx);
}

.info {
  color: #FFFFFF;
  font-size: 28rpx;

  position: fixed;
  left: 40rpx;
  bottom: 100rpx;

  .title {
    font-size: 32rpx;
    margin: 5rpx 0;
  }
}

</style>