<template>
  <view class="call-view">
    <view class="nav-bar">
      <text class="duration">{{duration}}</text>
    </view>

    <!-- 音频通话 -->
    <view v-if="currentCall.mediaType===0" class="profile">
      <image class="user-avatar" mode="widthFix" :src="friend.data.avatar"></image>
      <text class="user-name">{{friend.data.name}}</text>
    </view>

    <!-- 视频通话 -->
    <view v-if="currentCall.mediaType === 1" class="content">
      <view class="container">
        <!-- 大视图 -->
        <!-- #ifdef APP -->
        <grtc-video v-if="!mainVideoUser.cameraMuted" :userId="mainVideoUser.id" class="main-video"></grtc-video>
        <!-- #endif -->
        <!-- #ifdef H5 -->
        <web-grtc-video v-if="!mainVideoUser.cameraMuted" :userId="mainVideoUser.id" class="main-video"></web-grtc-video>
        <!-- #endif -->
        <view v-if="mainVideoUser.cameraMuted" class="main-video">
          <image class="main-avatar" mode="widthFix" :src="mainVideoUser.data.avatar"></image>
        </view>

        <!-- 小视图 -->
        <!-- #ifdef APP -->
        <grtc-video v-if="!miniVideoUser.cameraMuted" @click="toggleUser()" :userId="miniVideoUser.id" class="mini-video"></grtc-video>
        <!-- #endif -->
        <!-- #ifdef H5 -->
        <web-grtc-video v-if="!miniVideoUser.cameraMuted" @click="toggleUser()" :userId="miniVideoUser.id" class="mini-video"></web-grtc-video>
        <!-- #endif -->
        <view v-if="miniVideoUser.cameraMuted" class="mini-video">
          <image class="mini-avatar" mode="widthFix" :src="miniVideoUser.data.avatar"></image>
        </view>
      </view>
    </view>

    <view class="action-box">
      <view class="action-top">
        <view class="action-item" @click="toggleMic()">
          <view v-if="self.micMuted">
            <view class="item-background-disable">
              <image class="action-bar-icon" src="/static/images/microphone-disable.png"></image>
            </view>
            <text class="action-text">麦克风已关</text>
          </view>
          <view v-else>
            <view class="item-background">
              <image class="action-bar-icon" src="/static/images/microphone.png"></image>
            </view>
            <text class="action-text">麦克风已开</text>
          </view>
        </view>
        <view class="action-item" @click="toggleSpeaker()">
          <view v-if="currentCall.speakerOn">
            <view class="item-background">
              <image class="action-bar-icon" src="/static/images/speaker.png"></image>
            </view>
            <text class="action-text">扬声器已开</text>
          </view>
          <view v-else>
            <view class="item-background-disable">
              <image class="action-bar-icon" src="/static/images/speaker-disable.png"></image>
            </view>
            <text class="action-text">扬声器已关</text>
          </view>
        </view>
        <view class="action-item" @click="toggleCamera()" v-if="currentCall.mediaType === 1">
          <view v-if="self.cameraMuted">
            <view class="item-background-disable">
              <image class="action-bar-icon" src="/static/images/camera-disable.png"></image>
            </view>
            <text class="action-text">摄像头已关</text>
          </view>
          <view v-else>
            <view class="item-background">
              <image class="action-bar-icon" src="/static/images/camera.png"></image>
            </view>
            <text class="action-text">摄像头已开</text>
          </view>
        </view>
      </view>
      <view class="action-item" @click="end()">
        <view class="end-background">
          <image class="action-icon" src="/static/images/phoneEnd.png"></image>
        </view>
      </view>
      <view class="switch-camera" @click="switchCamera()" v-if="currentCall.mediaType === 1">
        <image class="switch-camera-icon" src="/static/images/switch-camera.png"></image>
      </view>
    </view>
  </view>
</template>

<script>
  import webGrtcVideo from '../components/web-grtc-video/web-grtc-video.vue';
  const GRTC = uni.$GRTC;
  let interval = null;
  export default {
    data() {
      return {
        currentCall: GRTC.currentCall(),
        self: null,
        friend: null,
        mainVideoUser: null,
        miniVideoUser: null,
        duration: null
      }
    },
    components: {
      webGrtcVideo,
    },
    onLoad() {
      const isCaller = uni.$currentUser.id === this.currentCall.caller.id;
      this.self = this.getSelf();
      this.friend = this.getFriend();
      this.miniVideoUser = this.self;
      this.mainVideoUser = this.friend;

      this.duration = this.formattedDuration();
      interval = setInterval(() => {
        this.duration = this.formattedDuration();
      }, 1000);

      this.initListener();
    },
    onUnload() {
      GRTC.off(GRTC.EVENT.USER_QUIT, this.onUserQuit);
      GRTC.off(GRTC.EVENT.USER_MIC_CHANGED, this.onUserMicChanged);
      GRTC.off(GRTC.EVENT.USER_CAMERA_CHANGED, this.onUserCameraChanged);
    },
    onBackPress(event) {
      return event.from === 'backbutton';  // 禁止安卓侧滑返回
    },
    methods: {
      getSelf() {
        if (uni.$currentUser.id === this.currentCall.caller.id) {
          return this.currentCall.caller
        }
        return this.currentCall.callees[0]
      },
      getFriend() {
        if (uni.$currentUser.id === this.currentCall.caller.id) {
          return this.currentCall.callees[0]
        }
        return this.currentCall.caller
      },
      initListener() {
        GRTC.on(GRTC.EVENT.USER_QUIT, this.onUserQuit);
        GRTC.on(GRTC.EVENT.USER_MIC_CHANGED, this.onUserMicChanged);
        GRTC.on(GRTC.EVENT.USER_CAMERA_CHANGED, this.onUserCameraChanged);
      },
      onUserQuit(event) {
        clearInterval(interval);
        const username = event.user.id === this.self.id ? '' : event.user.data.name;
        let message = '';
        switch (event.reason) {
          case 'GOEASY_DISCONNECTED':
            message = `${username}网络异常`;
            break;
          case 'HUNG_UP':
            message = `${username}已挂断`;
            break;
        }
        uni.showToast({
        	title: message,
        	icon: "none"
        });
        uni.navigateBack();
      },
      onUserMicChanged(event) {
        console.log("onUserMicChange",event);
      },
      onUserCameraChanged(event) {
        console.log("onUserCameraChanged",event);
      },
      end() {
        GRTC.end();
      },
      switchCamera() {
        GRTC.switchCamera();
      },
      toggleSpeaker() {
        GRTC.toggleSpeaker(!this.currentCall.speakerOn);
      },
      toggleMic() {
        GRTC.muteMic(!this.self.micMuted);
      },
      toggleCamera() {
        GRTC.muteCamera(!this.self.cameraMuted);
      },
      toggleUser() {
        const temp = this.mainVideoUser;
        this.mainVideoUser = this.miniVideoUser;
        this.miniVideoUser = temp;
      },
      formattedDuration() {
        const seconds = this.currentCall.getDuration();
        const minutes = Math.floor(seconds / 60);
        const formattedSeconds = String(seconds % 60).padStart(2, '0');
        const formattedMinutes = String(minutes).padStart(2, '0');
        return `${formattedMinutes}:${formattedSeconds}`; // mm:ss
      }
    }
  }
</script>

<style scoped>
  @import url('/static/style/rtcCallStyle.css');
</style>