import io from 'socket.io-client';
import { ref, onMounted, onUnmounted, onBeforeUnmount } from 'vue';
import useAppStore from '@/store/modules/app';
// import router from '@/router';

// let socketHand: any = null; //ws实例对象
// socketHand = io(address + '/', {
//   transports: ['websocket', 'polling'],
//   query: {
//     type: 'hand',
//     Authorization: token ? myToken : ''
//   }
// });

export function useWebSocket() {
  const address: any = import.meta.env.VITE_APP_BASE_API;
  const token: any = localStorage.getItem('token');
  const deviceid: any = localStorage.getItem('deviceid');
  const myToken: any = 'JWT ' + token;

  // let socketHand: any = useAppStore().getSocketHand() || null; //ws实例对象
  // if (socketHand == null) {
  //   socketHand = io(address + '/', {
  //     transports: ['websocket', 'polling'],
  //     query: {
  //       type: 'hand',
  //       Authorization: token ? myToken : '',
  //     }
  //   });
  //   useAppStore().setSocketHand(socketHand);
  // }

  let socketHand: any = null; //ws实例对象
  socketHand = io(address + '/', {
    transports: ['websocket', 'polling'],
    query: {
      type: 'hand',
      Authorization: token ? myToken : ''
    }
  });

  function handWs(callback: any) {
    if (socketHand == null) {
      return false;
    }
    callback({
      wksid: socketHand.id
    });
    socketHand.on('connect', (e: any) => {
      callback(e);
    });
    socketHand.on('my_response', (e: any) => {
      callback(e);
    });
    socketHand.on('device_login_result', (e: any) => {
      callback(e);
    });
    socketHand.on('fe_device_init_result', (e: any) => {
      callback(e);
    });
    socketHand.on('handcontroller_ack', (e: any) => {
      callback(e);
    });
    socketHand.on('handcontroller_result', (e: any) => {
      callback(e);
    });
    socketHand.on('disconnect', (e: any) => {
      callback(e);
    });
  }

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

  /**
   * 开始连接登录
   */
  function startLogin(data?: any, callback?: any) {
    sendMessage(
      'fe_get_qrlogin',
      {
        deviceid: deviceid ? deviceid : ''
      },
      () => {}
    );
  }

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

  /**
   * 开始连接手势
   */
  function startHand(data?: any, callback?: any) {
    sendMessage(
      'handcontroller',
      {
        hctrl_name: `handcontroller_${data}`,
        cmd: 'open_handcontroller'
      },
      () => {}
    );
  }

  /**
   * 获取手势状态
   */
  function stateHand(data?: any, callback?: any) {
    sendMessage(
      'handcontroller',
      {
        hctrl_name: `handcontroller_${data}`,
        cmd: 'get_handcontroller_state'
      },
      () => {}
    );
  }

  /**
   * 关闭连接手势
   */
  function closeHand(data?: any, callback?: any) {
    sendMessage(
      'handcontroller',
      {
        hctrl_name: `handcontroller_${data}`,
        cmd: 'terminate_handcontroller'
      },
      () => {}
    );
  }

  onBeforeUnmount(() => {
    // if (socketHand) {
    //   // if(['/gesture'].includes(router.currentRoute.value.path)){
    //   //   socketHand.close();
    //   //   socketHand = null;
    //   // }
    //   socketHand = null;
    // }
    // getClearTimer();
    if (socketHand) {
      socketHand.close();
      socketHand = null;
    }
  });

  return { handWs, sendMessage, startLogin, startDevice, startHand, stateHand, closeHand };
}
