import { get } from './index';
import { ElNotification, ElButton } from 'element-plus';
import { h } from 'vue';

const env = import.meta.env;

// 获取电脑机器码
export const getdeviceuuidApi = async () => {
  const res = await get('/getdeviceuuid');
  return res;
};

// 启动应用
export const execApi = async (params: any) => {
  const res = await get(`/exec?path=${params.path}&param=${params.param}`);
  return res;
};

const openDownLoadFun = () => {
  const downloadUrl = `${env.VITE_API_FILE_URL}/data/downloadFile?fileId=${273}`;
  window.open(downloadUrl, '_blank');
};

const startFun = () => {
  // 启动插件
  const a = document.createElement('a');
  a.style.display = 'none';
  a.href = 'bydAssistant://';
  a.click();

  setTimeout(async () => {
    const res: any = await getdeviceuuidApi();
    if (res) {
      const uuid = res
        .replace('UUID', '')
        .replace(/\ +/g, '')
        .replace(/[\r\n]/g, '');

      localStorage.setItem('USER_UUID', uuid);
    }
    console.log('获取到的uuid');
  }, 5000);
};

// 启动插件
export const startupPlugin = () => {
  // ElMessageBox.confirm('请启用插件或者安装插件', '提示', {
  //   confirmButtonText: '下载插件',
  //   cancelButtonText: '启动插件',
  //   type: 'warning',
  //   distinguishCancelAndClose: true,
  // })
  //   .then(async () => {
  //     // 以动态创建a标签进行下载
  //     const downloadUrl = `${env.VITE_API_FILE_URL}/data/downloadFile?fileId=${273}`;
  //     window.open(downloadUrl, '_blank');

  //   })
  //   .catch(async (e) => {
  //     if (e === 'cancel') {
  //       // 启动插件
  //       const a = document.createElement('a');
  //       a.style.display = 'none';
  //       a.href = 'bydAssistant://';
  //       a.click();

  //       setTimeout(async () => {
  //         const res: any = await getdeviceuuidApi();
  //         if (res) {
  //           const uuid = res
  //             .replace('UUID', '')
  //             .replace(/\ +/g, '')
  //             .replace(/[\r\n]/g, '');

  //           localStorage.setItem('USER_UUID', uuid);
  //         }

  //         console.log('获取到的uuid');

  //       }, 5000);
  //     }
  //   });

  ElNotification({
    title: '没有检测到插件存在，无法获取用户信息，请打开插件或下载安装后再打开',
    type: 'warning',
    message: () => h('div', {
      class: 'el-notification-button-box',
    }, [
      h(ElButton, {
        'onClick': () => openDownLoadFun(),
        type: 'primary',
      }, '安装'),
      h(ElButton, {
        'onClick': () => startFun(),
      }, '启动'),
    ]),
  });
};
