const { app, BrowserWindow, ipcMain, webContents, Menu } = require("electron");
const path = require("path");
const sqLite = require("sqlite");
const os = require("os");

global.PUBLIC_PATH = "./resources/app/public/";
global.PUBLIC_MODEL_PATH = "./resources/app/public/model";
global.PUBLIC_FRONT_PATH = "./resources/app/front";
global.PUBLIC_URL_PATH = "public/";
global.FONT_URL = "http://127.0.0.1:3001/#/";

global.DB_IS_SQLITE = true;
if (process.argv.includes("env=dev")) {
  global.PUBLIC_PATH = "public/";
  global.PUBLIC_FRONT_PATH = "front/";
  global.PUBLIC_MODEL_PATH = "public/model";
  global.FONT_URL = "http://127.0.0.1:3001/#/";
}

require("./serve/main");

const createWindow = () => {
  var a = {
    webPreferences: {
      preload: path.join(__dirname, "preload.js"),
    },
    // 配置 icon
    frame: false,
    resizable: false,
    icon: "public/20230220093713-迅捷PDF转换器.ico",
  };
  const win = new BrowserWindow(a);
  // 隐藏菜单栏
  Menu.setApplicationMenu(null);
  // 启动后加载URL
  win.maximize();
  win.loadURL(global.FONT_URL);
  win.webContents.setWindowOpenHandler((details) => { });
  // 启动后打开工具栏
  win.webContents.openDevTools();
  // win.maximize()
  //  设置菜单栏是否展示
  // win.setMenu(null)
  // 设置背景颜色
  // win.setBackgroundColor('red')
  // 设置全屏
  // win.setFullScreen(true)
  // 设置透明度
  // win.setOpacity(0.5)
  console.log(sqLite);
};
const isWin7 = os.release().startsWith("6.1");
if (isWin7) {
  //win7下 ，关闭硬件加速
  app.disableHardwareAcceleration();
}
app.whenReady().then(() => {
  createWindow();
  // 关闭所有窗口的菜单栏
  // electron.Menu.setApplicationMenu(null)
  ipcMain.handle("fetch", async (e, options) => {
    let data;
    try {
      data = await require(`./src/server${options.path}.js`)(options);
    } catch (error) {
      return {
        sussces: false,
        message: error,
      };
    }
    return {
      sussces: true,
      data,
    };
  });
});

/**
 * 监听所有窗口关闭事件
 */

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});

// 获取当前APP路径
console.log(app.getAppPath(), __dirname);
