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 socketBodypose: any = null; //ws实例对象

  function bodyposeWs(callback: any) {
    if (socketBodypose == null) {
      socketBodypose = io(address + '/', { transports: ['websocket', 'polling'], query: { type: 'bodypose', Authorization: token ? myToken : '' } });
    }
    socketBodypose.on('my_response', (e: any) => {
      callback(e);
    });
    socketBodypose.on('fe_device_init_result', (e: any) => {
      e.type = "fe_device_init_result";
      if (e.device_info == undefined) {
        closeWS();
      }
      callback(e);
    });
    socketBodypose.on('bodyposecontroller_ack', (e: any) => {
      callback(e);
    });
    socketBodypose.on('bodyposecontroller_result', (e: any) => {
      e.type = "bodyposecontroller_result";
      callback(e);
    });
    socketBodypose.on('disconnect', (e: any) => {
      callback(e);
    });
  }

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

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

  /**
   * 查看姿态识别模块是否可用
   */
  function checkBodypose(data?: any, callback?: any) {
    sendMessage(
      'bodyposecontroller',
      {
        cmd: 'check_bodyposecontroller_available',
        ctrl_name: `bodyposecontroller_${data}`
      },
      () => { }
    );
  }

  /**
   * 开启姿态识别模块
   */
  function openBodypose(data?: any, callback?: any) {
    sendMessage(
      'bodyposecontroller',
      {
        cmd: 'open_bodyposecontroller',
        ctrl_name: `bodyposecontroller_${data}`
      },
      () => { }
    );
  }

  /**
   * 关闭姿态识别模块
   */
  function terminateBodypose(data?: any, callback?: any) {
    sendMessage(
      'bodyposecontroller',
      {
        cmd: 'terminate_bodyposecontroller',
        ctrl_name: `bodyposecontroller_${data}`
      },
      () => {
      }
    );
  }

  /**
   * 暂停姿态识别模块
   */
  function suspendBodypose(data?: any, callback?: any) {
    sendMessage(
      'bodyposecontroller',
      {
        cmd: 'suspend_bodyposecontroller',
        ctrl_name: `bodyposecontroller_${data}`
      },
      () => { }
    );
  }

  /**
   * 重启姿态识别模块
   */
  function resumeBodypose(data?: any, callback?: any) {
    sendMessage(
      'bodyposecontroller',
      {
        cmd: 'resume_bodyposecontroller',
        ctrl_name: `bodyposecontroller_${data}`
      },
      () => { }
    );
  }

  /**
   * 获取姿态识别模块状态
   */
  function getBodyposeState(data?: any, callback?: any) {
    sendMessage(
      'bodyposecontroller',
      {
        cmd: 'get_bodyposecontroller_state',
        ctrl_name: `bodyposecontroller_${data}`
      },
      () => { }
    );
  }

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

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

  return { bodyposeWs, startDevice, sendMessage, checkBodypose, openBodypose, terminateBodypose, suspendBodypose, resumeBodypose, getBodyposeState, closeWS };
}
