import { GetCheckoutRoomNum, PostAnonLogin, GetAgoraConf, GetRoomRtcToken } from '../../api/form/index'
import { getUUID } from '../../utils/utils'
import { Message } from 'tdesign-miniprogram';
import { onStop, startSignalr } from '../../utils/singlr';
import { agora } from '../../utils/agora'
Page({
  data: {
    meetingForm: {
      roomNum: '34351300',
      roomName: '杨杨',
    },
    isRecord: false,
    isCamera: false,
  },
  onShow() {
    onStop()
  },
  onLoad() {
    wx.authorize({
      scope: 'scope.record',
      success: () => {
        this.setData({
          isRecord: true,
        })
      },
      fail: () => {
        this.setData({
          isRecord: false,
        })
      }
    });
    wx.authorize({
      scope: 'scope.camera',
      success: () => {
        this.setData({
          isCamera: true,
        })
      },
      fail: () => {
        this.setData({
          isCamera: false,
        })
      }
    });
  },
  async getRoomRtcToken(roomNum: string, callBack: Function): Promise<void> {
    Promise.all([GetRoomRtcToken(roomNum), GetRoomRtcToken(roomNum + 'a'), GetAgoraConf()]).then(res => {
      if (res[0].code === 200 && res[1].code === 200 && res[2].code === 200) {
        callBack({
          token: res[0].data,
          tokenA: res[1].data,
          appid: res[2].data
        })
      }
    })
  },
  joinMeeting() {
    const navigateToMeeting = async (deviceId: string): Promise<void> => {
      await PostAnonLogin({
        deviceId,
        nickName: this.data.meetingForm.roomName,
        roomNum: this.data.meetingForm.roomNum,
      }).then(res => {
        if (res.code === 200) {
          wx.setStorage({
            key: "user",
            data: res.data
          })
          startSignalr(() => {
            this.getRoomRtcToken(this.data.meetingForm.roomNum, (option) => {
              agora.setOption({
                token: option.token,
                tokenA: option.tokenA,
                channelId: this.data.meetingForm.roomNum,
                appId: option.appid,
                uid: res.data.uid,
                screenShareId: res.data.screenShareId,
              })
              wx.navigateTo({
                url: `/pages/meeting/index?roomNum=${this.data.meetingForm.roomNum}`
              })
            })
          })
        }
      })
    }
    if (!this.data.meetingForm.roomNum) {
      Message.error({
        context: this,
        offset: [90, 32],
        duration: 3000,
        content: '请输入会议号！',
      });
      return
    }
    if (!this.data.meetingForm.roomName) {
      Message.error({
        context: this,
        offset: [90, 32],
        duration: 3000,
        content: '请输入入会名称！',
      });
      return
    }
    GetCheckoutRoomNum(this.data.meetingForm.roomNum).then(res => {
      if (res.code === 200) {
        if (res.data) {
          wx.getStorage({
            key: "deviceId",
            success(res: any) {
              navigateToMeeting(res.data)
            },
            fail() {
              const uuid = getUUID()
              wx.setStorage({
                key: "deviceId",
                data: uuid
              })
              navigateToMeeting(uuid)
            }
          })
        } else {
          Message.error({
            context: this,
            offset: [90, 32],
            duration: 3000,
            content: '房间号不存在！',
          });
        }
      }
    })
  },
  setting() {
    wx.openSetting({
      success: (res) => {
        this.setData({
          isCamera: res.authSetting['scope.camera'],
          isRecord: res.authSetting['scope.record'],
        })
      }
    })
  },
  changeMeetingForm(e) {
    switch (e.target.dataset.type) {
      case 'roomNum':
        this.setData({
          meetingForm: {
            roomNum: e.type === 'clear' ? '' : e.detail.value,
            roomName: this.data.meetingForm.roomName,
          }
        })
        break;
      case 'roomName':
        this.setData({
          meetingForm: {
            roomNum: this.data.meetingForm.roomNum,
            roomName: e.type === 'clear' ? '' : e.detail.value,
          }
        })
        break;
    }
  }
})