const { app, Tray, Menu, nativeImage } = require('electron');
const path = require('path');
const fs = require('fs');
const { spawn } = require('child_process');

let tray = null;

function startServer() {
  console.log('Starting OpenChamber server...');
  
  let serverPath;
  let cwdPath;
  let execPath;
  
  if (app.isPackaged) {
    serverPath = path.join(process.resourcesPath, 'server', 'index.js');
    cwdPath = path.join(process.resourcesPath);
    execPath = path.join(process.resourcesPath, 'nodejs', 'node.exe');
  } else {
    serverPath = path.join(__dirname, '..', 'server', 'index.js');
    cwdPath = path.join(__dirname, '..');
    execPath = 'node';
  }
  
  console.log('Server path:', serverPath);
  console.log('CWD:', cwdPath);
  console.log('Exec:', execPath);
  
  const env = { ...process.env };
  env.OPENCHAMBER_PORT = '3000';
  env.OPENCODE_SKIP_START = 'true';
  
  // 创建批处理脚本
  const batContent = `"${execPath}" "${serverPath}"`;
  const batPath = path.join(cwdPath, 'start-server.bat');
  fs.writeFileSync(batPath, batContent);
  
  console.log('Bat path:', batPath);
  
  try {
    const child = spawn('cmd.exe', ['/c', batPath], {
      cwd: cwdPath,
      stdio: 'inherit',
      env: env,
      detached: false
    });
    
    child.on('error', (err) => {
      console.error('Server error:', err);
    });
    
    child.on('exit', (code) => {
      console.log('Server exited:', code);
    });
  } catch (e) {
    console.error('Failed to start:', e);
  }
}

function createTray() {
  try {
    let iconPath;
    if (app.isPackaged) {
      iconPath = path.join(process.resourcesPath, 'public', 'favicon.png');
    } else {
      iconPath = path.join(__dirname, '..', 'public', 'favicon.png');
    }
    
    let icon;
    try {
      icon = nativeImage.createFromPath(iconPath);
      if (icon.isEmpty()) {
        icon = nativeImage.createEmpty();
      }
    } catch (e) {
      icon = nativeImage.createEmpty();
    }
    
    tray = new Tray(icon);
    
    const contextMenu = Menu.buildFromTemplate([
      { label: 'OpenChamber Server', enabled: false },
      { type: 'separator' },
      { label: 'Open http://localhost:3000', click: () => {
        require('electron').shell.openExternal('http://localhost:3000');
      }},
      { type: 'separator' },
      { label: 'Quit', click: () => {
        app.quit();
      }}
    ]);
    
    tray.setToolTip('OpenChamber Server');
    tray.setContextMenu(contextMenu);
  } catch (e) {
    console.error('Failed to create tray:', e);
  }
}

app.whenReady().then(() => {
  startServer();
  createTray();
});

app.on('window-all-closed', () => {
  app.quit();
});
