// 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() {
  //删除前端储存数据
  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 storageSetInfos = (data) => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.storageSetInfo(JSON.stringify(data),function (result) {
          resolve(result);
        });
      }else{
        console.log("pyBridge is null未连接上")
      }
    });
  };

  //设置储存数据
  const readSetInfos = (data) => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.readSetInfo(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未连接上")
    }
  };

    //指定直播间
  const Specifystreaming = (data) => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.find_big_brother_in_rooms(data,function (result) {
          resolve(result);
        });
      }else{
        console.log("pyBridge is null未连接上")
      }
    });
  };
  //储存直播间id
  const setStorageStreamId = (data) => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.saveFrontString(JSON.stringify(data),function (result) {
          resolve(result);
        });
      }else{
        console.log("pyBridge is null未连接上")
      }
    });
  };
  //获取直播间id
  const getStorageStreamId = (data) => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.loadFrontStringRaw(JSON.stringify(data),function (result) {
          resolve(result);
        });
      }else{
        console.log("pyBridge is null未连接上")
      }
    });
  };

  const setClipboards = (data) => {
    return new Promise((resolve, reject) => {
      if (pyBridge.value) {
        pyBridge.value.setClipboard(JSON.stringify(data),function (result) {
          resolve(result);
        });
      }else{
        console.log("pyBridge is null未连接上")
      }
    });
  };

  //跳转到主播页面
  const givePyAnchorId = (id) => {
    console.log("id",id);
    if (pyBridge.value) {
      pyBridge.value.givePyAnchorId(id, function (result) {
      });
    }
  };
  //跳转到主播页面
  const openAnchorIdRooms = (id) => {
    console.log("id",id);
    if (pyBridge.value) {
      pyBridge.value.openAnchorIdRoom(id, function (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,
    Specifystreaming,
    setStorageStreamId,
    getStorageStreamId,
    openAnchorIdRooms,
    storageSetInfos,
    readSetInfos,
    setClipboards
  };
}