const { BrowserWindow, globalShortcut, ipcMain, screen } = require("electron");
const path = require("node:path");
const storage = require("../storage");

let mouseEventsIgnored = false;

function createWindow(sendToRenderer, geminiSessionRef) {
  // Get layout preference (default to 'normal')
  let windowWidth = 1100;
  let windowHeight = 800;

  const mainWindow = new BrowserWindow({
    width: windowWidth,
    height: windowHeight,
    frame: false,
    transparent: true,
    hasShadow: false,
    alwaysOnTop: true,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false, // TODO: change to true
      backgroundThrottling: false,
      enableBlinkFeatures: "GetDisplayMedia",
      webSecurity: true,
      allowRunningInsecureContent: false,
    },
    backgroundColor: "#00000000",
  });

  const { session, desktopCapturer } = require("electron");
  session.defaultSession.setDisplayMediaRequestHandler(
    (request, callback) => {
      desktopCapturer.getSources({ types: ["screen"] }).then((sources) => {
        callback({ video: sources[0], audio: "loopback" });
      });
    },
    { useSystemPicker: true },
  );

  mainWindow.setResizable(false);
  mainWindow.setContentProtection(true);
  mainWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });

  // Hide from Windows taskbar
  if (process.platform === "win32") {
    try {
      mainWindow.setSkipTaskbar(true);
    } catch (error) {
      console.warn("Could not hide from taskbar:", error.message);
    }
  }

  // Hide from Mission Control on macOS
  if (process.platform === "darwin") {
    try {
      mainWindow.setHiddenInMissionControl(true);
    } catch (error) {
      console.warn("Could not hide from Mission Control:", error.message);
    }
  }

  // Center window at the top of the screen
  const primaryDisplay = screen.getPrimaryDisplay();
  const { width: screenWidth } = primaryDisplay.workAreaSize;
  const x = Math.floor((screenWidth - windowWidth) / 2);
  const y = 0;
  mainWindow.setPosition(x, y);

  if (process.platform === "win32") {
    mainWindow.setAlwaysOnTop(true, "screen-saver", 1);
  }

  mainWindow.loadFile(path.join(__dirname, "../index.html"));

  // After window is created, initialize keybinds
  mainWindow.webContents.once("dom-ready", () => {
    setTimeout(() => {
      const defaultKeybinds = getDefaultKeybinds();
      let keybinds = defaultKeybinds;

      // Load keybinds from storage
      const savedKeybinds = storage.getKeybinds();
      if (savedKeybinds) {
        keybinds = { ...defaultKeybinds, ...savedKeybinds };
      }

      updateGlobalShortcuts(
        keybinds,
        mainWindow,
        sendToRenderer,
        geminiSessionRef,
      );
    }, 150);
  });

  setupWindowIpcHandlers(mainWindow, sendToRenderer, geminiSessionRef);

  return mainWindow;
}

function getDefaultKeybinds() {
  const isMac = process.platform === "darwin";
  return {
    moveUp: isMac ? "Alt+Up" : "Ctrl+Up",
    moveDown: isMac ? "Alt+Down" : "Ctrl+Down",
    moveLeft: isMac ? "Alt+Left" : "Ctrl+Left",
    moveRight: isMac ? "Alt+Right" : "Ctrl+Right",
    toggleVisibility: isMac ? "Cmd+\\" : "Ctrl+\\",
    toggleClickThrough: isMac ? "Cmd+M" : "Ctrl+M",
    nextStep: isMac ? "Cmd+Enter" : "Ctrl+Enter",
    previousResponse: isMac ? "Cmd+[" : "Ctrl+[",
    nextResponse: isMac ? "Cmd+]" : "Ctrl+]",
    scrollUp: isMac ? "Cmd+Shift+Up" : "Ctrl+Shift+Up",
    scrollDown: isMac ? "Cmd+Shift+Down" : "Ctrl+Shift+Down",
    expandResponse: isMac ? "Cmd+E" : "Ctrl+E",
    emergencyErase: isMac ? "Cmd+Shift+E" : "Ctrl+Shift+E",
  };
}

function updateGlobalShortcuts(
  keybinds,
  mainWindow,
  sendToRenderer,
  geminiSessionRef,
) {
  console.log("Updating global shortcuts with:", keybinds);

  // Unregister all existing shortcuts
  globalShortcut.unregisterAll();

  const primaryDisplay = screen.getPrimaryDisplay();
  const { width, height } = primaryDisplay.workAreaSize;
  const moveIncrement = Math.floor(Math.min(width, height) * 0.1);

  // Register window movement shortcuts
  const movementActions = {
    moveUp: () => {
      if (!mainWindow.isVisible()) return;
      const [currentX, currentY] = mainWindow.getPosition();
      mainWindow.setPosition(currentX, currentY - moveIncrement);
    },
    moveDown: () => {
      if (!mainWindow.isVisible()) return;
      const [currentX, currentY] = mainWindow.getPosition();
      mainWindow.setPosition(currentX, currentY + moveIncrement);
    },
    moveLeft: () => {
      if (!mainWindow.isVisible()) return;
      const [currentX, currentY] = mainWindow.getPosition();
      mainWindow.setPosition(currentX - moveIncrement, currentY);
    },
    moveRight: () => {
      if (!mainWindow.isVisible()) return;
      const [currentX, currentY] = mainWindow.getPosition();
      mainWindow.setPosition(currentX + moveIncrement, currentY);
    },
  };

  // Register each movement shortcut
  Object.keys(movementActions).forEach((action) => {
    const keybind = keybinds[action];
    if (keybind) {
      try {
        globalShortcut.register(keybind, movementActions[action]);
        console.log(`Registered ${action}: ${keybind}`);
      } catch (error) {
        console.error(`Failed to register ${action} (${keybind}):`, error);
      }
    }
  });

  // Register toggle visibility shortcut
  if (keybinds.toggleVisibility) {
    try {
      globalShortcut.register(keybinds.toggleVisibility, () => {
        if (mainWindow.isVisible()) {
          mainWindow.hide();
        } else {
          mainWindow.showInactive();
        }
      });
      console.log(`Registered toggleVisibility: ${keybinds.toggleVisibility}`);
    } catch (error) {
      console.error(
        `Failed to register toggleVisibility (${keybinds.toggleVisibility}):`,
        error,
      );
    }
  }

  // Register toggle click-through shortcut
  if (keybinds.toggleClickThrough) {
    try {
      globalShortcut.register(keybinds.toggleClickThrough, () => {
        mouseEventsIgnored = !mouseEventsIgnored;
        if (mouseEventsIgnored) {
          mainWindow.setIgnoreMouseEvents(true, { forward: true });
          console.log("Mouse events ignored");
        } else {
          mainWindow.setIgnoreMouseEvents(false);
          console.log("Mouse events enabled");
        }
        mainWindow.webContents.send(
          "click-through-toggled",
          mouseEventsIgnored,
        );
      });
      console.log(
        `Registered toggleClickThrough: ${keybinds.toggleClickThrough}`,
      );
    } catch (error) {
      console.error(
        `Failed to register toggleClickThrough (${keybinds.toggleClickThrough}):`,
        error,
      );
    }
  }

  // Register next step shortcut (either starts session or takes screenshot based on view)
  if (keybinds.nextStep) {
    try {
      globalShortcut.register(keybinds.nextStep, async () => {
        console.log("Next step shortcut triggered");
        try {
          // Determine the shortcut key format
          const isMac = process.platform === "darwin";
          const shortcutKey = isMac ? "cmd+enter" : "ctrl+enter";

          // Use the new handleShortcut function
          mainWindow.webContents.executeJavaScript(`
                        cheatingDaddy.handleShortcut('${shortcutKey}');
                    `);
        } catch (error) {
          console.error("Error handling next step shortcut:", error);
        }
      });
      console.log(`Registered nextStep: ${keybinds.nextStep}`);
    } catch (error) {
      console.error(
        `Failed to register nextStep (${keybinds.nextStep}):`,
        error,
      );
    }
  }

  // Register previous response shortcut
  if (keybinds.previousResponse) {
    try {
      globalShortcut.register(keybinds.previousResponse, () => {
        console.log("Previous response shortcut triggered");
        sendToRenderer("navigate-previous-response");
      });
      console.log(`Registered previousResponse: ${keybinds.previousResponse}`);
    } catch (error) {
      console.error(
        `Failed to register previousResponse (${keybinds.previousResponse}):`,
        error,
      );
    }
  }

  // Register next response shortcut
  if (keybinds.nextResponse) {
    try {
      globalShortcut.register(keybinds.nextResponse, () => {
        console.log("Next response shortcut triggered");
        sendToRenderer("navigate-next-response");
      });
      console.log(`Registered nextResponse: ${keybinds.nextResponse}`);
    } catch (error) {
      console.error(
        `Failed to register nextResponse (${keybinds.nextResponse}):`,
        error,
      );
    }
  }

  // Register scroll up shortcut
  if (keybinds.scrollUp) {
    try {
      globalShortcut.register(keybinds.scrollUp, () => {
        console.log("Scroll up shortcut triggered");
        sendToRenderer("scroll-response-up");
      });
      console.log(`Registered scrollUp: ${keybinds.scrollUp}`);
    } catch (error) {
      console.error(
        `Failed to register scrollUp (${keybinds.scrollUp}):`,
        error,
      );
    }
  }

  // Register scroll down shortcut
  if (keybinds.scrollDown) {
    try {
      globalShortcut.register(keybinds.scrollDown, () => {
        console.log("Scroll down shortcut triggered");
        sendToRenderer("scroll-response-down");
      });
      console.log(`Registered scrollDown: ${keybinds.scrollDown}`);
    } catch (error) {
      console.error(
        `Failed to register scrollDown (${keybinds.scrollDown}):`,
        error,
      );
    }
  }

  // Register expand response shortcut
  if (keybinds.expandResponse) {
    try {
      globalShortcut.register(keybinds.expandResponse, () => {
        console.log("Expand response shortcut triggered");
        sendToRenderer("expand-response");
      });
      console.log(`Registered expandResponse: ${keybinds.expandResponse}`);
    } catch (error) {
      console.error(
        `Failed to register expandResponse (${keybinds.expandResponse}):`,
        error,
      );
    }
  }

  // Register emergency erase shortcut
  if (keybinds.emergencyErase) {
    try {
      globalShortcut.register(keybinds.emergencyErase, () => {
        console.log("Emergency Erase triggered!");
        if (mainWindow && !mainWindow.isDestroyed()) {
          mainWindow.hide();

          if (geminiSessionRef.current) {
            geminiSessionRef.current.close();
            geminiSessionRef.current = null;
          }

          sendToRenderer("clear-sensitive-data");

          setTimeout(() => {
            const { app } = require("electron");
            app.quit();
          }, 300);
        }
      });
      console.log(`Registered emergencyErase: ${keybinds.emergencyErase}`);
    } catch (error) {
      console.error(
        `Failed to register emergencyErase (${keybinds.emergencyErase}):`,
        error,
      );
    }
  }
}

function setupWindowIpcHandlers(mainWindow, sendToRenderer, geminiSessionRef) {
  ipcMain.on("view-changed", (event, view) => {
    if (!mainWindow.isDestroyed()) {
      const primaryDisplay = screen.getPrimaryDisplay();
      const { width: screenWidth } = primaryDisplay.workAreaSize;

      if (view === "assistant") {
        // Shrink window for live view
        const liveWidth = 850;
        const liveHeight = 400;
        const x = Math.floor((screenWidth - liveWidth) / 2);
        mainWindow.setSize(liveWidth, liveHeight);
        mainWindow.setPosition(x, 0);
      } else {
        // Restore full size
        const fullWidth = 1100;
        const fullHeight = 800;
        const x = Math.floor((screenWidth - fullWidth) / 2);
        mainWindow.setSize(fullWidth, fullHeight);
        mainWindow.setPosition(x, 0);
        mainWindow.setIgnoreMouseEvents(false);
      }
    }
  });

  ipcMain.handle("window-minimize", () => {
    if (!mainWindow.isDestroyed()) {
      mainWindow.minimize();
    }
  });

  ipcMain.on("update-keybinds", (event, newKeybinds) => {
    if (!mainWindow.isDestroyed()) {
      updateGlobalShortcuts(
        newKeybinds,
        mainWindow,
        sendToRenderer,
        geminiSessionRef,
      );
    }
  });

  ipcMain.handle("toggle-window-visibility", async (event) => {
    try {
      if (mainWindow.isDestroyed()) {
        return { success: false, error: "Window has been destroyed" };
      }

      if (mainWindow.isVisible()) {
        mainWindow.hide();
      } else {
        mainWindow.showInactive();
      }
      return { success: true };
    } catch (error) {
      console.error("Error toggling window visibility:", error);
      return { success: false, error: error.message };
    }
  });

  ipcMain.handle("update-sizes", async (event) => {
    // With the sidebar layout, the window size is user-controlled.
    // This handler is kept for compatibility but is a no-op now.
    return { success: true };
  });
}

module.exports = {
  createWindow,
  getDefaultKeybinds,
  updateGlobalShortcuts,
  setupWindowIpcHandlers,
};
