// pythonBridge.js
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
const pyBridge  = ref(null);
// 初始化 QWebChannel
const initBridge = () => {
  if (/localhost/.test(window.location.href)) return
  new QWebChannel(qt.webChannelTransport, (channel) => {
    pyBridge.value = channel.objects.bridge;
  });
};
export function usePythonBridge() {




  // // 调用 Python 方法
  // const fetchDataConfig = (data) => {
  //   return new Promise((resolve, reject) => {
  //     if (bridge.value) {
  //       bridge.value.fetchDataConfig(data, function (result) {
  //         resolve(result);
  //       });
  //     }


  //   });
  // };

  // // 查询获取主播的数据
  // const fetchDataCount = () => {
  //   return new Promise((resolve, reject) => {
  //     if (bridge.value) {
  //       bridge.value.fetchDataCount(function (result) {
  //         resolve(result);
  //       });
  //     }
  //   });
  // };

  //删除前端储存数据
  const deleteStorageData = (data) => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.deleteAccountInfo(JSON.stringify(data),function (result) {
          resolve(result);
        });
      }else{
        console.log("pyBridge is null未连接上")
      }
    });
  };

  //获取前端储存数据
  const getStorageData = (data) => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.readAccountInfo(JSON.stringify(data),function (result) {
          resolve(result);
        });
      }else{
        console.log("pyBridge is null未连接上")
      }
    });
  };
  //设置前端储存数据
  const setStorageData = (data) => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.storageAccountInfo(JSON.stringify(data),function (result) {
          resolve(result);
        });
      }else{
        console.log("pyBridge is null未连接上")
      }
    });
  };
  
  // 查询获取大哥的数据
  const controlTask = (data) => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.control_task(data,function (result) {
          resolve(result);
        });
      }else{
        console.log("pyBridge is null未连接上")
      }
    });
  };

  //总数有效数
  const getBrotherInfo = () => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.getBrotherInfo(function (result) {
          resolve(JSON.parse(result));
        });
      }else{
        console.log("pyBridge is null未连接上")
      }
    });
  };

  // 打开tk后台
  const loginTikTok = () => {
    if (pyBridge.value) {
      pyBridge.value.loginTikTok(function (result) {
      });
    }else{
      console.log("pyBridge is null未连接上")
    }
  };

  // // 登录tk后台
  // const loginBackStage = (data) => {
  //   if (bridge.value) {
  //     if (data.index == 0) {
  //       bridge.value.loginBackStage(JSON.stringify(data));
  //     } else if (data.index == 1) {
  //       bridge.value.loginBackStageCopy(JSON.stringify(data));
  //     }

  //   }
  // };

  //跳转到主播页面
  const givePyAnchorId = (id) => {
    console.log("id",id);
    if (pyBridge.value) {
      pyBridge.value.givePyAnchorId(id, function (result) {
      });
    }
  };

  // //查询登录状态
  // const backStageloginStatus = () => {
  //   return new Promise((resolve, reject) => {
  //     if (bridge.value) {
  //       bridge.value.backStageloginStatus(function (result) {
  //         resolve(result);
  //       });
  //     }
  //   });

  // };
  // //查询登录状态
  // const backStageloginStatusCopy = () => {
  //   return new Promise((resolve, reject) => {
  //     if (bridge.value) {
  //       bridge.value.backStageloginStatusCopy(function (result) {
  //         resolve(result);
  //       });
  //     }
  //   });

  // };

  //导出表格
  const exportToExcel = (data) => {
    if (pyBridge .value) {
      pyBridge .value.exportToExcel(JSON.stringify(data));
    }

  };
  const stopScript = () => {
    if (pyBridge .value) {
      pyBridge .value.stopScript();
    }

  };

  //获取版本号
  const getVersion = () => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.currentVersion(function (result) {
          resolve(result);
        });
      }
    });
  };
  // 在组件挂载时初始化桥接
  onMounted(initBridge);

  return {
    loginTikTok,
    exportToExcel,
    stopScript,
    controlTask,
    getBrotherInfo,
    getVersion,
    givePyAnchorId,
    getStorageData,
    setStorageData,
    deleteStorageData,
  };
}