import io from 'socket.io-client';
import { ref, onMounted, onUnmounted, onBeforeUnmount } from 'vue';

export function useWebSocket() {
  const address: any = import.meta.env.VITE_APP_BASE_API;
  const token: any = localStorage.getItem('token');
  const myToken: any = 'JWT ' + token;
  let socketFace: any = null; //ws实例对象

  function faceWs(callback: any) {
    if (socketFace == null) {
      socketFace = io(address + '/', { transports: ['websocket', 'polling'], query: { type: 'face', Authorization: token ? myToken : '' } });
    }
    socketFace.on('my_response', (e: any) => {
      callback(e);
    });
    socketFace.on('fe_device_init_result', (e: any) => {
      e.type = "fe_device_init_result";
      if (e.device_info == undefined) {
        closeWS();
      }
      callback(e);
    });
    socketFace.on('facecontroller_ack', (e: any) => {
      callback(e);
    });
    socketFace.on('facecontroller_result', (e: any) => {
      e.type = "facecontroller_result";
      callback(e);
    });
    socketFace.on('disconnect', (e: any) => {
      let obj = { type: "disconnect" };
      callback(obj);
    });
  }

  /**
   * 发送消息
   */
  function sendMessage(type: string, data: any, callback?: () => void) {
    if (socketFace == null) {
      return false;
    }
    if (socketFace.connected) {
      callback = callback || function () { };
      socketFace.emit(type, data, callback);
    }
  }

  /**
   * 开始连接设备信息
   */
  function startDevice(data?: any, callback?: any) {
    sendMessage(
      'fe_device_init',
      {
        data: data
      },
      () => { }
    );
  }

  /**
   * 查看人脸识别模块是否可用
   */
  function checkFace(data?: any, callback?: any) {
    sendMessage(
      'facecontroller',
      {
        cmd: 'check_facecontroller_available',
        ctrl_name: `facecontroller_${data}`
      },
      () => { }
    );
  }

  /**
   * 开启人脸识别模块
   */
  function openFace(data?: any, callback?: any) {
    sendMessage(
      'facecontroller',
      {
        cmd: 'open_facecontroller',
        ctrl_name: `facecontroller_${data}`
      },
      () => { }
    );
  }

  /**
   * 关闭人脸识别模块
   */
  function terminateFace(data?: any, callback?: any) {
    sendMessage(
      'facecontroller',
      {
        cmd: 'terminate_facecontroller',
        ctrl_name: `facecontroller_${data}`
      },
      () => {
      }
    );
  }

  /**
   * 暂停人脸识别模块
   */
  function suspendFace(data?: any, callback?: any) {
    sendMessage(
      'facecontroller',
      {
        cmd: 'suspend_facecontroller',
        ctrl_name: `facecontroller_${data}`
      },
      () => { }
    );
  }

  /**
   * 重启人脸识别模块
   */
  function resumeFace(data?: any, callback?: any) {
    sendMessage(
      'facecontroller',
      {
        cmd: 'resume_facecontroller',
        ctrl_name: `facecontroller_${data}`
      },
      () => { }
    );
  }

  /**
   * 获取人脸识别模块状态
   */
  function getFaceState(data?: any, callback?: any) {
    sendMessage(
      'facecontroller',
      {
        cmd: 'get_facecontroller_state',
        ctrl_name: `facecontroller_${data}`
      },
      () => { }
    );
  }

  /**
   * 关闭WS
   */
  function closeWS() {
    if (socketFace) {
      socketFace.close();
      socketFace = null;
    }
  }

  onBeforeUnmount(() => {
    closeWS();
  });

  return { faceWs, startDevice, sendMessage, checkFace, openFace, terminateFace, suspendFace, resumeFace, getFaceState, closeWS };
}
