// pythonBridge.js
import { ref, onMounted } from 'vue';

export function usePythonBridge() {
  const bridge = ref(null);

  // 初始化 QWebChannel
  const initBridge = () => {
    new QWebChannel(qt.webChannelTransport, (channel) => {
      bridge.value = channel.objects.bridge;
    });
  };

  // 调用 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);
        });
      }
    });
  };

  // 打开tk后台
  const loginTikTok = () => {
    if (bridge.value) {
      bridge.value.loginTikTok(function (result) {

      });
    }

  };

  // 登录tk后台
  const loginBackStage = (data) => {
    return new Promise((resolve, reject) => {
      if (bridge.value) {
        bridge.value.loginBackStage(data, function (result) {
          if (result) {
            resolve(result);
          } else {
            reject(result);
          }

        });
      }
    });
  };

  //跳转到主播页面
  const givePyAnchorId = (id) => {

    if (bridge.value) {
      bridge.value.givePyAnchorId(id, function (result) {

      });
    }
  };
  // 在组件挂载时初始化桥接
  onMounted(initBridge);

  return {
    fetchDataConfig,
    fetchDataCount,
    loginBackStage,
    loginTikTok,
    givePyAnchorId
  };
}