import { dialog, Menu, Tray, app, shell } from "electron";
import path from "path";
import pkg from "../../package.json";
import os from "os";
import { commonConst } from "./common/utils";

function createDock(window) {
  return new Promise((resolve, reject) => {
    let icon;
    if (commonConst.macOS()) {
      icon = "./rocket.png";
    } else if (commonConst.windows()) {
      icon = parseInt(os.release()) < 10 ? "./icon@2x.png" : "./icon.ico";
    } else {
      icon = "icon@2x.png";
    }
    const appIcon = path.join(__static, icon);
    const dockMenu = Menu.buildFromTemplate([
      {
        label: "注册",
        click() {
          shell.openExternal("https://passport.csdn.net/newlogin?code=mobile");
        }
      },  
      {
        label: "帮助文档",
        click: () => {
          process.nextTick(() => {
            shell.openExternal(
              "https://yre5673znb.feishu.cn/docs/doccnMnLv3iZ3epFvjXprnPum3b"
            );
          });
        }
      },
      {
        label: "意见反馈",
        click: () => {
          process.nextTick(() => {
            shell.openExternal("https://www.wenjuan.com/s/UZBZJvmzhg/");
          });
        }
      },
      { type: "separator" },
      {
        label: "显示窗口",
        accelerator: "Alt+R",
        click() {
          window.show();
        }
      },
      {
        label: "偏好设置",
        click() {
          window.show();
          window.webContents.send("tray-setting");
        }
      },
      {
        label: "关于",
        click() {
          dialog.showMessageBox({
            title: "Quicker",
            message: "即刻",
            detail: `Version: ${pkg.version}\nAuthor: libai`
          });
        }
      },
      { type: "separator" },
      {
        label: "重启",
        click() {
          app.relaunch();
          app.quit();
        }
      },
      {
        role: "quit",
        label: "退出"
      }
    ]);
    app.dock.setMenu(dockMenu);
    app.dock.setIcon(appIcon);

    resolve(app);
  });
}

export default createDock;
