const path = require("path");
const { app, BrowserWindow, Tray, Menu, ipcMain, dialog, shell, globalShortcut } = require("electron");
const { default: ipcTypes } = require("../src/ipc.types.commonjs");
const { initializeStore, store } = require("./electron-store");
const { autoUpdater } = require("electron-updater");
const log = require("electron-log");
const Sentry = require("@sentry/electron/main");

const isDev = !app.isPackaged;
const enableDevTools = isDev || process.env.ELECTRON_ENABLE_DEVTOOLS === "1";
const openDevToolsOnStart =
  enableDevTools && (isDev ? process.env.ELECTRON_OPEN_DEVTOOLS !== "0" : process.env.ELECTRON_OPEN_DEVTOOLS === "1");
const installReactDevTools = process.env.ELECTRON_INSTALL_REACT_DEVTOOLS === "1";

//const Nucleus = require("nucleus-nodejs");
let ipcHandlersInitialized = false;

function initializeIpcHandlers() {
  if (ipcHandlersInitialized) return;

  require("./ipc-main-handler");
  require("./analytics");
  ipcHandlersInitialized = true;
}

Sentry.init({
  dsn: "https://9840e0f304124299e379d9347e12d2e6@o492140.ingest.sentry.io/4505728058523648",
  ipcMode: Sentry.IPCMode.Protocol,
  ignoreErrors: [
    "SimpleURLLoaderWrapper",
    "Cannot read properties of null (reading 'webContents')",
    "EBUSY: resource busy or locked"
  ]
});
autoUpdater.autoDownload = true;

autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = "info";
log.info("App starting...", app.getVersion());

// Conditionally include the dev tools installer to load React DevTools.
let installExtension, REACT_DEVELOPER_TOOLS;
if (isDev && installReactDevTools) {
  const devTools = require("electron-devtools-installer");
  installExtension = devTools.default;
  REACT_DEVELOPER_TOOLS = devTools.REACT_DEVELOPER_TOOLS;
}

var menu = Menu.buildFromTemplate([
  {
    label: "File",
    submenu: [
      {
        label: "Relaunch",
        click() {
          app.exit();
          app.relaunch();
        }
      },
      {
        label: "Clear Settings",
        click() {
          store.reset("filePaths");
          mainWindow.webContents.session.clearStorageData();
        }
      },
      {
        label: "Sign Out",
        click() {
          mainWindow.webContents.send(ipcTypes.app.toRenderer.signOut);
        }
      },
      {
        label: "Exit",
        click() {
          app.quit();
        }
      }
    ]
    // Other code removed for brevity
  },
  {
    label: isDev ? "DEVELOPMENT -- HELP" : "Help",
    submenu: [
      {
        label: "Rescue",
        click() {
          shell.openExternal("http://imexrescue.com");
        }
      },
      {
        label: `Check for Updates (currently ${app.getVersion()})`,
        click() {
          checkForUpdates();
        }
      },
      {
        label: `Show Release Notes`,
        click() {
          mainWindow.webContents.send(
            ipcTypes.app.toRenderer.setReleaseNotes,
            require("./changelog.json")[app.getVersion()]
          );
        }
      },
      {
        label: "Open Config File",
        click() {
          shell.openPath(store.path);
        }
      },
      {
        label: "Open Log File",
        click() {
          shell.openPath(path.join(app.getPath("appData"), "ImeX RPS\\logs"));
        }
      },
      ...(enableDevTools
        ? [
            {
              label: "Toggle DevTools",
              accelerator: "CmdOrCtrl+Alt+I",
              click() {
                if (mainWindow) mainWindow.webContents.toggleDevTools();
              }
            }
          ]
        : []),
      {
        label: "Third Party Notices",
        click() {
          openNoticeWindow();
        }
      }
    ]
  }
]);

let mainWindow;
let noticeWindow;
let tray = null;

function createWindow() {
  // Create the browser window.
  Menu.setApplicationMenu(menu);

  const appPath = app.getAppPath();
  const preloadPath = isDev
    ? path.join(appPath, "electron", "preload-src.js")
    : path.join(appPath, "dist-electron", "preload.cjs");

  mainWindow = new BrowserWindow({
    // width: 800,
    // height: 600,
    title: `ImEX RPS ${app.getVersion()}`,
    icon: path.join(appPath, "src", "assets", "logo192.png"),

    webPreferences: {
      nodeIntegration: false,
      enableRemoteModule: false,
      webSecurity: true,
      worldSafeExecuteJavaScript: true,
      contextIsolation: true,
      devTools: enableDevTools,
      preload: preloadPath // use a preload script
    }
  });

  const gotTheLock = app.requestSingleInstanceLock();
  if (!gotTheLock) {
    app.quit();
  } else {
    app.on("second-instance", (event, commandLine, workingDirectory) => {
      // Someone tried to run a second instance, we should focus our window.
      if (mainWindow) {
        if (mainWindow.isMinimized()) mainWindow.restore();
        mainWindow.show();
        mainWindow.focus();
        mainWindow.maximize();
      }
    });

    // and load the index.html of the app.
    // win.loadFile("index.html");
    if (isDev) {
      mainWindow.loadURL("http://localhost:3006");
    } else {
      const indexPath = path.join(appPath, "build", "index.html");
      mainWindow.loadFile(indexPath);
    }

    // mainWindow.on("close", function (event) {
    //   event.preventDefault();
    //   mainWindow.hide();
    //   if (!tray) {
    //     tray = createTray();
    //   }
    // });

    mainWindow.on("minimize", function (event) {
      event.preventDefault();
      mainWindow.hide();
      if (!tray) {
        tray = createTray();
      }
    });

    ipcMain.on(ipcTypes.quit, (event, arg) => {
      app.isQuiting = true;
      app.quit();
    });

    // Auto-open DevTools in dev. DevTools itself can emit Chromium protocol
    // noise such as Autofill.* messages in Electron.
    if (openDevToolsOnStart) {
      mainWindow.webContents.openDevTools({
       // mode: "detach"
      });
    }

    mainWindow.maximize();

    if (enableDevTools) {
      globalShortcut.register("CommandOrControl+Shift+I", () => {
        mainWindow.webContents.toggleDevTools();
      });
      globalShortcut.register("CommandOrControl+Alt+I", () => {
        mainWindow.webContents.toggleDevTools();
      });
    }
  }
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(async () => {
  await initializeStore();
  initializeIpcHandlers();

  const { default: contextMenu } = await import("electron-context-menu");
  contextMenu({ showInspectElement: enableDevTools });

  createWindow();

  if (isDev) {
    console.log(`Path to Settings File: ${store.path}`);
    console.log(`Path to Log File: ${log.log}`);
    if (installReactDevTools) {
      installExtension(REACT_DEVELOPER_TOOLS)
        .then((name) => console.log(`Added Extension:  ${name}`))
        .catch((error) => console.log(`An error occurred: , ${error}`));
    }
  }

  setInterval(() => {
    checkForUpdates();
  }, 1000 * 60 * 30); //Added auto update check for RPS-38

  // ipcMain.on(ipcTypes.default.webcontent, (event, ...obj) => {
  //   console.log("event", event);
  //   mainWindow.webContents.send(event, obj);
  // });
});

if (isDev) app.setAppUserModelId(process.execPath);
else app.setAppUserModelId("com.imex.rps");
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  console.log("Activate");
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

function createTray() {
  const appPath = app.getAppPath();
  let appIcon = new Tray(path.join(appPath, "src", "assets", "logo192.png"));
  const contextMenu = Menu.buildFromTemplate([
    {
      label: "Show",
      click: function () {
        mainWindow.show();
      }
    },
    {
      label: "Exit",
      click: function () {
        app.isQuiting = true;
        app.quit();
      }
    }
  ]);

  appIcon.on("double-click", function (event) {
    mainWindow.show();
    mainWindow.maximize();
  });

  appIcon.setToolTip(`ImEX RPS ${app.getVersion()}`);
  appIcon.setContextMenu(contextMenu);
  return appIcon;
}

function checkForUpdates() {
  try {
    autoUpdater.checkForUpdates();
  } catch (error) {
    log.error("Error checking for updates", error);
  }
}

function openNoticeWindow() {
  if (noticeWindow) {
    noticeWindow.show();
    return;
  }

  noticeWindow = new BrowserWindow({
    width: 900,
    height: 700,
    title: "Third Party Notices",
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true,
      devTools: enableDevTools
    }
  });

  const appPath = app.getAppPath();
  noticeWindow.loadURL(`file://${path.join(appPath, "electron", "licenses.txt")}`);
  noticeWindow.on("closed", () => {
    noticeWindow = null;
  });
}

autoUpdater.on("update-available", (ev) => {
  log.log("Update available.", ev);
  if (mainWindow) mainWindow.webContents.send(ipcTypes.app.toRenderer.updateAvailable, ev);
});

autoUpdater.on("update-not-available", (ev) => {
  log.log("Update not available.", ev);
});

autoUpdater.on("error", (ev, err) => {
  log.log("Error in auto-updater.", ev, err);
});

ipcMain.on(ipcTypes.app.toMain.setReleaseChannel, (event, channel) => {
  autoUpdater.channel = channel;
  checkForUpdates();
});

ipcMain.on(ipcTypes.app.toMain.checkForUpdates, (event, args) => {
  checkForUpdates();
});

ipcMain.on(ipcTypes.app.toMain.downloadUpdates, (event, args) => {
  //Nucleus.track("DOWNLOAD_UPDATE_FROM_RENDERER");
  autoUpdater.downloadUpdate();
});

ipcMain.on(ipcTypes.app.toMain.installUpdates, (event, args) => {
  //Nucleus.track("INSTALL_UPDATE_FROM_RENDERER");
  const isSilent = true;
  const isForceRunAfter = true;
  autoUpdater.quitAndInstall(isSilent, isForceRunAfter);
});

autoUpdater.on("download-progress", (ev) => {
  log.log("Download Progress:", ev);
  if (mainWindow) mainWindow.webContents.send(ipcTypes.app.toRenderer.downloadProgress, ev);
});

autoUpdater.on("update-downloaded", (ev, info) => {
  //Nucleus.track("UPDATE_DOWNLOADED", ev);

  if (mainWindow) {
    mainWindow.webContents.send(ipcTypes.app.toRenderer.downloadProgress, {
      ...ev,
      percent: 100
    });
  }

  dialog
    .showMessageBox({
      type: "info",
      title: "ImeX RPS Update Manager",
      message: `ImEX RPS is ready to update to Version ${ev.version}. It is highly recommended that you update immediately. Would you like to update now? RPS will automatically restart.`,
      buttons: ["Yes", "No"]
    })
    .then(({ response }) => {
      if (response === 0) {
        const isSilent = true;
        const isForceRunAfter = true;
        store.set("showChangeLog", true);
        autoUpdater.quitAndInstall(isSilent, isForceRunAfter);
      } else {
        log.warn("Updated ignored.");
      }
    });
});
