import { Menu, Tray, nativeImage } from "electron";

import trayIconAsset from "../../assets/desktop/icon.png?asset";
import macOsTrayIconAsset from "../../assets/desktop/iconTemplate.png?asset";
import { version } from "../../package.json";

import { mainWindow, quitApp } from "./window";
import { checkForUpdates } from "./updater";
import { getThemeFiles, getActiveTheme, applyTheme, cycleTheme, reloadTheme, openThemesFolder } from "./themes";
import { cycleTheme, getActiveThemeName, getThemeCount, openThemesFolder, reloadThemes } from "./themes";

// internal tray state
let tray: Tray = null;

// Create and resize tray icon for macOS
function createTrayIcon() {
  if (process.platform === "darwin") {
    const image = nativeImage.createFromDataURL(macOsTrayIconAsset);
    const resized = image.resize({ width: 20, height: 20 });
    resized.setTemplateImage(true);
    return resized;
  } else {
    return nativeImage.createFromDataURL(trayIconAsset);
  }
}

export function initTray() {
  const trayIcon = createTrayIcon();
  tray = new Tray(trayIcon);
  updateTrayMenu();
  tray.setToolTip(`Sanctum ${version}`);
  tray.setImage(trayIcon);
  tray.on("click", () => {
    if (mainWindow.isVisible()) {
     mainWindow.hide();
    } else {
     mainWindow.show();
     mainWindow.focus();
    }
  });
}

export function updateTrayMenu() {
  tray.setContextMenu(
    Menu.buildFromTemplate([
      { label: "Sanctum", type: "normal", enabled: false },
      {
        label: "Version",
        type: "submenu",
        submenu: Menu.buildFromTemplate([
          {
            label: version,
            type: "normal",
            enabled: false,
          },
        ]),
      },
      {
        label: "Check for Updates",
        type: "normal",
        click: () => checkForUpdates(),
      },
      {
        label: "Themes",
        type: "submenu",
        submenu: Menu.buildFromTemplate([
          {
            label: `Active: ${getActiveThemeName()}`,
            type: "normal",
            enabled: false,
          },
          { type: "separator" },
          {
            label: "Next Theme →",
            type: "normal",
            enabled: getThemeCount() > 0,
            click: () => cycleTheme(),
          },
          {
            label: "Reload Themes",
            type: "normal",
            click: () => reloadThemes(),
          },
          { type: "separator" },
          {
            label: "Open Themes Folder",
            type: "normal",
            click: () => openThemesFolder(),
          },
        ]),
      },
      {
        label: "Themes",
        type: "submenu",
        submenu: Menu.buildFromTemplate([
          {
            label: "None",
            type: "radio",
            checked: getActiveTheme() === null,
            click: () => { applyTheme(mainWindow.webContents, null); updateTrayMenu(); },
          },
          ...getThemeFiles().map(file => ({
            label: file.replace(/\.css$/i, ""),
            type: "radio" as const,
            checked: getActiveTheme() === file,
            click: () => { applyTheme(mainWindow.webContents, file); updateTrayMenu(); },
          })),
          { type: "separator" as const },
          {
            label: "Cycle Theme",
            type: "normal" as const,
            click: () => { cycleTheme(mainWindow.webContents).then(() => updateTrayMenu()); },
          },
          {
            label: "Reload Theme",
            type: "normal" as const,
            click: () => reloadTheme(mainWindow.webContents),
          },
          {
            label: "Open Themes Folder",
            type: "normal" as const,
            click: () => openThemesFolder(),
          },
        ]),
      },
      { type: "separator" },
      {
        label: mainWindow.isVisible() ? "Hide App" : "Show App",
        type: "normal",
        click() {
          if (mainWindow.isVisible()) {
            mainWindow.hide();
          } else {
            mainWindow.show();
          }
        },
      },
      {
        label: "Quit App",
        type: "normal",
        click: quitApp,
      },
    ]),
  );
}
