<template>
  <view>
    <u-popup key="1" :show="videoShow" :round="10" @close="closeVideo">
      <view class="" style="padding: 10px;">
        <view>
          <view class="container">
            <div
              ref="videoContent"
              @tap="renderScript.handleClick"
              id="url-player-test"
              :videoData="videoData"
              :opname="opname"
              :change:opname="renderScript.opnameChange"
              :change:videoData="renderScript.receiveMsg"
            ></div>
          </view>
        </view>
        
        <div class="fullScreenButton-container">
          <div
            :class="`prism-fullscreen-btn ${isFullScreen ? 'fullscreen' : ''}`"
            @tap="renderScript.changeVideoScreen"
          ></div>
        </div>
        
        <view class="btn" style="text-align: center;">
          <button type="primary" @click="closeVideo" size="mini">关闭视频</button>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script setup lang="ts">
import { ref, nextTick } from 'vue'
import type { IVideoData } from '@/types/video'

// Props 定义
interface Props {
  videoData: IVideoData
}

const props = defineProps<Props>()

// Emits 定义
const emit = defineEmits<{
  close: []
}>()

// 响应式数据
const videoShow = ref(true)
const isFullScreen = ref(false)
const opname = ref('')

// 方法: 关闭视频
const closeVideo = () => {
  opname.value = 'close'
  nextTick(() => {
    emit('close')
  })
}
</script>

<script module="renderScript" lang="renderjs">
// RenderJS 模块 - 保持 Vue2 写法
// jQuery 在 RenderJS 中通过全局变量访问
var $ = window.jQuery || window.$

export default {
  mounted() {
    console.log(this.options, '这是monted')
  },
  
  data() {
    return {
      player: null,
      curTime: null,
      curStatus: null
    }
  },
  
  watch: {
    curTime(val) {
      if (this.curTime !== null && this.curStatus !== null) {
        // 可以添加逻辑
      }
    }
  },
  
  methods: {
    handleClick(event, ownerInstance) {
      console.log('event at line 165:', event)
    },
    
    emitData(event, ownerInstance) {
      ownerInstance.callMethod('recordTime', {
        time: this.curTime,
        status: this.curStatus
      })
    },
    
    changeVideoScreen(event, ownerInstance) {
      var status = this.player.fullscreenService.getIsFullScreen()
      
      ownerInstance.callMethod('screenChange', {
        status: status,
        primary: status ? 'portrait' : 'landscape'
      })
      
      if (status) {
        setTimeout(() => {
          plus.screen.lockOrientation('portrait-primary')
          this.player.fullscreenService.cancelFullScreen()
        }, 100)
      } else {
        this.player.fullscreenService.requestFullScreen()
        setTimeout(() => {
          plus.screen.lockOrientation('landscape-primary')
        }, 100)
      }
    },
    
    endEmitData(event, ownerInstance) {
      ownerInstance.callMethod('handleEnd')
    },
    
    getLive() {
      if (this.videoData.type == 1) {
        var fullScreenButtonComponent = Aliplayer.Component({
          init: function(status, toAddress) {
            this.fullScreenStatus = status
            this.$html = $('.fullScreenButton-container')
          },
          
          createEl: function(el) {
            this.$html.find('.ad').attr('src', this.adAddress)
            var $adWrapper = this.$html.find('.ad-wrapper')
            $adWrapper.attr('href', this.toAddress)
            $adWrapper.click(function() {})
            $(el).find('.prism-time-display').after(this.$html)
          },
          
          playing: function(player, e) {
            this.$html.show()
          }
        })
        
        console.log('this.currentVideoList at line 456111111111111111111111:', this.videoList)
        
        var player = new Aliplayer({
          id: 'url-player-test',
          vid: this.videoData.videoId,
          playauth: this.videoData.playAuth,
          encryptType: 1,
          playConfig: {
            EncryptType: 'AliyunVoDEncryption'
          },
          width: '100%',
          height: '200px',
          playsinline: true,
          controlBarVisibility: 'click',
          cover: '',
          components: [
            {
              name: 'adComponent',
              type: fullScreenButtonComponent,
              args: ['http://101.201.146.165:8088/Pf-EH/statics/uploadFile/2024-05-10/b0f420c7-9178-41ad-9dd6-f59a64a6e190.png']
            }
          ],
          skinLayout: [
            { name: 'bigPlayButton', align: 'blabs', x: 30, y: 80 },
            { name: 'H5Loading', align: 'cc' },
            { name: 'errorDisplay', align: 'tlabs', x: 0, y: 0 },
            { name: 'infoDisplay' },
            { name: 'tooltip', align: 'blabs', x: 0, y: 56 },
            { name: 'thumbnail' },
            {
              name: 'controlBar',
              align: 'blabs',
              x: 0,
              y: 0,
              children: [
                { name: 'progress', align: 'blabs', x: 0, y: 44 },
                { name: 'playButton', align: 'tl', x: 15, y: 12 },
                { name: 'timeDisplay', align: 'tl', x: 10, y: 7 },
                { name: 'prism-speed-selector', align: 'tr', x: 15, y: 12 },
                { name: 'volume', align: 'tr', x: 5, y: 10 }
              ]
            }
          ]
        }, function(player) {})
        
        this.player = player
      }
    },
    
    opnameChange(newValue, oldValue, ownerVm, vm) {
      console.log('opnameChange-----------', newValue)
      if (newValue == 'close') {
        if (this.player) {
          this.player.dispose()
        }
      }
    },
    
    receiveMsg(newValue, oldValue, ownerVm, vm) {
      console.log('数据变化newValue', newValue)
      if (newValue.playAuth) {
        this.loadWebPlayerSDK()
      }
    },
    
    checkValue() {
      console.log(this.videoData, this.videoData.playAuth, '1111888888')
      if (!this.videoData.playAuth) {
        setTimeout(() => {
          this.checkValue()
        }, 1000)
      } else {
        console.log('this.videoList at line 这是这只只是594:', this.currentVideoList)
        this.getLive()
      }
    },
    
    loadWebPlayerSDK() {
      return new Promise((resolve, reject) => {
        // 先加载 jQuery
        if (!window.jQuery && !window.$) {
          const jquery_tag = document.createElement('script')
          jquery_tag.type = 'text/javascript'
          jquery_tag.src = 'https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js'
          jquery_tag.charset = 'utf-8'
          jquery_tag.onload = () => {
            $ = window.jQuery || window.$
            this.loadAliPlayerSDK(resolve, reject)
          }
          jquery_tag.onerror = () => {
            console.error('jQuery 加载失败')
            reject(new Error('jQuery 加载失败'))
          }
          document.body.appendChild(jquery_tag)
        } else {
          $ = window.jQuery || window.$
          this.loadAliPlayerSDK(resolve, reject)
        }
      })
    },
    
    loadAliPlayerSDK(resolve, reject) {
      const s_tag = document.createElement('script')
      s_tag.type = 'text/javascript'
      s_tag.src = 'https://g.alicdn.com/apsara-media-box/imp-web-player/2.20.3/aliplayer-min.js'
      s_tag.charset = 'utf-8'
      s_tag.onload = () => {
        const s_tag1 = document.createElement('script')
        s_tag1.type = 'text/javascript'
        s_tag1.src = 'https://player.alicdn.com/aliplayer/presentation/js/aliplayercomponents.min.js'
        s_tag1.charset = 'utf-8'
        s_tag1.onload = () => {
          this.checkValue()
          resolve()
        }
        s_tag1.onerror = () => {
          console.error('阿里云播放器组件加载失败')
          reject(new Error('阿里云播放器组件加载失败'))
        }
        document.body.appendChild(s_tag1)
      }
      s_tag.onerror = () => {
        console.error('阿里云播放器 SDK 加载失败')
        reject(new Error('阿里云播放器 SDK 加载失败'))
      }
      document.body.appendChild(s_tag)
      
      const l_tag = document.createElement('link')
      l_tag.rel = 'stylesheet'
      l_tag.href = 'https://g.alicdn.com/apsara-media-box/imp-web-player/2.20.3/skins/default/aliplayer-min.css'
      document.body.appendChild(l_tag)
    }
  }
}
</script>

<style scoped>
.fullScreenButton-container {
  color: #fff;
  float: right;
  height: 35px;
  margin-top: 6px;
  margin-right: 5px;
  display: flex;
  align-items: center;
  position: relative;
}
</style>
