import { BrowserWindow, ipcMain } from 'electron';
import { hideMainWindow, quitApplication, requestMainWindowClose, showMainWindow } from '../window';
import type { IpcContext } from './types';

export const registerWindowHandlers = ({ getMainWindow }: IpcContext) => {
  ipcMain.on(
    'window-control',
    (event, action: 'minimize' | 'maximize' | 'close' | 'fullscreen') => {
      const browserWindow = BrowserWindow.fromWebContents(event.sender) ?? getMainWindow();
      if (!browserWindow) return;
      if (action === 'minimize') browserWindow.minimize();
      else if (action === 'maximize') {
        if (browserWindow.isMaximized()) browserWindow.unmaximize();
        else browserWindow.maximize();
      } else if (action === 'fullscreen') {
        // Windows 上 setFullScreen 会因 titleBarStyle: 'hidden' 产生白边
        // 使用 simpleFullScreen 避免此问题，macOS/Linux 用标准全屏
        if (process.platform === 'win32') {
          browserWindow.setSimpleFullScreen(!browserWindow.isSimpleFullScreen());
        } else {
          browserWindow.setFullScreen(!browserWindow.isFullScreen());
        }
      } else if (action === 'close') {
        const mainWindow = getMainWindow();
        if (mainWindow && browserWindow.id === mainWindow.id) {
          requestMainWindowClose();
        } else {
          browserWindow.close();
        }
      }
    },
  );

  // 窗口拖动已改为 CSS -webkit-app-region: drag
  // 保留 IPC 通道兼容旧代码
  ipcMain.on('window-drag:start', () => {});
  ipcMain.on('window-drag:move', () => {});
  ipcMain.on('window-drag:end', () => {});

  ipcMain.on('window-toggle', () => {
    const browserWindow = getMainWindow();
    if (!browserWindow) return;
    if (browserWindow.isVisible()) {
      hideMainWindow();
    } else {
      showMainWindow();
    }
  });

  ipcMain.on('quit-app', () => {
    quitApplication();
  });
};
