<template>
  <div class="home">
    <div class="home-header">
      <span>距离下次刷新监控人员还剩{{ timeNumber }}秒</span>
      <el-button type="primary" @click="refreshUserList" style="margin-left: 10px;">刷新</el-button>
    </div>
    <div class="home-content" v-if="userList.length">
      <div v-for="item in userList" class="home-content-item">
        <div class="home-content-item-card" :id="'player-wrapper-' + item.uid">
          <div class="home-content-item-card-name">{{ item.userName }}</div>
        </div>
      </div>
    </div>
    <div class="home-content" v-else style="justify-content: center;align-items: center;align-content: center;">
      <el-empty description="暂无数据" />
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref, onUnmounted } from "vue";
import { agora } from "@/utils/package/agora";
import { GetAgoraConf, GetPolling, GetRoomRtcToken } from "@/api";
import { storage } from '@/utils'
const userList = ref<any>([])
const channel = ref('')
const timer = ref<NodeJS.Timeout>()
const timeNumber = ref(30)
onMounted(() => {
  init()
  startCountdown()
});

onUnmounted(() => {
  clearInterval(timer.value)
});

const startCountdown = () => {
  clearInterval(timer.value);
  timeNumber.value = 30;
  timer.value = setInterval(() => {
    timeNumber.value--;
    if (timeNumber.value === 0) {
      refreshUserList();
      timeNumber.value = 30;
    }
  }, 1000);
};
const init = async () => {
  const paramsString = location.search.startsWith('?') ? location.search.slice(1) : location.search;
  const searchParams = new URLSearchParams(paramsString)
  channel.value = searchParams.get('channel') as string;
  storage.setItem('token', searchParams.get('token'))
  agora.setOptions('uid', Number(searchParams.get('uid')))
  agora.setOptions('channel', channel.value)
  await getAgoraConf()
  await getPolling()
  await getRoomRtcToken()
  agoraOn()
};

const getAgoraConf = async () => {
  await GetAgoraConf().then(res => {
    if (res.code === 200) {
      agora.init();
      agora.setOptions('appId', res.data)
    }
  })
};

const getPolling = async () => {
  await GetPolling(channel.value, '9').then((res: any) => {
    if (res.code === 200) {
      userList.value = res.data.map((item: any) => {
        return {
          ...item,
          newScreenShareId: Number(1 + item.screenShareId)
        }
      })
    }
  })
};
const refreshUserList = async () => {
  await agora.leave()
  await getPolling()
  await getRoomRtcToken()
};
const getRoomRtcToken = async () => {
  GetRoomRtcToken(channel.value).then(res => {
    if (res.code === 200) {
      agora.setOptions('token', res.data)
      agora.join()
    }
  })
};
const agoraOn = async () => {
  agora.on({
    userPublished: (user: any, mediaType: 'video' | 'audio') => {
      if (mediaType === 'video') {
        user.videoTrack.play(document.getElementById(`player-wrapper-${user.uid}`));
      }
    },
  })
}
</script>
<style lang="scss" scoped>
.home {
  background-color: rgb(7, 9, 11);
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 4px 120px;
  box-sizing: border-box;

  &-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    padding: 0 20px;
  }

  &-content {
    flex-grow: 1;
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;

    &-item {
      height: calc(100% / 3);
      width: calc(100% / 3);
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;

      &-card {
        width: 516px;
        height: 290px;
        box-sizing: border-box;
        position: relative;
        background-color: #101317;
        border: 1px solid #EBEBEB;

        &-name {
          background-color: rgb(253, 194, 41);
          color: black;
          font-size: 26px;
          position: absolute;
          left: 4px;
          bottom: 4px;
          padding: 0px 10px;
          z-index: 1;
        }
      }
    }
  }
}
</style>
