import consola from 'consola';
import { computed, ref, unref } from 'vue';
// import TRTC from 'trtc-js-sdk';
import type { Client } from 'trtc-js-sdk';

import { useRtcStore } from '/@/store/modules/rtc';
// import Dialog from '/@/components/basic/dialog';
import {
  createAgoraClient,
  AgoraClient,
  unMutedAudio as argoraUnMutedAudio,
  mutedAudio as argoraMutedAudio,
  unMuteVideo as argoraUnMuteVideo,
  muteVideo as argoraMuteVideo,
  switchToFrontCam as argoraSwitchToFrontCam,
  switchToBackCam as argoraSwitchToBackCam,
  cleanAll as argoraCleanAll,
  isHasMicrophone as argoraIsHasMicrophone,
  isHasCamera as argoraIsHasCamera,
} from './useAgora';
import {
  createRTCSocket as createTXRTCSocket,
  unMutedAudio as txUnMutedAudio,
  mutedAudio as txMutedAudio,
  unMuteVideo as txUnMuteVideo,
  muteVideo as txMuteVideo,
  switchToFrontCam as txSwitchToFrontCam,
  switchToBackCam as txSwitchToBackCam,
} from './useTencent';
import { useMiniApp } from './useMiniApp';
import { useRoom } from './useRoom';

let localClient: Client | AgoraClient;

const sdkType = ref(0);

// const muteAudioLeader = ref(false);
export const muteVideoLeader = ref(true);
export const useCameraFront = ref(true);

const { isUsingMiniApp } = useMiniApp();

const globalVideoEnable = computed(
  () => Number(import.meta.env.VITE_ENABLE_VIDEO) === 1 && !unref(isUsingMiniApp),
);
//rtc- 腾讯云与声网结合
// console.log('getCameras', await AgoraRTC.getCameras());

// const isInitRTC = computed(() => useRtcStore().isInitRTC);

async function initRtcSDK(): Promise<void> {
  createRTCSocket();
}

async function createRTCSocket(): Promise<void> {
  try {
    if (!unref(isUsingMiniApp)) {
      const { getSign } = useRoom();
      const rtcStore = useRtcStore();
      let uid = rtcStore.userId;
      if (rtcStore.role === 'leader') {
        uid = `${rtcStore.roomId}-${rtcStore.userId}`;
      }
      const res = await getSign(uid, rtcStore.roomId, rtcStore.role, 'fire');
      // console.warn('web rtc入口', res);
      const { sign, operatorType, sdkAppId } = res;
      sdkType.value = Number(operatorType);
      if (Number(operatorType) === 1) {
        localClient = await createAgoraClient(String(sdkAppId), rtcStore.roomId, sign, uid);
      } else if (operatorType === 0) {
        localClient = await createTXRTCSocket(
          String(sdkAppId),
          rtcStore.roomId,
          sign,
          rtcStore.userId,
        );
        //腾讯云
      }
    } else {
      console.log('小程序关闭rtc入口');
    }
  } catch (error) {
    consola.error({
      tag: 'createRTCSocket',
      message: error,
    });
  }
}
const handleLeave = () => {
  //通用离开方法
  localClient && localClient.leave();
  console.error('rtc-leave');
};
const mutedAudio = () => {
  //通用禁音方法
  if (sdkType.value === 1) {
    argoraMutedAudio();
  }
  if (sdkType.value === 0) {
    txMutedAudio();
  }
  //通用离开方法
};
const unMutedAudio = () => {
  //通用开音方法
  if (sdkType.value === 1) {
    argoraUnMutedAudio();
  }
  if (sdkType.value === 0) {
    txUnMutedAudio();
  }
};
const muteVideo = () => {
  //通用开音方法
  if (sdkType.value === 1) {
    argoraMuteVideo();
  }
  if (sdkType.value === 0) {
    txMuteVideo();
  }
  muteVideoLeader.value = true;
};
const unMuteVideo = () => {
  //通用开音方法
  if (sdkType.value === 1) {
    argoraUnMuteVideo();
  }
  if (sdkType.value === 0) {
    txUnMuteVideo();
  }
  muteVideoLeader.value = false;
};

const switchToFrontCam = () => {
  //通用前置摄像方法
  if (sdkType.value === 1) {
    argoraSwitchToFrontCam();
  }
  if (sdkType.value === 0) {
    txSwitchToFrontCam();
  }
  useCameraFront.value = true;
};
const switchToBackCam = () => {
  //通用后置摄像方法
  if (sdkType.value === 1) {
    argoraSwitchToBackCam();
  }
  if (sdkType.value === 0) {
    txSwitchToBackCam();
  }
  useCameraFront.value = false;
};

const cleanAll = () => {
  if (sdkType.value === 1) {
    argoraCleanAll();
  }
};
const isHasMicrophone = computed(() => {
  if (sdkType.value === 1) {
    return argoraIsHasMicrophone.value;
  } else {
    return argoraIsHasMicrophone.value;
  }
});
const isHasCamera = computed(() => {
  if (sdkType.value === 1) {
    return argoraIsHasCamera.value;
  } else {
    return argoraIsHasCamera.value;
  }
});
export function useRtcSdk() {
  return {
    initRtcSDK,
    handleLeave,
    mutedAudio,
    unMutedAudio,
    muteVideo,
    unMuteVideo,
    muteVideoLeader,
    switchToFrontCam,
    switchToBackCam,
    useCameraFront,
    client: localClient,
    globalVideoEnable,
    cleanAll,
    isHasMicrophone,
    isHasCamera,
  };
}
