import { BrowserWindow, shell } from "electron";
import { createWindow } from "./index";
import { mainWinUrl } from "../utils/config";
import { useStore } from "../store";
import { isLinux } from "../utils/config";

class MainWindow {
  private win: BrowserWindow | null = null;
  private winURL: string;
  constructor() {
    this.winURL = mainWinUrl;
  }
  /**
   * 保存窗口大小和状态
   */
  private saveBounds() {
    if (this.win?.isFullScreen()) return;
    const store = useStore();
    const bounds = this.win?.getBounds();
    if (bounds) {
      const maximized = this.win?.isMaximized();
      store.set("window", { ...bounds, maximized });
    }
  }
  /**
   * 主窗口事件
   * @returns void
   */
  private event(): void {
    if (!this.win) return;
    const store = useStore();
    // 配置网络代理
    if (store.get("proxy")) {
      this.win.webContents.session.setProxy({ proxyRules: store.get("proxy") });
    }

    // 窗口打开处理程序
    this.win.webContents.setWindowOpenHandler((details) => {
      const { url } = details;
      if (url.startsWith("https://") || url.startsWith("http://")) {
        shell.openExternal(url);
      }
      return { action: "deny" };
    });
    // 窗口显示时
    this.win?.on("show", () => {
      this.win?.webContents.send("lyricsScroll");
    });
    // 窗口获得焦点时
    this.win?.on("focus", () => {
      this.saveBounds();
    });
    // 窗口大小改变时
    this.win?.on("resized", () => {
      // 若处于全屏则不保存
      if (this.win?.isFullScreen()) return;
      this.saveBounds();
    });
    // 窗口位置改变时
    this.win?.on("moved", () => {
      this.saveBounds();
    });
    // 窗口最大化时
    this.win?.on("maximize", () => {
      this.saveBounds();
      this.win?.webContents.send("win-state-change", true);
    });
    // 窗口取消最大化时
    this.win?.on("unmaximize", () => {
      this.saveBounds();
      this.win?.webContents.send("win-state-change", false);
    });
    // Linux 无法使用 resized 和 moved
    if (isLinux) {
      this.win?.on("resize", () => {
        // 若处于全屏则不保存
        if (this.win?.isFullScreen()) return;
        this.saveBounds();
      });
      this.win?.on("move", () => {
        this.saveBounds();
      });
    }
    // 窗口关闭
    this.win?.on("close", (event) => {
      event.preventDefault();
      this.win?.hide();
    });
  }
  /**
   * 创建窗口
   * @returns BrowserWindow | null
   */
  create(): BrowserWindow | null {
    const store = useStore();
    const { width, height } = store.get("window");
    this.win = createWindow({
      // 菜单栏
      titleBarStyle: "customButtonsOnHover",
      width,
      height,
      minHeight: 600,
      minWidth: 800,
      show: false,
    });
    if (!this.win) return null;
    // 加载地址
    this.win.loadURL(this.winURL);
    // 窗口事件
    this.event();
    return this.win;
  }
  /**
   * 获取窗口
   * @returns BrowserWindow | null
   */
  getWin(): BrowserWindow | null {
    if (this.win && !this.win.isDestroyed()) {
      return this.win;
    }
    return null;
  }
  /**
   * 显示主窗口
   */
  showWindow() {
    if (this.win) {
      this.win.show();
      if (this.win.isMinimized()) this.win.restore();
      this.win.focus();
    }
  }
}
export default new MainWindow();
