import { app, BrowserWindow, session } from 'electron';
import { fileURLToPath } from 'url';
import path from 'path';
import { setContentSecurityPolicy } from './main-js/security.js';
import { createWindow } from './main-js/window-setup.js';
import { registerIpcHandlers as registerDeviceManagerHandlers } from './main-js/adb/device-manager.js';
import { registerIpcHandlers as registerDeviceInfoHandlers } from './main-js/adb/device-info.js';
import { registerIpcHandlers as registerScreenshotHandlers } from './main-js/adb/screenshot.js';
import { registerIpcHandlers as registerInputHandlers } from './main-js/adb/input.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;



// 应用启动逻辑：设置 CSP、创建窗口、监听激活事件
app.whenReady().then(() => {
  setContentSecurityPolicy(isDev);
  createWindow();

  // 注册所有 ADB 相关的 IPC 处理器
  registerDeviceManagerHandlers();
  registerDeviceInfoHandlers();
  registerScreenshotHandlers();
  registerInputHandlers();

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
  
  // 禁用 before-quit 事件的确认对话框
  app.on('before-quit', (event) => {
    // 不阻止退出，直接退出应用
    // 如果需要清理资源，可以在这里添加
  });
});

// 应用关闭逻辑：macOS 保持运行，其他平台退出
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    // 直接退出，不询问
    app.exit(0);
  }
});

